def mount(image, path, sync=False, readOnly=False, partition=0): image = params.convert(image, convert=str, check=os.path.exists) path = params.convert(path, convert=os.path.realpath, check=os.path.exists) partition = params.convert(partition, convert=int, gte=0, lte=4) ImageError.check(not _isMounted(path), ImageError.CODE_ALREADY_MOUNTED, "Path is already mounted", {"path": path}) options = ["loop"] if not partition else [] if sync: options.append("sync") if readOnly: options.append("ro") try: if partition: partitions = _getPartitions(image) start, size = partitions[partition - 1] start, size = start * 512, size * 512 ImageError.check(start > 0 and size > 0, ImageError.CODE_FAILED_TO_CREATE, "Wrong partition table", { "image": image, "table": partitions }) options += ["offset=%d" % start, "sizelimit=%d" % size] cmd.run(["mount", "-o%s" % ",".join(options), image, path]) except cmd.CommandError, err: raise ImageError(ImageError.CODE_FAILED_TO_MOUNT, "Failed to mount image", { "path": path, "image": image, "options": options, "error": repr(err) })
def create(path, format="qcow2", size=None, backingImage=None): format = params.convert(format, convert=str) path = params.convert(path, check=lambda p: not os.path.exists(p)) size = params.convert(size, convert=str, null=True) backingImage = params.convert(backingImage, check=os.path.exists, null=True) c = ["qemu-img", "create", "-f", format] if backingImage: c += ["-o", "backing_file=%s" % backingImage] c.append(path) if size and not backingImage: c.append(size) try: cmd.run(c) except CommandError, err: data = { "path": path, "format": format, "size": size, "backing_image": backingImage } data.update(err.data) raise QemuImgError(QemuImgError.CODE_FAILED_TO_CREATE, "Failed to create image", data)
def unmount(path, ignoreUnmounted=False): path = params.convert(path, convert=os.path.realpath, check=os.path.exists) if not _isMounted(path): if ignoreUnmounted: return raise ImageError(ImageError.CODE_ALREADY_UNMOUNTED, "Path is already unmounted", {"path": path}) try: cmd.run(["umount", path]) except cmd.CommandError, err: raise ImageError(ImageError.CODE_FAILED_TO_UNMOUNT, "Failed to unmount image", {"path": path, "error": repr(err)})
def convert(src, dst, srcFormat="qcow2", dstFormat="qcow2", compress=True): src = params.convert(src, check=_checkImage) dst = params.convert(dst, check=lambda p: not os.path.exists(p)) srcFormat = params.convert(srcFormat, convert=str) dstFormat = params.convert(dstFormat, convert=str) try: cmd.run(["qemu-img", "convert", "-c" if compress else "", "-f", srcFormat, "-O", dstFormat, src, dst]) except CommandError, err: data = {"src": src, "dst": dst, "src_format": srcFormat, "dst_format": dstFormat} data.update(err.data) raise QemuImgError(QemuImgError.CODE_FAILED_TO_CONVERT, "Failed to convert image", data)
def download(urls, dest, hash=None): urls = params.convert(urls, convert=list) dest = params.convert(dest, convert=os.path.realpath) hash = params.convert(hash, convert=str, null=True) path, fname = os.path.split(dest) Aria2Error.check(os.path.exists(path), Aria2Error.CODE_DEST_PATH_DOES_NOT_EXIST, "Destination path does not exit", {"path": path}) args = ["aria2c", "-d", path, "-o", fname, "-c", "-V", "--auto-file-renaming=false", "--allow-overwrite=true"] if hash: args.append("--checksum=%s" % hash) args += urls cmd.run(args)
def prepare(self, vmid, imagepath, nlxtp_floppy_filename=None, nlxtp_device_filename=None,ram=512,cpus=1, vncport=None, vncpassword=None,keyboard="en-us"): self._checkStatus(vmid,[StateName.CREATED]) driver_type = self.DRIVER_MAPPING[self.TYPE] if self.TYPE == TechName.KVM: cmd_ = ["virt-install", "--connect", self.HYPERVISOR_MAPPING[self.TYPE], "--name", "vm_%d" % vmid, "--ram", str(ram), "--vcpus", str(cpus), #"--os-type", ostype, "--import", #Use the image given to install a guest os "--disk", "path=%s,driver_type=%s" % (imagepath, driver_type), # VDA ,device=disk,driver_type=%s,bus=virtio, % driver_type ("--disk" if nlxtp_device_filename else ""),("path=%s" % nlxtp_device_filename #,device=disk,bus=virtio if nlxtp_device_filename else ""), # Virtual 'big' device for nlxtp ("--disk" if nlxtp_floppy_filename else ""), # Floppy device for nlXTP ("path=%s,device=floppy,cache=writethrough" % nlxtp_floppy_filename if nlxtp_floppy_filename else ""), "--graphics", "vnc%s%s,keymap=%s" % ( (",port=%s" % vncport if vncpassword else ""), (",password=%s" % vncpassword if vncpassword else ""), keyboard), "--nonetworks", #Don't create automatically a default bridge "--noautoconsole", # Don't enter a console on the device after install "--noreboot"] #Don't boot the device after install else: cmd_ = ["virt-install", "--connect", self.HYPERVISOR_MAPPING[self.TYPE], "--name", "vm_%d" % vmid, "--ram", str(ram), "--vcpus", str(cpus), # "--os-type", ostype, "--import", # Use the image given to install a guest os "--filesystem", "%s,/,type=mount" % (imagepath), # VDA ,device=disk,driver_type=%s,bus=virtio, % driver_type #("--disk" if nlxtp_device_filename else ""), #("path=%s" % nlxtp_device_filename # ,device=disk,bus=virtio # if nlxtp_device_filename else ""), # Virtual 'big' device for nlxtp #("--disk" if nlxtp_floppy_filename else ""), # Floppy device for nlXTP #( #"path=%s,device=floppy,cache=writethrough" % nlxtp_floppy_filename if nlxtp_floppy_filename else ""), "--host-devices","/dev/net/tun" "--graphics", "vnc%s%s,keymap=%s" % ( (",port=%s" % vncport if vncpassword else ""), (",password=%s" % vncpassword if vncpassword else ""), keyboard), "--nonetworks", # Don't create automatically a default bridge "--noautoconsole", # Don't enter a console on the device after install "--noreboot"] # Don't boot the device after install cmd.run(cmd_) self.writeInitialConfig(self.TYPE, vmid)
def _imageInfo(path, format=None): format = params.convert(format, convert=str, null=True) path = params.convert(path, check=os.path.exists) try: if format: res = cmd.run(["qemu-img", "info", "-f", format, "--output=json", path]) else: res = cmd.run(["qemu-img", "info", "--output=json", path]) return json.loads(res) except CommandError, err: data = {"path": path} data.update(err.data) raise QemuImgError(QemuImgError.CODE_INVALID_IMAGE, "Invalid image", data)
def create(path, size): path = params.convert(path, convert=os.path.realpath, check=lambda p: not os.path.exists(p)) size = params.convert(size, convert=int, gte=1) try: cmd.run(["mkfs.vfat", "-C", path, str(size)]) except cmd.CommandError, err: raise VFatError(VFatError.CODE_FAILED_TO_CREATE, "Failed to create image", { "path": path, "size": size, "error": repr(err) })
def _imageInfo(path, format=None): format = params.convert(format, convert=str, null=True) path = params.convert(path, check=os.path.exists) try: if format: res = cmd.run( ["qemu-img", "info", "-f", format, "--output=json", path]) else: res = cmd.run(["qemu-img", "info", "--output=json", path]) return json.loads(res) except CommandError, err: data = {"path": path} data.update(err.data) raise QemuImgError(QemuImgError.CODE_INVALID_IMAGE, "Invalid image", data)
def unmount(path, ignoreUnmounted=False): path = params.convert(path, convert=os.path.realpath, check=os.path.exists) if not _isMounted(path): if ignoreUnmounted: return raise VFatError(VFatError.CODE_ALREADY_UNMOUNTED, "Path is already unmounted", {"path": path}) try: cmd.run(["umount", path]) except cmd.CommandError, err: raise VFatError(VFatError.CODE_FAILED_TO_UNMOUNT, "Failed to unmount image", { "path": path, "error": repr(err) })
def _virsh(self, cmd_, args=None, timeout=None): if not args: args = [] cmd_ = ["virsh", "-c", self.HYPERVISOR_MAPPING[self.TYPE], cmd_] + args if timeout: cmd_ = ["perl", "-e", "alarm %d; exec @ARGV" % timeout] + cmd_ out = cmd.run(cmd_) return out
def prepare(self, vmid, imagepath, nlxtp_floppy_filename=None, nlxtp_device_filename=None, ram=512, cpus=1, vncport=None, vncpassword=None, keyboard="en-us"): self._checkStatus(vmid, [StateName.CREATED]) driver_type = self.DRIVER_MAPPING[self.TYPE] cmd_ = [ "virt-install", "--connect", self.HYPERVISOR_MAPPING[self.TYPE], "--name", "vm_%d" % vmid, "--ram", str(ram), "--vcpus", str(cpus), #"--os-type", ostype, "--import", #Use the image given to install a guest os "--disk", "path=%s,driver_type=%s" % (imagepath, driver_type ), # VDA ,device=disk,driver_type=%s,bus=virtio, % driver_type ("--disk" if nlxtp_device_filename else ""), ( "path=%s" % nlxtp_device_filename #,device=disk,bus=virtio if nlxtp_device_filename else ""), # Virtual 'big' device for nlxtp ("--disk" if nlxtp_floppy_filename else ""), # Floppy device for nlXTP ("path=%s,device=floppy,cache=writethrough" % nlxtp_floppy_filename if nlxtp_floppy_filename else ""), "--graphics", "vnc%s%s,keymap=%s" % ((",port=%s" % vncport if vncpassword else ""), (",password=%s" % vncpassword if vncpassword else ""), keyboard), "--nonetworks", #Don't create automatically a bridge "--noautoconsole", # Don't enter a console on the device after install "--noreboot" ] #Don't boot the device after install cmd.run(cmd_) self.writeInitialConfig(self.TYPE, vmid)
def create(path, format="qcow2", size=None, backingImage=None): format = params.convert(format, convert=str) path = params.convert(path, check=lambda p: not os.path.exists(p)) size = params.convert(size, convert=str, null=True) backingImage = params.convert(backingImage, check=os.path.exists, null=True) c = ["qemu-img", "create", "-f", format] if backingImage: c += ["-o", "backing_file=%s" % backingImage] c.append(path) if size and not backingImage: c.append(size) try: cmd.run(c) except CommandError, err: data = {"path": path, "format": format, "size": size, "backing_image": backingImage} data.update(err.data) raise QemuImgError(QemuImgError.CODE_FAILED_TO_CREATE, "Failed to create image", data)
def _getPartitions(path): output = cmd.run(["sfdisk", "-d", path]) pat = re.compile('.*\s+start=\s*(\d+)\s*,\s*size=\s*(\d+)\s*,.*') partitions = [] for p in output.splitlines(): m = pat.match(p) if m: partitions.append(map(int, m.groups())) return partitions
def mount(image, path, sync=False, readOnly=False): image = params.convert(image, convert=str, check=os.path.exists) path = params.convert(path, convert=os.path.realpath, check=os.path.exists) VFatError.check(not _isMounted(path), VFatError.CODE_ALREADY_MOUNTED, "Path is already mounted", {"path": path}) options = ["loop"] if sync: options.append("sync") if readOnly: options.append("ro") try: cmd.run(["mount", "-o%s" % ",".join(options), image, path]) except cmd.CommandError, err: raise VFatError(VFatError.CODE_FAILED_TO_MOUNT, "Failed to mount image", { "path": path, "image": image, "options": options, "error": repr(err) })
def mount(image, path, sync=False, readOnly=False, partition=0): image = params.convert(image, convert=str, check=os.path.exists) path = params.convert(path, convert=os.path.realpath, check=os.path.exists) partition = params.convert(partition, convert=int, gte=0, lte=4) ImageError.check(not _isMounted(path), ImageError.CODE_ALREADY_MOUNTED, "Path is already mounted", {"path": path}) options = ["loop"] if not partition else [] if sync: options.append("sync") if readOnly: options.append("ro") try: if partition: partitions = _getPartitions(image) start, size = partitions[partition-1] start, size = start * 512, size * 512 ImageError.check(start > 0 and size > 0, ImageError.CODE_FAILED_TO_CREATE, "Wrong partition table", {"image": image, "table": partitions}) options += ["offset=%d" % start, "sizelimit=%d" % size] cmd.run(["mount", "-o%s" % ",".join(options), image, path]) except cmd.CommandError, err: raise ImageError(ImageError.CODE_FAILED_TO_MOUNT, "Failed to mount image", {"path": path, "image": image, "options": options, "error": repr(err)})
def convert(src, dst, srcFormat="qcow2", dstFormat="qcow2", compress=True): src = params.convert(src, check=_checkImage) dst = params.convert(dst, check=lambda p: not os.path.exists(p)) srcFormat = params.convert(srcFormat, convert=str) dstFormat = params.convert(dstFormat, convert=str) try: cmd.run([ "qemu-img", "convert", "-c" if compress else "", "-f", srcFormat, "-O", dstFormat, src, dst ]) except CommandError, err: data = { "src": src, "dst": dst, "src_format": srcFormat, "dst_format": dstFormat } data.update(err.data) raise QemuImgError(QemuImgError.CODE_FAILED_TO_CONVERT, "Failed to convert image", data)
def create(path, size, nested=False): path = params.convert(path, convert=os.path.realpath, check=lambda p: not os.path.exists(p)) size = params.convert(size, convert=int, gte=8192) nested = params.convert(nested, convert=bool) try: cmd.run(["dd", "if=/dev/zero", "of=%s" % path, "bs=1k", "count=%d" % size]) device = path if nested: cmd.run(["sfdisk", path], input=",\n") partitions = _getPartitions(path) start, size = partitions[0] start, size = start * 512, size * 512 ImageError.check(start > 0 and size > 0, ImageError.CODE_FAILED_TO_CREATE, "Failed to create partition table", {"path": path, "table": partitions}) device = cmd.run(["losetup", "-f", "--show", "--offset", str(start), "--sizelimit", str(size), path]).splitlines()[0] ImageError.check(os.path.exists(device), ImageError.CODE_FAILED_TO_CREATE, "Failed to bind to loop device", {"path": path, "size": size, "loop_device": device}) cmd.run(["mkfs.vfat", device]) if nested: cmd.run(["losetup", "-d", device]) except cmd.CommandError, err: raise ImageError(ImageError.CODE_FAILED_TO_CREATE, "Failed to create image", {"path": path, "size": size, "error": repr(err)})
def plugins(): return cmd.run(["busybox", "--list"]).splitlines()
def create(path, size, nested=False): path = params.convert(path, convert=os.path.realpath, check=lambda p: not os.path.exists(p)) size = params.convert(size, convert=int, gte=8192) nested = params.convert(nested, convert=bool) try: cmd.run( ["dd", "if=/dev/zero", "of=%s" % path, "bs=1k", "count=%d" % size]) device = path if nested: cmd.run(["sfdisk", path], input=",\n") partitions = _getPartitions(path) start, size = partitions[0] start, size = start * 512, size * 512 ImageError.check(start > 0 and size > 0, ImageError.CODE_FAILED_TO_CREATE, "Failed to create partition table", { "path": path, "table": partitions }) device = cmd.run([ "losetup", "-f", "--show", "--offset", str(start), "--sizelimit", str(size), path ]).splitlines()[0] ImageError.check(os.path.exists(device), ImageError.CODE_FAILED_TO_CREATE, "Failed to bind to loop device", { "path": path, "size": size, "loop_device": device }) cmd.run(["mkfs.vfat", device]) cmd.run(["sync"]) if nested: cmd.run(["losetup", "-d", device]) except cmd.CommandError, err: raise ImageError(ImageError.CODE_FAILED_TO_CREATE, "Failed to create image", { "path": path, "size": size, "error": repr(err) })