def create_snapshot_qemu(connection, index, base_image): """Create the snapshot image for a VM using a base image :param connection: SSH connection to the remote host :param index: index of the VM to be spawn :param base_image: path of the VM base image in the remote host :return: snapshot image path """ vm_image = '/var/lib/libvirt/images/%s.qcow2' % index connection.execute('rm -- "%s"' % vm_image) status, _, _ = connection.execute('test -r %s' % base_image) if status: if not os.access(base_image, os.R_OK): raise exceptions.LibvirtQemuImageBaseImageNotPresent( vm_image=vm_image, base_image=base_image) # NOTE(ralonsoh): done in two steps to avoid root permission # issues. LOG.info('Copy %s from execution host to remote host', base_image) file_name = os.path.basename(os.path.normpath(base_image)) connection.put_file(base_image, '/tmp/%s' % file_name) status, _, error = connection.execute( 'mv -- "/tmp/%s" "%s"' % (file_name, base_image)) if status: raise exceptions.LibvirtQemuImageCreateError( vm_image=vm_image, base_image=base_image, error=error) LOG.info('Convert image %s to %s', base_image, vm_image) qemu_cmd = ('qemu-img create -f qcow2 -o backing_file=%s %s' % (base_image, vm_image)) status, _, error = connection.execute(qemu_cmd) if status: raise exceptions.LibvirtQemuImageCreateError( vm_image=vm_image, base_image=base_image, error=error) return vm_image
def gen_cdrom_image(connection, file_path, vm_name, vm_user, key_filename, mac, ip): """Generate ISO image for CD-ROM """ user_config = [" - name: {user_name}", " ssh_authorized_keys:", " - {pub_key_str}"] if vm_user != "root": user_config.append(" sudo: ALL=(ALL) NOPASSWD:ALL") meta_data = "/tmp/meta-data" user_data = "/tmp/user-data" network_data = "/tmp/network-config" with open(".".join([key_filename, "pub"]), "r") as pub_key_file: pub_key_str = pub_key_file.read().rstrip() user_conf = os.linesep.join(user_config).format(pub_key_str=pub_key_str, user_name=vm_user) cmd_lst = [ "touch %s" % meta_data, USER_DATA_TEMPLATE.format(user_file=user_data, host=vm_name, user_config=user_conf), NETWORK_DATA_TEMPLATE.format(network_file=network_data, mac_address=mac, ip_address=ip), "genisoimage -output {0} -volid cidata -joliet -r {1} {2} {3}".format(file_path, meta_data, user_data, network_data), "rm {0} {1} {2}".format(meta_data, user_data, network_data), ] for cmd in cmd_lst: LOG.info(cmd) status, _, error = connection.execute(cmd) if status: raise exceptions.LibvirtQemuImageCreateError(error=error)