Skip to content

Instantly share code, notes, and snippets.

@amotl
Created August 18, 2014 21:17
Show Gist options
  • Save amotl/065775aba1d56e7b9577 to your computer and use it in GitHub Desktop.
Save amotl/065775aba1d56e7b9577 to your computer and use it in GitHub Desktop.
Dumps proper lvcreate, mkfs and mkswap commands for cloning the filesystem layout of Xen guests
#!/usr/bin/env python
import os
import re
import sys
import shlex
import subprocess
# clone filesystem layout of Xen guests
# TODO:
# - don't hardcode target volume group; here: vg1
# - don't hardcode target filesystem type; here: ext3
def include(filename):
if os.path.exists(filename):
execfile(filename)
return locals()
def run(command):
cmd = shlex.split(command)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
return out
def lvdisplay_get_size(payload):
m = re.search('LV Size\s+(.+)$', payload, re.MULTILINE)
return m.group(1).replace(' ', '')
def main():
configfile = sys.argv[1]
config = include(configfile)
#print config['disk']
for disk in config['disk']:
device = disk.split(':')[1].split(',')[0]
#print device,
size = lvdisplay_get_size(run('lvdisplay %s' % device))
#print size
device_basename = os.path.basename(device)
lvcreate = "lvcreate -L '%s' -n %s vg1" % (size, device_basename)
print lvcreate
device_real = '/dev/vg1/%s' % device_basename
mkfs = 'mkfs.ext3 %s' % device_real
if 'swap' in device_basename:
mkfs = 'mkswap %s' % device_real
print mkfs
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment