NAME: Building Your Own Bootable CD In Linux Shanker Balan http://shankerbalan.com/ Changelog: * Fri Oct 19 2001 - Added info about mounting CDROM * Fri Oct 12 2001 - Initial roll-out SYNOPSIS: This is a tutorial for building your own (BYO) bootable CD with Linux on it. DESCRIPTION: A bootable CD with a running usable copy of Linux can help in a lot of situations like the following: - Rescue/salvage thrashed Linux partitions - Build boot CD for specific tasks like X-Terminal - Add more here... (Fix Me) REQUIREMENTS: CD Re-Writers Blank CD-RW medias ( use fast ones ) VMWare ( for easy testing ) HOW IT WORKS: 1) Create a root file system with the required file - kernel, kernel modules, init scripts and other binaries which you want to use 2) Compress the root file system so that it can be copied to the boot floppy and be used as a Ram-Disk Image. 3) We create a 2.88MB boot floppy with the required files - kernel, ram-disk 4) Make the floppy bootable 5) Create a ISO image using the bootable floppy as boot image 6) Write the bootable cd image onto a CD-R PROCEDURE: Step 1: Create the root file system floppy Image We are not using a "real" floppy for our work, we use dd to create a 4.44MB file, format it, loopback mount it, populate it with files and binaries which is required to boot a Linux system and finally compress it. For this tutorial, lets assume that we want to make a bootable CD for use as an X-Terminal. To make things simpler for populating the root file system, use one of the many available floppy distributions as a template. Here, i am using the 2diskXwin 2 floppy distribution as a base. Create a root file system with 4.44MB of space, this is adequate for combining the contents of both the 1.44Mb disks in the 2diskXwin mini Linux set. root# dd if=/dev/zero of=/Image bs=1k count=4440 This will create a "Image" of size 4.44MB populated with "zeros". Next, we create an Ext2 filesystem on it root# mke2fs -m0 0 -i 2000 Image The "-m0" and "-i 2000" make sure that no space is reserved for root and there are lots of inodes (2000) to go around. Loopback mount it root# mkdir /mnt/rootfs root# mount -o loop -t ext2 Image /mnt/rootfs The image is mounted under /mnt/rootfs. Now populate rootfs with the files you want in your Linux "distro". Since I am using 2diskXwin as a template, i am blindly copying the contents of both the floppies into /mnt/rootfs. Once the files are copied, unmount rootfs and compress it root# umount /mnt/rootfs root# dd if=Image bs=1k | gzip -v9 > Image.gz Root filesystems compress very well. In my tests, the compression ratio was 60%. This brings the size of Image from 4.44MB to around 2.66MB. This is to used as Ram-Disk. Step 2: Create the bootable floppy Next, we create a 2.88MB floppy image, create an MS-DOS filesystem on it, copy the kernel, Image.gz and the syslinux config file to it and make it bootable. The root image has cannot be more than 2.88MB as the BIOS might fail to work with larger floppy images. root# dd if=/dev/zero of=root.bin bs=1k count=2880 root# mkfs.msdos root.bin root# mkdir /mnt/root.bin root# mount -t vfat root.bin /mnt/root.bin Copy the kernel and Image.gz to /mnt/root.bin. I used the boot floppy kernel from the RedHat kernel-BOOT package for this. Size matters, so make sure that the kernel you want to use is as small as possible. "syslinux" is used to make the root.bin image bootable. Create a file called "syslinux.cfg" in the root of your floppy image. root# cd /mnt/root.bin root# vim syslinux.cfg Put the following lines into it ---- syslinux.cfg ---- default linux prompt 1 timeout 600 label linux kernel vmlinuz append initrd=Image.gz root=/dev/ram0 ---- syslinux.cfg ---- The Ram Disk to use is Image.gz and the root is set to /dev/ram0. Finally, we umount root.bin and make it bootable using syslinux root# umount /mnt/root.bin root# syslinux root.bin syslinux will read the syslinux.cfg inside root.bin and make the image bootable. Step3: Creating the bootable ISO image root# mkdir cdrom root# mkdir cdrom/boot root# cp boot.bin cdrom/boot root# touch cdrom/boot/boot.catalog root# cd cdrom root# mkisofs -b boot/boot.img -c boot/boot.catalog -o bootcd.iso . This created a bootcd.iso in the cdrom/ directory using boot/boot.img as the boot image. Step4: Writing bootcd.iso Insert a CD-RW into the CD-Rewrite and burn the image root# cdrecord -blank=fast -v bootcd.iso Now fire up your VMWare session and test the bootcd with 2diskXwin which you have just created. If you are missing files/directories, add them to "Image" and redo the process again. Please note that the compression ratio falls if there is increased file activity in the root filesystem Image. MOUNTING THE CDROM: Since the size of the ramdisk/boot floppy is limited, the CDROM can be mounted and data be read off the CD media. This simple script can be used to detect the CDROM device and mount it. Dump this somewhere in rc.sysinit. # Hack to determine the cdrom device CDROM=`dmesg|grep 'ATAPI.*CD-ROM'|awk -F: '{print $1}'` echo "Creating link /dev/cdrom pointing to /dev/$CDROM: " ln -sf /dev/$CDROM /dev/cdrom echo "Mounting CDROM: " mount -t iso9660 -r /dev/$CDROM /cdrom /usr is a good choice for dumping on the CDROM. Create a symlink in the root filesystem for /usr pointing to /cdrom/usr. Dont forget to add the binary directories (/usr/bin, /usr/sbin, /usr/X11R6/bin etc) to your syatem PATH. SEE ALSO: 1. The Linux Bootdisk HOWTO http://www.linuxdoc.org/HOWTO/Bootdisk-HOWTO/index.html 2. CD-Writing HOWTO http://www.linuxdoc.org/HOWTO/CD-Writing-HOWTO.html TODO: The procedure described here has an inherent limitation - you are restricted to 2.88MB as your root filesystem size. To make full use of the CDROM, we need to place the root filesystem on the CD and loopback mount it as the root filesystem. (FIXED)