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.
master
Dustin 2023-02-26 12:20:44 -06:00
parent 184d270472
commit ed0e73f510
2 changed files with 9 additions and 5 deletions

View File

@ -2,6 +2,7 @@ import argparse
import logging import logging
import tempfile import tempfile
from pathlib import Path from pathlib import Path
from typing import Optional
import rich.console import rich.console
import rich.logging import rich.logging
@ -16,14 +17,14 @@ class CLI:
def __init__(self, console: rich.console.Console) -> None: def __init__(self, console: rich.console.Console) -> None:
self.console = console 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 tempfile.NamedTemporaryFile(suffix='.tar') as tmp_tar:
with self.console.status('Exporting OCI image as tarball ...'): with self.console.status('Exporting OCI image as tarball ...'):
tar = Path(tmp_tar.name) tar = Path(tmp_tar.name)
image.make_tar(name, tar) image.make_tar(name, tar)
self.console.print('Created tarball:', tar) self.console.print('Created tarball:', tar)
with self.console.status('Converting tarball to QCOW2 image ...'): 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) self.console.print('Created QCOW2 image:', img)
def list_images(self) -> None: def list_images(self) -> None:
@ -49,6 +50,7 @@ def parse_args() -> argparse.Namespace:
sp = parser.add_subparsers(dest='command', required=True) sp = parser.add_subparsers(dest='command', required=True)
p_import = sp.add_parser('import', help='Import container image') 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') p_import.add_argument('name', help='Container image name')
sp.add_parser('list', help='List VM images') sp.add_parser('list', help='List VM images')
@ -68,6 +70,6 @@ def main() -> None:
cli = CLI(console) cli = CLI(console)
match args.command: match args.command:
case 'import': case 'import':
cli.import_image(args.name) cli.import_image(args.name, args.size)
case 'list': case 'list':
cli.list_images() cli.list_images()

View File

@ -5,7 +5,7 @@ import string
import subprocess import subprocess
import tempfile import tempfile
from pathlib import Path from pathlib import Path
from typing import Iterable from typing import Iterable, Optional
import xdg 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 = get_image_dir() / name
dest = dest.with_suffix('.qcow2') dest = dest.with_suffix('.qcow2')
dest.parent.mkdir(parents=True, exist_ok=True) dest.parent.mkdir(parents=True, exist_ok=True)