From e98df68ded6515c3f6d8f8854697c15407aff000 Mon Sep 17 00:00:00 2001 From: Radostin Stoyanov Date: Sat, 26 Aug 2017 21:42:06 +0100 Subject: Add virt-builder source Add implementation for virt-builder source which aims to create container root file system from VM image build with virt-builder. Usage examples: $ virt-bootstrap virt-builder://fedora-25 /tmp/foo $ virt-bootstrap virt-builder://ubuntu-16.04 /tmp/bar --root-password secret $ virt-bootstrap virt-builder://fedora-25 /tmp/foo -f qcow2 --idmap 0:1000:10 $ sudo virt-bootstrap virt-builder://fedora-25 /tmp/foo --idmap 0:1000:10 Tests are also introduced along with the implementation. They cover creation of root file system and UID/GID mapping for 'dir' and 'qcow2' output format by mocking the build_image() method to avoid the time consuming call to virt-builder which might also require network connection with function which creates dummy disk image. Setting root password is handled by virt-builder and hence the introduced test only ensures that the password string is passed correctly. --- src/virtBootstrap/sources/__init__.py | 1 + src/virtBootstrap/sources/virt_builder_source.py | 154 +++++++++++++++++++++++ src/virtBootstrap/virt_bootstrap.py | 2 +- 3 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 src/virtBootstrap/sources/virt_builder_source.py (limited to 'src') diff --git a/src/virtBootstrap/sources/__init__.py b/src/virtBootstrap/sources/__init__.py index e891e9b..be6b25c 100644 --- a/src/virtBootstrap/sources/__init__.py +++ b/src/virtBootstrap/sources/__init__.py @@ -24,3 +24,4 @@ sources - Class definitions which process container image or from virtBootstrap.sources.file_source import FileSource from virtBootstrap.sources.docker_source import DockerSource +from virtBootstrap.sources.virt_builder_source import VirtBuilderSource diff --git a/src/virtBootstrap/sources/virt_builder_source.py b/src/virtBootstrap/sources/virt_builder_source.py new file mode 100644 index 0000000..628544f --- /dev/null +++ b/src/virtBootstrap/sources/virt_builder_source.py @@ -0,0 +1,154 @@ +# -*- coding: utf-8 -*- +# Authors: Radostin Stoyanov +# +# Copyright (C) 2017 Radostin Stoyanov +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +""" +VirtBuilderSource aim is to extract the root file system from VM image +build with virt-builder from template. +""" + +import os +import logging +import subprocess +import tempfile + +import guestfs +from virtBootstrap import utils + + +# pylint: disable=invalid-name +# Create logger +logger = logging.getLogger(__name__) + + +class VirtBuilderSource(object): + """ + Extract root file system from image build with virt-builder. + """ + def __init__(self, **kwargs): + """ + Create container rootfs by building VM from virt-builder template + and extract the rootfs. + + @param uri: Template name + @param fmt: Format used to store the output [dir, qcow2] + @param uid_map: Mappings for UID of files in rootfs + @param gid_map: Mappings for GID of files in rootfs + @param root_password: Root password to set in rootfs + @param progress: Instance of the progress module + """ + # Parsed URIs: + # - "virt-builder:///