def unmount_image(self, mount_point): utils.check_prerequisite_command('umount') if self.euca.debug: print 'Unmounting image...' subprocess.Popen(['umount', '-d', mount_point], stdout=subprocess.PIPE).communicate()[0] os.rmdir(mount_point)
def make_fs(self, image_path, fs_type = None, uuid = None, label = None): mkfs_prog = self.MAKEFS_CMD if fs_type: mkfs_prog = "mkfs.%s" % fs_type else: fs_type = "ext3" tunecmd = [ ] if fs_type.startswith("ext"): mkfs = [ mkfs_prog , '-F', image_path ] if uuid: tunecmd = [ 'tune2fs', '-U', uuid, image_path ] if label: mkfs.extend([ '-L', label ]) elif fs_type == "xfs": mkfs = [ mkfs_prog , image_path ] if label: mkfs.extend([ '-L', label ]) tunecmd = [ 'xfs_admin', '-U', uuid, image_path ] elif fs_type == "btrfs": if uuid: raise(UnsupportedException("btrfs with uuid not supported")) if label: mkfs.extend([ '-L', label ]) else: raise(UnsupportedException("unsupported fs %s" % fs_type)) utils.check_prerequisite_command(mkfs_prog) if self.debug: print 'Creating filesystem with %s' % mkfs makefs_cmd = subprocess.Popen(mkfs,subprocess.PIPE).communicate()[0] if len(tunecmd) > 0: utils.check_prerequisite_command(tunecmd[0]) tune_cmd = subprocess.Popen(tunecmd,subprocess.PIPE).communicate()[0]
def make_fs(self, image_path, fs_type=None, uuid=None, label=None): mkfs_prog = self.MAKEFS_CMD if fs_type: mkfs_prog = "mkfs.%s" % fs_type else: fs_type = "ext3" tunecmd = [] if fs_type.startswith("ext"): mkfs = [mkfs_prog, '-F', image_path] if uuid: tunecmd = ['tune2fs', '-U', uuid, image_path] if label: mkfs.extend(['-L', label]) elif fs_type == "xfs": mkfs = [mkfs_prog, image_path] if label: mkfs.extend(['-L', label]) tunecmd = ['xfs_admin', '-U', uuid, image_path] elif fs_type == "btrfs": if uuid: raise (UnsupportedException("btrfs with uuid not supported")) if label: mkfs.extend(['-L', label]) else: raise (UnsupportedException("unsupported fs %s" % fs_type)) utils.check_prerequisite_command(mkfs_prog) if self.debug: print 'Creating filesystem with %s' % mkfs makefs_cmd = subprocess.Popen(mkfs, subprocess.PIPE).communicate()[0] if len(tunecmd) > 0: utils.check_prerequisite_command(tunecmd[0]) tune_cmd = subprocess.Popen(tunecmd, subprocess.PIPE).communicate()[0]
def copy_to_image(self, mount_point, volume_path, excludes): try: utils.check_prerequisite_command('rsync') except NotFoundError: raise CopyError rsync_cmd = ['rsync', '-aXS'] for exclude in excludes: rsync_cmd.append('--exclude') rsync_cmd.append(exclude) rsync_cmd.append(volume_path) rsync_cmd.append(mount_point) if self.euca.debug: print 'Copying files...' for exclude in excludes: print 'Excluding:', exclude pipe = subprocess.Popen(rsync_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = pipe.communicate() for dir in self.img.ESSENTIAL_DIRS: dir_path = os.path.join(mount_point, dir) if not os.path.exists(dir_path): os.mkdir(dir_path) if dir == 'tmp': os.chmod(dir_path, 01777) self.img.make_essential_devs(mount_point) mtab_file = open('/etc/mtab', 'r') while 1: mtab_line = mtab_file.readline() if not mtab_line: break mtab_line_parts = mtab_line.split(' ') mount_location = mtab_line_parts[1] fs_type = mtab_line_parts[2] if fs_type == 'tmpfs': mount_location = mount_location[1:] dir_path = os.path.join(mount_point, mount_location) if not os.path.exists(dir_path): if self.euca.debug: print 'Making essential directory %s' \ % mount_location os.makedirs(dir_path) mtab_file.close() if pipe.returncode: # rsync return code 23: Partial transfer due to error # rsync return code 24: Partial transfer due to vanished source files if pipe.returncode in (23, 24): print >> sys.stderr, 'Warning: rsync reports files partially copied:' print >> sys.stderr, output else: print >> sys.stderr, 'Error: rsync failed with return code %d' \ % pipe.returncode raise CopyError
def copy_to_image(self, mount_point, volume_path, excludes): try: utils.check_prerequisite_command('rsync') except NotFoundError: raise CopyError rsync_cmd = ['rsync', '-aXS'] for exclude in excludes: rsync_cmd.append('--exclude') rsync_cmd.append(exclude) rsync_cmd.append(volume_path) rsync_cmd.append(mount_point) if self.euca.debug: print 'Copying files...' for exclude in excludes: print 'Excluding:', exclude pipe = subprocess.Popen(rsync_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = pipe.communicate() for dir in self.img.ESSENTIAL_DIRS: dir_path = os.path.join(mount_point, dir) if not os.path.exists(dir_path): os.mkdir(dir_path) if dir == 'tmp': os.chmod(dir_path, 01777) self.img.make_essential_devs(mount_point) mtab_file = open('/etc/mtab', 'r') while 1: mtab_line = mtab_file.readline() if not mtab_line: break mtab_line_parts = mtab_line.split(' ') mount_location = mtab_line_parts[1] fs_type = mtab_line_parts[2] if fs_type == 'tmpfs': mount_location = mount_location[1:] dir_path = os.path.join(mount_point, mount_location) if not os.path.exists(dir_path): if self.euca.debug: print 'Making essential directory %s' \ % mount_location os.makedirs(dir_path) mtab_file.close() if pipe.returncode: # rsync return code 23: Partial transfer due to error # rsync return code 24: Partial transfer due to vanished source files if pipe.returncode in (23, 24): print 'Warning: rsync reports files partially copied:' print output else: print 'Error: rsync failed with return code %d' \ % pipe.returncode raise CopyError
def mount_image(self, image_path): utils.check_prerequisite_command('mount') tmp_mnt_point = '/tmp/%s' % hex(BN.rand(16))[2:6] if not os.path.exists(tmp_mnt_point): os.makedirs(tmp_mnt_point) if self.euca.debug: print 'Creating loopback device...' loop_dev = self.create_loopback(image_path) if self.euca.debug: print 'Mounting image...' subprocess.Popen(['mount', loop_dev, tmp_mnt_point], stdout=subprocess.PIPE).communicate() return (tmp_mnt_point, loop_dev)
def create_loopback(self, image_path): utils.check_prerequisite_command('losetup') tries = 0 while tries < MAX_LOOP_DEVS: loop_dev = subprocess.Popen(['losetup', '-f'], stdout=subprocess.PIPE).communicate()[0].replace('\n', '') if loop_dev: output = subprocess.Popen(['losetup', '%s' % loop_dev, '%s' % image_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() if not output[1]: return loop_dev else: print 'Could not create loopback device. Aborting' raise CommandFailed tries += 1
def tarzip_image(self, prefix, file, path): utils.check_prerequisite_command('tar') print 'Compressing image' targz = '%s.tar.gz' % os.path.join(path, prefix) targzfile = open(targz, 'w') # make process pipes tar_cmd = ['tar', 'ch', '-S'] file_path = self.euca.get_file_path(file) if file_path: tar_cmd.append('-C') tar_cmd.append(file_path) tar_cmd.append(self.euca.get_relative_filename(file)) else: tar_cmd.append(file) tarproc = subprocess.Popen(tar_cmd, stdout=subprocess.PIPE) zipproc = subprocess.Popen(['gzip'], stdin=subprocess.PIPE, stdout=targzfile) # pass tar output to digest and gzip sha_image = sha() buf = os.read(tarproc.stdout.fileno(), 8196) while buf: zipproc.stdin.write(buf) sha_image.update(buf) buf = os.read(tarproc.stdout.fileno(), 8196) zipproc.stdin.close() targzfile.close() tarproc.wait() zipproc.wait() for p, pname in [(tarproc, 'tar'), (zipproc, 'gzip')]: if p.returncode != 0: print "'%s' returned error (%i)" % (pname, p.returncode) raise CommandFailed if os.path.getsize(targz) <= 0: print 'Could not tar/compress image' raise CommandFailed return (targz, hexlify(sha_image.digest()))
def tarzip_image(self, prefix, file, path): utils.check_prerequisite_command('tar') print 'Compressing image' targz = '%s.tar.gz' % os.path.join(path, prefix) targzfile = open(targz, 'w') # make process pipes tar_cmd = ['tar', 'ch', '-S'] file_path = self.euca.get_file_path(file) if file_path: tar_cmd.append('-C') tar_cmd.append(file_path) tar_cmd.append(self.euca.get_relative_filename(file)) else: tar_cmd.append(file) tarproc = subprocess.Popen(tar_cmd, stdout=subprocess.PIPE) zipproc = subprocess.Popen(['gzip'], stdin=subprocess.PIPE, stdout=targzfile) # pass tar output to digest and gzip sha_image = sha() buf=os.read(tarproc.stdout.fileno(), 8196) while buf: zipproc.stdin.write(buf) sha_image.update(buf) buf=os.read(tarproc.stdout.fileno(), 8196) zipproc.stdin.close(); targzfile.close() tarproc.wait() zipproc.wait() for p, pname in [(tarproc, 'tar'), (zipproc, 'gzip')]: if p.returncode != 0: print "'%s' returned error (%i)" % (pname, p.returncode) raise CommandFailed if os.path.getsize(targz) <= 0: print 'Could not tar/compress image' raise CommandFailed return (targz, hexlify(sha_image.digest()))