Ubuntu – How to add custom scripts while making a LiveCD customization


I want to add custom scripts while making a customization of an Ubuntu LiveCD.

Best Answer

  • My stock answer for this question is this article over on Turnkeylinux's blog. It's an Ubuntu derived distro and they have a physical to live set of scripts in their repo that works pretty well. Bonus, you can install their scripts on Ubuntu mainline.

    There's also remastersys and a couple others, but I found the turnkey scripts to be really straightforward for my needs.

    The article:
    Converting a virtual disk image: VDI or VMDK to an ISO you can distribute By Alon Swartz - 58 comments | Latest by unit

    Why would anyone in their right mind want to convert a VM into an ISO?

    Good question, the answer for Conor Fox (who was the inspiration for this post - thanks Conor!) was to distribute his customized TurnKey PostgreSQL image so others could use it.

    Distributing an ISO as opposed to a VM image allows it to be installed on any virtualization platform, as well as on bare metal, with the added bonus of running live.

    I suppose that's a good enough reason, so lets get to it.

    Convert VM disk to raw image and mount it

    First we need to get qemu-img, a tool bundled with qemu (KVM's virtualization backend) to convert the VM disk to a raw image, and TKLPatch, the TurnKey customization mechanism to package the ISO.

    If you are not using a TurnKey installation, see the TKLPatch installation notes.

    apt-get install qemu
    apt-get install tklpatch

    I'll show how to convert a VMWare VMDK image into raw disk format. If you are using a different virtualization platform such as Virtualbox, see this post on converting a VDI to a raw image.

    qemu-img convert -f vmdk turnkey-core.vmdk -O raw turnkey-core.raw

    Next, mount the raw disk as a loopback device.

    mkdir turnkey-core.mount mount -o loop turnkey-core.raw turnkey-core.mount

    GOTCHA 1: If your VM has partitions, it's a little tricker. You'll need to setup the loop device, partition mappings and finally mount the rootfs partition. You will need kpartx to setup the mappings.

    loopdev=$(losetup -s -f turnkey-core.raw)
    apt-get install kpartx
    kpartx -a $loopdev
    # p1 refers to the first partition (rootfs)
    mkdir turnkey-core.mount
    mount /dev/mapper/$(basename $loopdev)p1 turnkey-core.mount

    Extract root filesystem and tweak for ISO configuration

    Now, make a copy of the root filesystem and unmount the loopback.

    mkdir turnkey-core.rootfs
    rsync -a -t -r -S -I turnkey-core.mount/ turnkey-core.rootfs
    umount -d turnkey-core.mount

    If your VM had partitions (GOTCHA 1):

    kpartx -d $loopdev losetup -d $loopdev

    Because the VM is an installed system as opposed to the ISO, the file system table needs to be updated.

    aufs / aufs rw 0 0
    tmpfs /tmp tmpfs nosuid,nodev 0 0

    GOTCHA 2: If your VM uses a kernel optimized for virtualization (like the one included in the TurnKey VM builds), you need to replace it with a generic kernel, and also remove vmware-tools if installed.

    tklpatch-chroot turnkey-core.rootfs
    # inside the chroot
    apt-get update
    apt-get install linux-image-generic
    dpkg --purge $(dpkg-query --showformat='${Package}\n' -W 'vmware-tools*')
    dpkg --purge $(dpkg-query --showformat='${Package}\n' -W '*-virtual')

    Generate the ISO

    Finally, prepare the cdroot and generate the ISO.

    tklpatch-prepare-cdroot turnkey-core.rootfs/
    tklpatch-geniso turnkey-core.cdroot/

    Thats it!

    Bonus: By default the ISO will boot automatically. If you want to include the TurnKey bootsplash and bootmenu, extract the cdroot from a TurnKey ISO and tell tklpatch-prepare-cdroot to use it as a template.

    tklpatch-extractiso turnkey-core.iso
    tklpatch-prepare-cdroot turnkey-core.rootfs/ turnkey-core.cdroot/
    tklpatch-geniso turnkey-core.cdroot/
  • Related Question