iso.py 3.82 KB
Newer Older
1 2 3 4
#!/usr/bin/env python3
"""Creates the actual ISO"""

import os
Fabian Kosmale's avatar
Fabian Kosmale committed
5 6
import glob
import re
7 8 9 10 11
import subprocess
import datetime
import shutil

inform = print  # TODO
Fabian Kosmale's avatar
Fabian Kosmale committed
12
iso_root = "iso_root"
13

Fabian Kosmale's avatar
Fabian Kosmale committed
14 15 16
squashfs_files = [] # list of squashfs image files

def make_suqash(directory="chakra", name=None):
17 18
    """TODO."""
    assert os.path.isdir(directory), "mksquash only works on directories!"
Fabian Kosmale's avatar
Fabian Kosmale committed
19 20 21 22
    if name is None:
      sqimg = os.path.join(os.getcwd(), "{}.img".format(directory))
    else:
      sqimg = name
23 24 25 26 27 28 29
    inform("Generating SquashFS image for {}".format(directory))
    # TODO: check if existing image is out of date
    inform("Creating SquashFS image. This may take some time...")
    command = "mksquashfs {} {} -noappend -comp xz -Xbcj x86".format(
        directory,
        sqimg
    ).split(" ")
Fabian Kosmale's avatar
Fabian Kosmale committed
30 31 32
    if not os.path.isfile(sqimg):
      subprocess.check_call(command)
    squashfs_files.append(sqimg)
33 34

def prepare_iso():
Fabian Kosmale's avatar
Fabian Kosmale committed
35 36 37
    label='CHAKRA_{}'.format(
        datetime.datetime.now().strftime("%Y-%m-%d"))
    inform(label)
38 39
    # create root directory for iso image
    os.makedirs(iso_root)
Fabian Kosmale's avatar
Fabian Kosmale committed
40 41 42 43 44 45
    # for more information see http://www.syslinux.org/wiki/index.php/ISOLINUX
    """Copies the required files"""
    # copy all isolinux files to iso_root/isolinux
    isolinux_dir = os.path.join(iso_root, "isolinux")
    # this will create isolinux_dir and copy everything from config/isolinux
    shutil.copytree("./config/isolinux", isolinux_dir)
46
    shutil.copy("/usr/lib/syslinux/bios/isolinux.bin", iso_root)
Fabian Kosmale's avatar
Fabian Kosmale committed
47 48 49 50 51 52 53
    isolinux_cfg = os.path.join(isolinux_dir, "isolinux.cfg")
    with open(isolinux_cfg) as infile:
        lines = infile.readlines()
        with open(isolinux_cfg, "w") as outfile:
            for line in lines:
                line = re.sub("%CHAKRAISO_LABEL%", label, line)
                outfile.write(line)
Fabian Kosmale's avatar
Fabian Kosmale committed
54 55 56
    # create the directory where the image will reside
    image_dir = os.path.join(iso_root, "chakra/boot/x86_64/")
    os.makedirs(image_dir)
Fabian Kosmale's avatar
Fabian Kosmale committed
57
    # copy the initramfs
Fabian Kosmale's avatar
Fabian Kosmale committed
58
    shutil.copy("./chakra/boot/chakraiso.img", os.path.join(image_dir, "chakraiso.img"))
59
    # copy the kernel
Fabian Kosmale's avatar
Fabian Kosmale committed
60
    shutil.copy("./chakra/boot/vmlinuz-linux", os.path.join(image_dir, "chakraiso"))
Fabian Kosmale's avatar
Fabian Kosmale committed
61 62 63 64 65 66 67
    # copy the isomounts file
    shutil.copy("./config/isomounts", os.path.join(iso_root, "chakra"))
    # copy the squashfs images
    squashfs_dir = os.path.join(iso_root, "chakra", "x86_64")
    os.makedirs(squashfs_dir)
    for file in squashfs_files:
      shutil.copy(file, squashfs_dir)
68 69 70 71 72

def make_iso(publisher='Chakra GNU/Linux <http://www.chakra-project.org>',
             application='Chakra GNU/Linux Live/Rescue CD',
             label='CHAKRA_{}'.format(
                 datetime.datetime.now().strftime("%Y-%m-%d")),
Fabian Kosmale's avatar
Fabian Kosmale committed
73
             imgname='output.iso'
74 75
             ):
    inform("Building ISO")
Fabian Kosmale's avatar
Fabian Kosmale committed
76 77
    # check if we really need the one below
    old_command = """
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    mkisofs -r -l
        -eltorito-boot isolinux/isolinux.bin
        -eltorito-catalog isolinux/boot.cat
        -uid 0 -gid 0
        -udf
        --iso-level 3
        -input-charset utf-8 -p "prepared by config"
        -no-emul-boot -boot-load-size 4 -boot-info-table
        -publisher {publisher}
        -A {application}
        -V {label}
        -o {imgname} .
        """.format(**{
            "publisher": publisher,
            "application": application,
            "label": label,
            "imgname": imgname,
        })
Fabian Kosmale's avatar
Fabian Kosmale committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    command = """mkisofs
-output
output.iso
-b
isolinux/isolinux.bin
-c
isolinux/boot.cat
-uid
0
-gid
0
--iso-level
3
-no-emul-boot
-boot-load-size
4
-boot-info-table
-l
-V
CHAKRA_2014-03-21
-udf
Fabian Kosmale's avatar
Fabian Kosmale committed
117 118
iso_root
"""
Fabian Kosmale's avatar
Fabian Kosmale committed
119 120 121
    with open("log", "w") as log:
      subprocess.check_call(command.strip().split("\n"), shell=False, stderr=log)
      subprocess.check_call(["isohybrid", imgname], stderr=log)
122 123

if __name__ == "__main__":
Fabian Kosmale's avatar
Fabian Kosmale committed
124
    make_suqash(os.path.abspath("chakra"), "root-image.sqfs")
Fabian Kosmale's avatar
Fabian Kosmale committed
125
    prepare_iso()
Fabian Kosmale's avatar
Fabian Kosmale committed
126
    make_iso()