BuildISO: Difference between revisions

From LFScript
→‎Create the ISO image: Change 'mkisofs' to 'xorrisofs'
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category:Scripts]]
This page defines one of the core features of LFScript; The ability to create a [[Live CD]].
This page defines one of the core features of LFScript; The ability to create a [[Live CD]].


Line 6: Line 7:


== Sources ==
== Sources ==
* <sourcecode>rootfs-$(uname -m).img</sourcecode>
* <sourcecode>file://rootfs-$(uname -m).img</sourcecode>


'''Note:''' You obviously can not download this file, it [[Creating a Live CD|can only be created by you]].
'''Note:''' You obviously can not download this file, it [[Creating a Live CD|can only be created by you]].
Line 25: Line 26:


LABEL live
LABEL live
     MENU LABEL ^Boot the live system ($(uname -m))
     MENU LABEL ^Boot live ($(uname -m), default)
     MENU DEFAULT
     MENU DEFAULT
     KERNEL /boot/$(uname -m)/vmlinuz
     KERNEL /boot/$(uname -m)/vmlinuz
     APPEND initrd=/boot/$(uname -m)/initram.fs quiet
     APPEND initrd=/boot/$(uname -m)/initram.fs quiet
LABEL live_force_vga
    MENU LABEL ^Boot live ($(uname -m), 1024x768)
    KERNEL /boot/$(uname -m)/vmlinuz
    APPEND initrd=/boot/$(uname -m)/initram.fs quiet vga=792


LABEL harddisk
LABEL harddisk
Line 42: Line 48:
#!/bin/busybox sh
#!/bin/busybox sh


# Initramfs boot script 1.3.2 (2014-06-13)
# Initramfs boot script 1.4.0 (2017-02-26)
# Copyright (c) 2010-2014   Marcel van den Boer
# Copyright (c) 2010-2017   Marcel van den Boer
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# Permission is hereby granted, free of charge, to any person obtaining a copy
Line 107: Line 113:
cd /
cd /


#########################################
#############################################
}; unionMount() { # UNIONFS LIVECD MODE #
}; overlayMount() { # OVERLAYFS LIVECD MODE #
#########################################
#############################################


# A union mount takes one or more directories and combines them transparantly
# An overlay mount takes one or more directories and combines them transparantly
# in a third. This function creates a writable directory in memory (tmpfs) and
# in a third. This function creates a writable directory in memory (tmpfs) and
# uses it to overlay the read-only system image, resulting in a fully writable
# uses it to overlay the read-only system image, resulting in a fully writable
# root file system.
# root file system.
#
# The only downside to this method is that it requires a union type filesystem
# in the kernel, which can only be accomplished by patching the kernel as there
# is no such feature in a vanilla kernel.


mkdir -p /mnt/writable
mkdir -p /mnt/writable
mount -t tmpfs -o rw tmpfs /mnt/writable
mount -t tmpfs -o rw tmpfs /mnt/writable
mkdir -p /mnt/writable/upper
mkdir -p /mnt/writable/work


UNIONFSOPT="/mnt/writable=rw:/mnt/system=ro"
D_LOWER="/mnt/system"
mount -t unionfs -o dirs=${UNIONFSOPT} unionfs ${ROOT} 2> /dev/null || {
D_UPPER="/mnt/writable/upper"
D_WORK="/mnt/writable/work"
OVERLAYFSOPT="lowerdir=${D_LOWER},upperdir=${D_UPPER},workdir=${D_WORK}"


     # If UnionFS fails, fall back to copy/bind mounting
mount -t overlay overlay -o ${OVERLAYFSOPT} ${ROOT} 2> /dev/null || {
 
     # If OverlayFS fails, fall back to copy/bind mounting
     copyBindMount
     copyBindMount
}
}
Line 202: Line 210:


if [ "${DEVICE}" == "" ]; then
if [ "${DEVICE}" == "" ]; then
     echo "FATAL: Boot medium not found."
     echo "STOP: Boot medium not found."
     exec /bin/busybox sh
     exec /bin/busybox sh
fi
fi
Line 209: Line 217:
mount -t squashfs -o ro,loop /mnt/medium/boot/${ARCH}/root.sfs /mnt/system || {
mount -t squashfs -o ro,loop /mnt/medium/boot/${ARCH}/root.sfs /mnt/system || {
     if [ -r /mnt/medium/boot/${ARCH}/root.sfs ]; then
     if [ -r /mnt/medium/boot/${ARCH}/root.sfs ]; then
         echo "FATAL: Unable to mount system image. The kernel probably lacks"
         echo "STOP: Unable to mount system image. The kernel probably lacks"
         echo "       SquashFS support. You may need to recompile it."
         echo "     SquashFS support. You may need to recompile it."
     else
     else
         echo "FATAL: Unable to mount system image. It seems to be missing."
         echo "STOP: Unable to mount system image. It seems to be missing."
     fi
     fi


Line 222: Line 230:


# Select LiveCD mode
# Select LiveCD mode
unionMount # Might fall back to copyBindMount
overlayMount # Might fall back to copyBindMount


# Move current mounts to directories accessible in the new root
# Move current mounts to directories accessible in the new root
Line 290: Line 298:
popd
popd
gzip -9 initramfs.cpio
gzip -9 initramfs.cpio
rm -rf mnt_init</build>
rm&nbsp;-rf mnt_init</build>


=== Mount, verify and compress the system image ===
=== Mount, verify and compress the system image ===
Line 335: Line 343:
<build>mkdir -p live/boot/{isolinux,$(uname -m)}
<build>mkdir -p live/boot/{isolinux,$(uname -m)}
cp -v /usr/share/syslinux/isolinux.bin live/boot/isolinux
cp -v /usr/share/syslinux/isolinux.bin live/boot/isolinux
cp -v /boot/vmlinuz-*                 live/boot/$(uname -m)/vmlinuz
cp -v /usr/share/syslinux/ldlinux.c32 live/boot/isolinux
cp -v /boot/config-*                   live/boot/$(uname -m)/config</build>
 
cp -v mnt/boot/vmlinuz-* live/boot/$(uname -m)/vmlinuz
cp -v mnt/boot/config-* live/boot/$(uname -m)/config</build>


Copy all files from the root file system image that have been prepared to [[BuildISO Customisation|customize the Live CD]]:
Copy all files from the root file system image that have been prepared to [[BuildISO Customisation|customize the Live CD]]:
Line 349: Line 359:
Unless a customised configuration has been copied a moment ago, use the boot loader configuration created when we started:
Unless a customised configuration has been copied a moment ago, use the boot loader configuration created when we started:
<build>if [ ! -e "live/boot/isolinux/isolinux.cfg" ]; then
<build>if [ ! -e "live/boot/isolinux/isolinux.cfg" ]; then
     cp -v /usr/share/syslinux/menu.c32 live/boot/isolinux
     cp -v /usr/share/syslinux/menu.c32    live/boot/isolinux
     mv -v isolinux.cfg                 live/boot/isolinux
    cp -v /usr/share/syslinux/libutil.c32 live/boot/isolinux
     mv -v isolinux.cfg                   live/boot/isolinux
else
else
     rm&nbsp;-rf isolinux.cfg
     rm&nbsp;-rf isolinux.cfg

Latest revision as of 21:41, 15 April 2017

This page defines one of the core features of LFScript; The ability to create a Live CD.

This page is set up in way so that it can be imported into LFScript automatically, just like the pages for the other Extra packages are. Al though you should be able to run these commands yourself, it is intended for them to be executed by LFScript.

LFScript can find this software under the name buildiso.

Sources

Note: You obviously can not download this file, it can only be created by you.

Dependencies

Installation

isolinux.cfg

This creates a default configuration file for the isolinux boot loader. If you have created a custom configuration, this file file be ignored:

cat > isolinux.cfg << EOF
DEFAULT menu.c32
PROMPT 0
MENU TITLE Select an option...
TIMEOUT 300

LABEL live
    MENU LABEL ^Boot live ($(uname -m), default)
    MENU DEFAULT
    KERNEL /boot/$(uname -m)/vmlinuz
    APPEND initrd=/boot/$(uname -m)/initram.fs quiet

LABEL live_force_vga
    MENU LABEL ^Boot live ($(uname -m), 1024x768)
    KERNEL /boot/$(uname -m)/vmlinuz
    APPEND initrd=/boot/$(uname -m)/initram.fs quiet vga=792

LABEL harddisk
    MENU LABEL Boot from first ^Hard disk
    LOCALBOOT 0x80

EOF

Init script

This creates the core initialisation script for the Live CD. It is this script that runs immediately after the Linux kernel has been loaded by the boot loader. It is responsible for locating and mounting the CD medium, mounting the root file system image and then give control to the Operating System inside that image:

cat > init.sh << "EndOfFile"
#!/bin/busybox sh

# Initramfs boot script 1.4.0 (2017-02-26)
# Copyright (c) 2010-2017   Marcel van den Boer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

# FS layout at the start of this script:
# - /boot/id_label
# - /bin/busybox
# - /dev/console (created by kernel)
# - /init (this file)

set -e

ARCH="<ARCH>"

###########################################
copyBindMount() { # COPY/BIND LIVECD MODE #
###########################################

# This function bind-mounts directories which are designed to be capable of
# read-only access and copies the remaining directories to a tmpfs.
#
# The downside of this method is that the resulting root filesystem is not
# fully writable. So, for example, installation of new programs will not be
# possible.
#
# However, this function can be used without any modification to the kernel and
# is therefore perfect for use as a fallback if other options are not available.

# Mount a tmpfs where the new rootfs will be.
mount -t tmpfs tmpfs ${ROOT} # Allows remounting root in the bootscripts

# Bind mount read-only filesystems, copy the rest
cd /mnt/system
for dir in $(ls -1); do
    case ${dir} in
        lost+found)
            ;;
        bin | boot | lib | opt | sbin | usr)
            mkdir ${ROOT}/${dir}
            mount --bind ${dir} ${ROOT}/${dir}
            ;;
        *)
            cp -R ${dir} ${ROOT}
            ;;
    esac
done
cd /

#############################################
}; overlayMount() { # OVERLAYFS LIVECD MODE #
#############################################

# An overlay mount takes one or more directories and combines them transparantly
# in a third. This function creates a writable directory in memory (tmpfs) and
# uses it to overlay the read-only system image, resulting in a fully writable
# root file system.

mkdir -p /mnt/writable
mount -t tmpfs -o rw tmpfs /mnt/writable
mkdir -p /mnt/writable/upper
mkdir -p /mnt/writable/work

D_LOWER="/mnt/system"
D_UPPER="/mnt/writable/upper"
D_WORK="/mnt/writable/work"
OVERLAYFSOPT="lowerdir=${D_LOWER},upperdir=${D_UPPER},workdir=${D_WORK}"

mount -t overlay overlay -o ${OVERLAYFSOPT} ${ROOT} 2> /dev/null || {

    # If OverlayFS fails, fall back to copy/bind mounting
    copyBindMount
}

######################
} # END OF FUNCTIONS #
######################

# Make required applets easier to access
for applet in cat chmod cp cut grep ls mkdir mknod mount umount switch_root; do
    /bin/busybox ln /bin/busybox /bin/${applet}
done

# Clear the screen
#clear # Don't! This will clear the Linux boot logo when using a framebuffer.
       # If you want to clear the screen on boot add the "clear" command to
       # '/usr/share/live/sec_init.sh' in the system image.

# Create device nodes required to run this script
# Note: /dev/console will already be available in the ramfs
mknod /dev/null c  1  3

mknod /dev/scd0 b 11  0  # +--------
mknod /dev/scd1 b 11  1  # |
mknod /dev/scd2 b 11  2  # |
mknod /dev/scd3 b 11  3  # |
                         # |
mknod /dev/sda  b  8  0  # |
mknod /dev/sda1 b  8  1  # |
mknod /dev/sda2 b  8  2  # |
mknod /dev/sda3 b  8  3  # |
mknod /dev/sda4 b  8  4  # |
                         # |
mknod /dev/sdb  b  8 16  # |    <----
mknod /dev/sdb1 b  8 17  # |        Devices which could be or contain the
mknod /dev/sdb2 b  8 18  # |        boot medium...
mknod /dev/sdb3 b  8 19  # |
mknod /dev/sdb4 b  8 20  # |
                         # |
mknod /dev/sdc  b  8 32  # |
mknod /dev/sdc1 b  8 33  # |
mknod /dev/sdc2 b  8 34  # |
mknod /dev/sdc3 b  8 35  # |
mknod /dev/sdc4 b  8 36  # |
                         # |
mknod /dev/sdd  b  8 48  # |
mknod /dev/sdd1 b  8 49  # |
mknod /dev/sdd2 b  8 50  # |
mknod /dev/sdd3 b  8 51  # |
mknod /dev/sdd4 b  8 52  # +--------

# Create mount points for filesystems
mkdir -p /mnt/medium
mkdir -p /mnt/system
mkdir -p /mnt/rootfs

# Mount the /proc filesystem (enables filesystem detection for 'mount')
mkdir /proc
mount -t proc proc /proc

# Search for, and mount the boot medium
LABEL="$(cat /boot/id_label)"
for device in $(ls /dev); do
    [ "${device}" == "console" ] && continue
    [ "${device}" == "null"    ] && continue

    mount -o ro /dev/${device} /mnt/medium 2> /dev/null && \
    if [ "$(cat /mnt/medium/boot/${ARCH}/id_label)" != "${LABEL}" ]; then
        umount /mnt/medium
    else
        DEVICE="${device}"
        break
    fi
done

if [ "${DEVICE}" == "" ]; then
    echo "STOP: Boot medium not found."
    exec /bin/busybox sh
fi

# Mount the system image
mount -t squashfs -o ro,loop /mnt/medium/boot/${ARCH}/root.sfs /mnt/system || {
    if [ -r /mnt/medium/boot/${ARCH}/root.sfs ]; then
        echo "STOP: Unable to mount system image. The kernel probably lacks"
        echo "      SquashFS support. You may need to recompile it."
    else
        echo "STOP: Unable to mount system image. It seems to be missing."
    fi

    exec /bin/busybox sh
}

# Define where the new root filesystem will be
ROOT="/mnt/rootfs" # Also needed for /usr/share/live/sec_init.sh

# Select LiveCD mode
overlayMount # Might fall back to copyBindMount

# Move current mounts to directories accessible in the new root
cd /mnt
for dir in $(ls -1); do
    if [ "${dir}" != "rootfs" ]; then
        mkdir -p ${ROOT}/mnt/.boot/${dir}
        mount --move /mnt/${dir} ${ROOT}/mnt/.boot/${dir}
    fi
done
cd /

# If the boot medium is a CD, eject it on shutdown
[ "$(mount | grep iso9660)" != "" ] && \
cat > ${ROOT}/etc/rc.d/rc0.d/S98eject << EOF
#!/bin/sh

if [ -x /usr/bin/cdrecord ]; then
    /usr/bin/cdrecord -eject dev=/dev/${DEVICE} &> /dev/null

    echo -e "\033[31m"
    echo -en "Remove the boot medium, close the tray (if any), "
    echo -en "then press ENTER to power off."
    echo -e "\033[0m"

    read
fi
EOF
chmod +x ${ROOT}/etc/rc.d/rc0.d/S98eject

# Run secondary initialization (if the system provides it)
if [ -x ${ROOT}/usr/share/live/sec_init.sh ]; then
    . ${ROOT}/usr/share/live/sec_init.sh
fi

# Clean up
umount /proc

# Switch to the new root and launch INIT!
exec switch_root -c /dev/console ${ROOT} /sbin/init

EndOfFile

Identifying the boot medium

The initialisation script must have some way of identifying the medium from which it has been started.

The following command saves a 512-bit random number, to be used as a unique identifier. It will later be copied to the initramfs created after this and to the ISO file system itself:

dd if=/dev/random of=id_label bs=1 count=64

The initramfs

The initialisation script relies on certain system commands to be available during boot. However, because the root file system is not available yet (the very purpose of init.sh is to set it up), these commands must be available in advance.

Luckily, Linux supports the use of an initial RAM disk or initramfs. With this, we can provide the kernel with a file which has a minimal set of utilities (and init.sh) embedded. The kernel will automatically copy the contents of the file to a temporary root filesystem, and then turn over control to /init (init.sh).

In stead of copying the required utilities from the system image, many Live CD's opt to use a statically linked BusyBox in stead. This makes the initramfs not dependent on any libraries in the system image. And because busybox is only a single binary file, it makes the creation of the initial file system easy:

mkdir -pv mnt_init/{bin,boot}
cp -v id_label mnt_init/boot
cp -v /bin/busybox mnt_init/bin
mv init.sh mnt_init/init
sed -i "s/<ARCH>/$(uname -m)/g" mnt_init/init
chmod +x mnt_init/init

Once populated, compress the initial file system. The Linux kernel expects an cpio file, optionally compressed with gzip:

pushd mnt_init
find . | /bin/busybox cpio -o -H newc -F ../initramfs.cpio
popd
gzip -9 initramfs.cpio
rm -rf mnt_init

Mount, verify and compress the system image

In order to compress the system image, it must be mounted (read-only) first:

mkdir mnt
mount -o loop,ro rootfs-$(uname -m).img mnt

This next block performs some verification on the system image to ensure it can be used as a root file system. If you are executing the commands on this page manually, you should skip this block:

FAIL="0"

if [ ! -r mnt/usr/bin/$(uname -m)-*linux-gnu-gcc ]; then
    echo -n "ERROR: Unable to verify image architecture; Your system image does"
    echo " not contain the file '/usr/bin/$(uname -m)-*linux-gnu-gcc'"
    FAIL="1"
fi

if [ ! -d mnt/etc ]; then
    echo "ERROR: Your system image does not contain the directory '/etc'"
    FAIL="1"
fi

if [ ! -x mnt/sbin/init ]; then
    echo "ERROR: Your system image does not contain the executable '/sbin/init'"
    FAIL="1"
fi

if [ ! -e mnt/dev/console ]; then
    echo "ERROR: Your system image does not contain the device '/dev/console'"
    FAIL="1"
fi

if [ "${FAIL}" == "1" ]; then
    return 1
fi

Then, proceed to compress the system image:

mksquashfs mnt root.sfs -comp xz

Create the ISO image

Now that all components are ready, it's time to package it all up in a single ISO file.

First, copy the boot loader and Linux kernel to the CD file system:

mkdir -p live/boot/{isolinux,$(uname -m)}
cp -v /usr/share/syslinux/isolinux.bin live/boot/isolinux
cp -v /usr/share/syslinux/ldlinux.c32 live/boot/isolinux

cp -v mnt/boot/vmlinuz-* live/boot/$(uname -m)/vmlinuz
cp -v mnt/boot/config-*  live/boot/$(uname -m)/config

Copy all files from the root file system image that have been prepared to customize the Live CD:

if [ -e "mnt/usr/share/live/medium" ]; then
    cp -vR mnt/usr/share/live/medium/* live
fi

Now, unmount the root file system image as it is no longer needed:

umount mnt
rm -rf mnt

Unless a customised configuration has been copied a moment ago, use the boot loader configuration created when we started:

if [ ! -e "live/boot/isolinux/isolinux.cfg" ]; then
    cp -v /usr/share/syslinux/menu.c32    live/boot/isolinux
    cp -v /usr/share/syslinux/libutil.c32 live/boot/isolinux
    mv -v isolinux.cfg                    live/boot/isolinux
else
    rm -rf isolinux.cfg
fi

Move the various components to the CD file system:

mv -v root.sfs live/boot/$(uname -m)
mv -v id_label live/boot/$(uname -m)
mv -v initramfs.cpio.gz live/boot/$(uname -m)/initram.fs

Write out the final ISO:

xorrisofs -o system-$(uname -m).iso     \
          -b boot/isolinux/isolinux.bin \
          -c boot.cat                   \
          -no-emul-boot                 \
          -boot-load-size 4             \
          -boot-info-table              \
          -joliet -l -R                 \
          live
rm -rf live

Move the ISO image to LFScript's newpackages directory (you can skip this if you are running these commands manually):

mkdir -p /sources/buildmgr/newpackages
mv -v system-$(uname -m).iso /sources/buildmgr/newpackages

Done!