From ed0e73f5104491299aac2d7663e35f076cc395ca Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sun, 26 Feb 2023 12:20:44 -0600 Subject: [PATCH] import: Add --size argument The `ocivm import` command now supports setting the size of the created filesystem/disk image. This can be used to add free space to the disk. --- src/ocivm/cli.py | 8 +++++--- src/ocivm/image.py | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/ocivm/cli.py b/src/ocivm/cli.py index e53c64a..5c3a67e 100644 --- a/src/ocivm/cli.py +++ b/src/ocivm/cli.py @@ -2,6 +2,7 @@ import argparse import logging import tempfile from pathlib import Path +from typing import Optional import rich.console import rich.logging @@ -16,14 +17,14 @@ class CLI: def __init__(self, console: rich.console.Console) -> None: self.console = console - def import_image(self, name: str) -> None: + def import_image(self, name: str, size: Optional[str]) -> None: with tempfile.NamedTemporaryFile(suffix='.tar') as tmp_tar: with self.console.status('Exporting OCI image as tarball ...'): tar = Path(tmp_tar.name) image.make_tar(name, tar) self.console.print('Created tarball:', tar) with self.console.status('Converting tarball to QCOW2 image ...'): - img = image.import_image(name, tar) + img = image.import_image(name, tar, size) self.console.print('Created QCOW2 image:', img) def list_images(self) -> None: @@ -49,6 +50,7 @@ def parse_args() -> argparse.Namespace: sp = parser.add_subparsers(dest='command', required=True) p_import = sp.add_parser('import', help='Import container image') + p_import.add_argument('--size', help='Image size') p_import.add_argument('name', help='Container image name') sp.add_parser('list', help='List VM images') @@ -68,6 +70,6 @@ def main() -> None: cli = CLI(console) match args.command: case 'import': - cli.import_image(args.name) + cli.import_image(args.name, args.size) case 'list': cli.list_images() diff --git a/src/ocivm/image.py b/src/ocivm/image.py index 2a074e0..5044ff2 100644 --- a/src/ocivm/image.py +++ b/src/ocivm/image.py @@ -5,7 +5,7 @@ import string import subprocess import tempfile from pathlib import Path -from typing import Iterable +from typing import Iterable, Optional import xdg @@ -63,7 +63,9 @@ def make_tar(name: str, dest: Path) -> None: ) -def import_image(name: str, src: Path, size: str = '+1G') -> Path: +def import_image(name: str, src: Path, size: Optional[str]) -> Path: + if size is None: + size = '+1G' dest = get_image_dir() / name dest = dest.with_suffix('.qcow2') dest.parent.mkdir(parents=True, exist_ok=True)