Skip to content

Commit

Permalink
pass image/format as arguments to virt.import_image()
Browse files Browse the repository at this point in the history
I originally rejected this because import_image() would be copying
the image file if it wasn't in /var/lib/libvirt/images, and it seemed
better and more explicit to make the user put the image on the right
place (into /var/lib/libvirt/images).

However, an alternate view point on "import" is "just use the disk
image in-place, wherever it is", which is what the function does now
with this change.

In that case, passing arguments makes more sense than setting instance-
wide variables.

Signed-off-by: Jiri Jaburek <[email protected]>
  • Loading branch information
comps committed Oct 24, 2024
1 parent 09c9ab3 commit a7485ca
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
4 changes: 1 addition & 3 deletions lib/osbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,7 @@ def create(self, *, blueprint=None, bp_verbatim=None, rpmpack=None, **kwargs):
Path(self.osbuild_log).write_text(log)

# import the created qcow2 image as a VM
self.disk_path = image_path
self.disk_format = 'qcow2'
self.import_image(**kwargs)
self.import_image(image_path, 'qcow2', **kwargs)


def composer_cli(*args, log=True, check=True, **kwargs):
Expand Down
20 changes: 12 additions & 8 deletions lib/virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,24 +492,25 @@ def install(
self.disk_path = disk_path
self.disk_format = disk_format

def import_image(self, *, secure_boot=False):
def import_image(self, disk_path, disk_format='raw', *, secure_boot=False):
"""
Import an existing disk image, creating a new guest domain from it.
"""
if not self.disk_path or not self.disk_format:
raise RuntimeError("'disk_path' and 'disk_format' need to be set'")
if not Path(self.disk_path).exists():
raise RuntimeError(f"{self.disk_path} doesn't exist")
The image is used as-is, in place. No copy or move is performed.
Ideally, the image should be located in GUEST_IMG_DIR and named
after the '.name' attribute of the guest instance.
"""
if not Path(disk_path).exists():
raise RuntimeError(f"{disk_path} doesn't exist")

util.log(f"importing {self.disk_path} as {self.disk_format}")
util.log(f"importing {disk_path} as {disk_format}")

cpus = os.cpu_count() or 1

virt_install = [
'pseudotty', 'virt-install',
'--name', self.name, '--vcpus', str(cpus), '--memory', '2000',
'--disk', f'path={self.disk_path},format={self.disk_format},cache=unsafe',
'--disk', f'path={disk_path},format={disk_format},cache=unsafe',
'--network', 'network=default',
'--graphics', 'none', '--console', 'pty', '--rng', '/dev/urandom',
'--noreboot', '--import',
Expand All @@ -525,6 +526,9 @@ def import_image(self, *, secure_boot=False):
executable = util.libdir / 'pseudotty'
util.subprocess_run(virt_install, executable=executable, check=True)

self.disk_path = disk_path
self.disk_format = disk_format

def start(self):
if guest_domstate(self.name) == 'shut off':
virsh('start', self.name, check=True)
Expand Down

0 comments on commit a7485ca

Please sign in to comment.