Friday, April 2, 2010
Ubuntu "Live / Install CD" from existing system
A. Prepare the environment:
1. Set the variables:
B. Copy existing installation into the new filesystem:
C. Chroot into the new system and modify it:
1. Chroot into the copied system after mounting proc and dev:
3. (Optional) If you want your live cd to have an installer, install the Ubuntu installer:
5. Update the modules.dep and initramfs:
6. Remove non system users: (script)
7. Delete these files: (script)
8. Cleanup the chroot environment:
10. Create needed log files in /var/log: (script)
D. Prepare The CD directory tree:
1. Copy the kernel, the updated initrd and memtest prepared in the chroot:
Note: This step is only needed if you installed the Ubuntu installer ubiquity. This step generates two files (filesystem.manifest & filesystem.manifest-desktop).
3. Unmount bind mounted dirs:
5. Make Grub the bootloader for the CD
Copy grub file:
E. Build the CD/DVD
1. Make the ISO file:
1. Set the variables:
export WORK=~/work
export CD=~/cd
export FORMAT=squashfs
export FS_DIR=casper
2. Create the CD and the WORK directory structure:sudo mkdir -p ${CD}/{${FS_DIR},boot/grub} ${WORK}/rootfs
3. Install packages on the source system:sudo apt-get update
sudo apt-get install mkisofs grub squashfs-tools linux-headers-$(uname -r)
B. Copy existing installation into the new filesystem:
sudo rsync -av --one-file-system --exclude=/proc/* --exclude=/dev/*\ --exclude=/sys/* --exclude=/tmp/* --exclude=/home/*\ --exclude=/lost+found / ${WORK}/rootfs
If you have a separate boot partition you will have to copy it using the following command:sudo cp -av /boot/* ${WORK}/rootfs/boot
Copy custom settings:cp -r /home/yourusername/.config /home/yourusername/.xscreensaver /root/work/rootfs/etc/skel/
C. Chroot into the new system and modify it:
1. Chroot into the copied system after mounting proc and dev:
sudo mount -o bind /dev/ ${WORK}/rootfs/dev
sudo mount -t proc proc ${WORK}/rootfs/proc
sudo chroot ${WORK}/rootfs /bin/bash
2. Now you are within the chroot environment, type the following commands: LANG=
apt-get update
apt-get install casper discover1 xresprobe
"casper" contain the live scripts. "discover1" & "xresprobe" are used for autodetecting hardware at startup. 3. (Optional) If you want your live cd to have an installer, install the Ubuntu installer:
apt-get install ubiquity
4. (Optional) Install any packages you want to be in the CD with "apt-get".5. Update the modules.dep and initramfs:
depmod -a $(uname -r)
update-initramfs -u -k $(uname -r)
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 scripts installed with casper. 6. Remove non system users: (script)
for i in `cat /etc/passwd | awk -F":" '{print $1}'`
do
uid=`cat /etc/passwd | grep "^${i}:" | awk -F":" '{print $3}'`
[ "$uid" -gt "999" -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. 7. Delete these files: (script)
for i in "/etc/hosts /etc/hostname /etc/resolv.conf /etc/timezone /etc/fstab /etc/mtab /etc/shadow /etc/shadow- /etc/gshadow /etc/gshadow- /etc/gdm/gdm-cdd.conf /etc/X11/xorg.conf /boot/grub/menu.lst /boot/grub/device.map"
do
rm $i
done 2>/dev/null
These files are not needed in the CD/DVD. some of them are could interfer with the CD/DVD boot process. (e.g. shadow and gdm.conf-custom can interfere with autologin).8. Cleanup the chroot environment:
apt-get clean
find /var/run /var/log /var/mail /var/spool /var/lock /var/backups /var/tmp -type f -exec rm {} \;
rm -r /boot/*.bak /tmp/* /home/* /root/* 2>/dev/null
9. If you are using GDM recreate it's config file: [ -f "/etc/gdm/factory-gdm.conf" ] && cp -f /etc/gdm/factory-gdm.conf /etc/gdm/gdm.conf 2>/dev/null
A customized /etc/gdm/gdm.conf can interfere with the live CD/DVD autologin. 10. Create needed log files in /var/log: (script)
for i in dpkg.log lastlog mail.log syslog auth.log daemon.log faillog lpr.log mail.warn user.log boot debug mail.err messages wtmp bootstrap.log dmesg kern.log mail.info
do
touch /var/log/${i}
done
Most of these files are log files that have been cleaned in step 7. We created an empty files in their place to prevent the system from complaining at boot.rm /usr/lib/ubiquity/apt-setup/generators/50cdrom
(Optional) If you want the Installer Icon removed from the final system's Desktop:rm /usr/share/applications/ubiquity-gtkui.desktop
11. Exit chroot exit
D. Prepare The CD directory tree:
1. Copy the kernel, the updated initrd and memtest prepared in the chroot:
sudo cp -vp ${WORK}/rootfs/boot/vmlinuz-$(uname -r) ${CD}/boot/vmlinuz
sudo cp -vp ${WORK}/rootfs/boot/initrd.img-$(uname -r) ${CD}/boot/initrd.gz
sudo cp -vp ${WORK}/rootfs/boot/memtest86+.bin ${CD}/boot
2. Generate manifest:Note: This step is only needed if you installed the Ubuntu installer ubiquity. This step generates two files (filesystem.manifest & filesystem.manifest-desktop).
sudo chroot ${WORK}/rootfs dpkg-query -W --showformat='${Package} ${Version}\n' | sudo tee ${CD}/${FS_DIR}/filesystem.manifest
sudo cp -v ${CD}/${FS_DIR}/filesystem.manifest{,-desktop}
REMOVE='ubiquity casper user-setup discover1 xresprobe os-prober libdebian-installer4'
(script)for i in $REMOVE
do
sudo sed -i "/${i}/d" ${CD}/${FS_DIR}/filesystem.manifest-desktop
done
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 $REMOVE3. Unmount bind mounted dirs:
sudo umount ${WORK}/rootfs/dev ${WORK}/rootfs/proc
4. Convert the directory tree into a squashfs: sudo mksquashfs ${WORK}/rootfs ${CD}/${FS_DIR}/filesystem.${FORMAT}
Note: Make sure the resulting file size can fit into your live media.5. Make Grub the bootloader for the CD
Copy grub file:
sudo find /boot /usr/lib/grub/ -iname 'stage2_eltorito' -exec cp -v {} ${CD}/boot/grub \;
Make the menu.lstsudo nano ${CD}/boot/grub/menu.lst
Adjust and copy the following text into it and save it:# By default, boot the first entry.
default 0
# Boot automatically after 30 secs.
timeout 30
title System (Graphical Mode)
kernel /boot/vmlinuz BOOT=casper boot=casper nopersistent rw quiet splash
initrd /boot/initrd.gz
title System (Safe Graphical Mode)
kernel /boot/vmlinuz BOOT=casper boot=casper xforcevesa rw quiet splash
initrd /boot/initrd.gz
title Linux (Text Mode CLI)
kernel /boot/vmlinuz BOOT=casper boot=casper nopersistent textonly rw quiet
initrd /boot/initrd.gz
title Memory Test
kernel /boot/memtest86+.bin
title Boot the First Hard Disk
root (hd0)
chainloader +1
6. Calculate MD5:cd $CD && find . -type f -print0 | xargs -0 sudo md5sum | sudo tee ${CD}/md5sum.txt
E. Build the CD/DVD
1. Make the ISO file:
sudo mkisofs -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -V "MY_LIVE_CD" -cache-inodes -r -J -l -o ~/my-live-install-cd.iso $CD
2. Burn your new created ISO!