@@ -0,0 +1,2333 @@
+#!/usr/bin/env python%0A%0A%22%22%22%0AThis script is used for creating a qcow2 image and then mount it to a%0Anbd device in order to install LFS on it and then virtualize it with KVM or%0Awhatever hypervisor you are using.%0A%22%22%22%0A%0Aimport os%0Aimport shutil%0Aimport subprocess%0Aimport shlex%0A%0A%0A# Image configuration%0Aimage_name = %22LFS%22%0Aimage_format = %22qcow2%22%0Afs_type = %22ext4%22%0Aswap = True%0Aimage_size = %2210G%22%0Aimage_path = %22/home/%22+ os.getlogin() + %22/%22 + image_name + %22.%22 + image_format%0A%0A# Directory configuration%0Ainstallation_path = %22/mnt/LFS%22%0Asources = %22/mnt/LFS/sources%22%0A%0A# This function allow to check if the nbd kernel module is loaded; if not, it%0A# will load it. It requires sudo permissions.%0Adef check_nbd():%0A    print(%22Checking nbd module...%22)%0A    if os.access(%22/dev/nbd0%22, os.F_OK):%0A        print(%22nbd kernel module already loaded.%22)%0A%0A    else:%0A        print(%22nbd kernel module not loaded. Loading...%22)%0A        subprocess.check_output(shlex.split('sudo modprobe nbd max_part=16'))%0A        if os.access(%22/dev/nbd0%22, os.F_OK):%0A            print(%22nbd module: OK%22)%0A%0A# Checks if the build dir structure is correclty set%0Adef check_dirs():%0A%0A    if os.access(installation_path, os.F_OK):%0A        print(%22Build dir is correctly set.%22)%0A    else:%0A        print(%22Build dir does not exist, creating it...%22)%0A        subprocess.check_output(shlex.split('sudo mkdir ' + installation_path))%0A        check_dirs()%0A%0Adef build_image():%0A    print(%22Building image...%22)%0A%0A    # Image creation process%0A    subprocess.check_output(shlex.split('qemu-img create -f ' + image_format +%0A        ' ' + image_path + ' ' + image_size))%0A    if os.access(image_path, os.F_OK):%0A        print(%22Image successfully created.%22)%0A%0A%0A    # linking the image with the nbd device%0A    print(%22nbd-connecting the image...%22)%0A    subprocess.check_call(shlex.split('sudo qemu-nbd -c /dev/nbd0 '+ image_path))%0A%0A    # Setting a partition table for the image%0A    print(%22Partitioning...%22)%0A    os.system('%7B%5Cn' %5C%0A    'echo %22,512,82%22%5Cn' %5C%0A    'echo %22;%22%5Cn' %5C%0A    '%7D %7C sudo sfdisk /dev/nbd0 -D -uM%5Cn')%0A%0A    # Formatting the image, swap and ext3%0A    print(%22Formating image%22)%0A    os.system(%22sudo mkswap /dev/nbd0p1; sudo mkfs.ext3 /dev/nbd0p2 %22 + %5C%0A    %22; sudo mount /dev/nbd0p2 %22+ installation_path)%0A    print(%22Mounting image to %22 + installation_path + %22.%22)%0A%0A# Calling functions%0Acheck_nbd()%0Acheck_dirs()%0Abuild_image()%0A

add image-builder.py