Created
August 18, 2014 21:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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