Discussion of this wiki can be found here

Introduction

This HOWTO is about making a live CD/DVD from the main system on your hard drive.

This might be desired if you have customized your system and want to have it on CD.

Background on live CD/DVD

Note: This section is a clarification of how live CD works. You don't have to read it. You can skip it if you want.

What are these differences?

initramfs is virtual filesystem. It is a compressed cpio (cpio is an archive format similar to tar) archive that contains a minimal shell, kernel modules necessary for mounting the root filesystem and number of scripts that perform some tasks at boot time. The most important of these scripts is a script called init located at the root of the initramfs.

How does initramfs work?

How is initramfs created?

The live CD/DVD structure

(CD ROOT) 
|-------+casper 
|       |-------filesystem.${FORMAT}     
|       |-------filesystem.manifest 
|       |-------filesystem.manifest-desktop 
|       |-------vmlinuz |       
|-------initrd.img 
| 
|-------+boot 
|       |--------+grub 
|       |        
|       |        |--------grub.cfg
|       |
|-------memtest86+ 
| 
|--------md5sum.txt 

Outline of the steps

Conventions used in this HOWTO

Preparing the environment

  1. Set some variables

export WORK=~/work
export CD=~/cd
export FORMAT=squashfs
export FS_DIR=casper

sudo mkdir -p ${CD}/{${FS_DIR},boot/grub} ${WORK}/rootfs
  1. Install some packages on your current system:

sudo apt-get update

sudo apt-get install grub2 xorriso squashfs-tools qemu  (qemu optional)

qemu is (optional). It is only needed for testing the cd before burning it. It can be substituted with any other virtualization software like virtualbox.

Copy your installation into the new filesystem

sudo rsync -av --one-file-system --exclude=/proc/* --exclude=/dev/* \
--exclude=/sys/* --exclude=/tmp/* --exclude=/home/* --exclude=/lost+found \
--exclude=/var/tmp/* --exclude=/boot/grub/* --exclude=/root/* \
--exclude=/var/mail/* --exclude=/var/spool/* --exclude=/media/* \
--exclude=/etc/fstab --exclude=/etc/mtab --exclude=/etc/hosts \
--exclude=/etc/timezone --exclude=/etc/shadow* --exclude=/etc/gshadow* \
--exclude=/etc/X11/xorg.conf* --exclude=/etc/gdm/custom.conf \
--exclude=/etc/lightdm/lightdm.conf --exclude=${WORK}/rootfs / ${WORK}/rootfs

sudo cp -av /boot/* ${WORK}/rootfs/boot

(Optional) Copy settings in your home dir:

(optional)

CONFIG='.config .bashrc'

Now, Copy the CONFIG files using the following command: (optional)

cd ~ && for i in $CONFIG
do
sudo cp -rpv --parents $i ${WORK}/rootfs/etc/skel
done

Chroot into the new system and modify it

  1. Chroot into the copied system after mounting proc and dev:

sudo mount  --bind /dev/ ${WORK}/rootfs/dev

sudo mount -t proc proc ${WORK}/rootfs/proc

sudo mount -t sysfs sysfs ${WORK}/rootfs/sys

sudo mount -o bind /run ${WORK}/rootfs/run

sudo chroot ${WORK}/rootfs /bin/bash

N.B: All commands done within a chroot will be marked by (chroot).

(chroot)

LANG=
  1. Install Packages Essential for live CD:

(chroot)

apt-get update

(chroot)

apt-get install casper lupin-casper

casper contains the live scripts.

3. (Optional) If you want your live cd to have an installer, install the Ubuntu installer: (chroot)

apt-get install ubiquity ubiquity-frontend-gtk

Note: People using kde replace the previous command with: (chroot)

apt-get install ubiquity ubiquity-frontend-kde 

Credit for this note goe's to Fragadelic author of remastersys.

(Optional Step)Install any packages you want to be in the CD. Some of the following packages are useful in emergency situations:

sudo apt-get install gparted ms-sys testdisk wipe partimage xfsprogs reiserfsprogs jfsutils ntfs-3g ntfsprogs dosfstools mtools

gparted: patitioning tool. It is automatically installed as a dependecy of ubiquity.

(chroot)

depmod -a $(uname -r)

(chroot)

update-initramfs -u -k $(uname -r)

As already metioned above, the initramfs is reponsible for much of the preparation required at the boot time of the CD/DVD. The updated initramfs now contain the live scirpts installed with casper.

  1. Remove non system users

(chroot)

for i in `cat /etc/passwd | awk -F":" '{print $1}'`
do
        uid=`cat /etc/passwd | grep "^${i}:" | awk -F":" '{print $3}'`
        [ "$uid" -gt "998" -a  "$uid" -ne "65534"  ] && userdel --force ${i} 2> /dev/null
done

Non-system users are users created by you that have user id more than 999.

(chroot)

apt-get clean

(chroot)

find /var/log -regex '.*?[0-9].*?' -exec rm -v {} \;

(chroot)

find /var/log -type f | while read file
do
        cat /dev/null | tee $file
done

(chroot)

rm /etc/resolv.conf /etc/hostname
  1. Exit chroot

(chroot)

exit

Prepare The CD directory tree

  1. Copy the kernel, the updated initrd and memtest prepared in the chroot:

export kversion=`cd ${WORK}/rootfs/boot && ls -1 vmlinuz-* | tail -1 | sed 's@vmlinuz-@@'`

sudo cp -vp ${WORK}/rootfs/boot/vmlinuz-${kversion} ${CD}/${FS_DIR}/vmlinuz

sudo cp -vp ${WORK}/rootfs/boot/initrd.img-${kversion} ${CD}/${FS_DIR}/initrd.img

sudo cp -vp ${WORK}/rootfs/boot/memtest86+.bin ${CD}/boot

2. (optinal)Generate manifest:

(optinal)

sudo chroot ${WORK}/rootfs dpkg-query -W --showformat='${Package} ${Version}\n' | sudo tee ${CD}/${FS_DIR}/filesystem.manifest

(optinal)

sudo cp -v ${CD}/${FS_DIR}/filesystem.manifest{,-desktop}

(optinal)

REMOVE='ubiquity casper user-setup os-prober libdebian-installer4'

(optinal)

for i in $REMOVE
do
        sudo sed -i "/${i}/d" ${CD}/${FS_DIR}/filesystem.manifest-desktop
done

(optinal) These two files are used by the ubiquity installer when installing to harddisk. These two files are just lists of packages. Ubiquity compares these two files and removes packages unique to filesystem.manifest. This way when installing to harddisk, packages like casper which is only useful in a live CD/DVD are removed. These packages that will be removed at install are defined in the variable $REMOVE

  1. Unmount bind mounted dirs:

sudo umount ${WORK}/rootfs/proc

sudo umount ${WORK}/rootfs/sys

sudo umount ${WORK}/rootfs/dev
  1. Convert the directory tree into a squashfs:

sudo mksquashfs ${WORK}/rootfs ${CD}/${FS_DIR}/filesystem.${FORMAT} -noappend

Note: Make sure the resulting file size can fit into your live media.

  1. Make filesystem.size

echo -n $(sudo du -s --block-size=1 ${WORK}/rootfs | tail -1 | awk '{print $1}') | sudo tee ${CD}/${FS_DIR}/filesystem.size
  1. Calculate MD5

find ${CD} -type f -print0 | xargs -0 md5sum | sed "s@${CD}@.@" | grep -v md5sum.txt | sudo tee -a ${CD}/md5sum.txt
  1. Make Grub the bootloader of the CD Make the grub.cfg

sudo gedit ${CD}/boot/grub/grub.cfg

set default="0"
set timeout=10

menuentry "Ubuntu GUI" {
linux /casper/vmlinuz boot=casper quiet splash
initrd /casper/initrd.img
}

menuentry "Ubuntu in safe mode" {
linux /casper/vmlinuz boot=casper xforcevesa quiet splash
initrd /casper/initrd.img
}

menuentry "Ubuntu CLI" {
linux /casper/vmlinuz boot=casper textonly quiet splash
initrd /casper/initrd.img
}

menuentry "Ubuntu GUI persistent mode" {
linux /casper/vmlinuz boot=casper persistent quiet splash
initrd /casper/initrd.img
}

menuentry "Ubuntu GUI from RAM" {
linux /casper/vmlinuz boot=casper toram quiet splash
initrd /casper/initrd.img
}

menuentry "Check Disk for Defects" {
linux /casper/vmlinuz boot=casper integrity-check quiet splash
initrd /casper/initrd.img
}

menuentry "Memory Test" {
linux16 /boot/memtest86+.bin
}

menuentry "Boot from the first hard disk" {
set root=(hd0)
chainloader +1
}

Build the CD/DVD

  1. Make the ISO file

sudo grub-mkrescue -o ~/live-cd.iso ${CD}
  1. Test the CD Test using qemu emulator

qemu -cdrom ~/live-cd.iso -boot d

Or use any other virtualization program you like.

Update: As noted by az while testing the iso with qemu sometimes it drops you to an initramfs shell because of a problem with qemu. This behaviour has been confirmed by other users. In this case it is advisable to retest the iso with another virtualization software like virtualbox or to copy the iso to flash disk and test directly on your pc (See Appendix 2.).

3. (Optional) Clean our workspace

[ -d "$WORK" ] && rm -r $WORK $CD

Final Notes

Building the live media form scratch using debootstrap

Appendix 2. How to Make bootable USB flash

sudo blkid

sudo mount -t vfat /dev/by-uuid/<insert uuid here> /mnt

Install grub to the flash disk:

sudo grub-install --no-floppy --force --root-directory=/mnt /dev/sdc

replace the /dev/sdc with the device name of your flash. Note that we are using the device name of the flash itself not the partition in the flash (e.g. /dev/sdc not /dev/sdc1)

cp -v ~/live-cd.iso /mnt

sudo gedit /mnt/boot/grub/grub.cfg

and add the following entry

set default="0"
set timeout=10

insmod ntfs
search --no-floppy --fs-uuid <insert the UUID> --set=usb
set iso_path=/live-cd.iso
loopback loop (${usb})${iso_path}
set root=(loop)
set bootopts="boot=casper iso-scan/filename=${iso_path} noprompt"

menuentry "Boot ISO from HDD/USB" {
linux (loop)/casper/vmlinuz $bootopts
initrd (loop)/casper/initrd.img
}

search --no-floppy -l insert the LABEL --set=usb

Originally posted The Ubuntu Forums (ubuntuforums.org)

MakeALiveCD/DVD/BootableFlashFromHarddiskInstall (last edited 2015-07-12 20:34:35 by port-87-234-245-27)