1 AUTHOR: Mike Hernandez <mike at culmination dot org>
5 LICENSE: GNU Free Documentation License Version 1.2
7 SYNOPSIS: Beginners Guide To Creating A Live CD With LFS 6.0
9 ALTERNATE LOCATION: For most recent edition of this hint you can check
10 http://www.culmination.org/Mike/2.6-udev-nptl-bootcd.txt
13 This hint is a detailed description of how I made a live cd from a system
14 built with LFS 6.0. The reader should be able to follow the hint to create
15 a live cd of his or her own. It was adapted from the "Easy" hint to work with
16 LFS 6.0. It is my intention to make it as easy as possible for a first time
20 1. A working LFS System (and cdrtools if you want to burn the cd, of course)
21 2. An LFS system built for the purpose of creating a live cd (see below)
22 3. CD Writer + Media (but you knew that, I bet)
23 4. Syslinux (http://freshmeat.net/projects/syslinux/)
27 Before we get started here, let me make some things clear.
31 ------------------------------
32 | What it is and what it ain't |
33 ------------------------------
39 This is a hint for creating a simple live cd. To be more clear:
40 Upon booting from the cd, a user will be presented with a linux prompt.
42 If you build it from an LFS system (no BLFS stuff) then the cd will be
43 helpful for repairing an LFS system that got messed up (menu.lst typos
44 /etc/fstab weirdness, things like that).
46 Creating live cd's can be fun, and knowing how to do so opens up the door
47 for many possibilities. This hint is a good starting point for that
48 "ultimate rescue cd" idea you've had or maybe can help you answer the
49 "I wonder if Linux will run on my _____" question =)
55 As I said above, this will help you make a simple livecd. The user will be
56 presented with a linux prompt and not much else beyond that unless you add
57 the functionality yourself.
59 This hint does not describe how to put tons of software on the cd as you
60 might find on knoppix or the official LFS boot CD.
62 This hint does not describe how to create an install program to copy the
63 contents of the cd onto a hard drive.
65 Also, you will not find any cross-compiling info here, so if you want your
66 CD to boot on a completely different architecture than your own, you have
67 to do that research yourself.
69 --------------------------------------------------
70 | Why it is what it is, and it ain't what it ain't |
71 --------------------------------------------------
73 This hint is designed to help a first time live cd builder. I tried to keep
74 it as simple as possible, so that it wouldn't require too much advanced
75 knowledge of LFS to complete (the hint it's based on is called the "Easy"
76 boot CD hint, after all.) Of course you need to know a bit, but expert
77 knowledge is not a requirement. Creating an install CD, or using
78 filesystems such as squashfs, may require more info than a beginner might
79 have, and attempting to include all of it might make this hint more
80 confusing than helpful.
86 This hint, like the hint it is based on, requires that the reader have two
87 (yes 2) systems built. One system is your usual working system, which you
88 will use to burn the cd when you are done. The other is the system created
89 specifically for the purpose of putting on the live CD. If you just want
90 the CD to boot on the machine you built it on, then making a copy of your
91 LFS system to another partition is fine.
93 You might be wondering why you need two systems to create a live CD. The
94 reason is actually quite simple. If you are like me, you optimized your LFS
95 system to suit your pc (or in my case, laptop). I have a Pentium 4 system,
96 so all of the programs which can handle optimization have been built with:
98 CFLAGS='-march=pentium4'
100 So what's the issue? It's that I want my live CD to run on just about
101 anything that can boot a CD! The programs on my current system won't run on
102 any machine that isn't a pentium 4. I actually use many more optimizations
103 when I build, but here I mention the architecture because it brings up an
106 ----------------------------------------------------------
107 | Know in advance what hardware you want the CD to run on! |
108 ----------------------------------------------------------
110 Before you start you should also decide what you want your live CD to do.
111 You may want to consider optimizing everything in your live CD system for
112 size, or for a specific type of machine, or not to optimize at all.
113 Which is best? That's up to you.
115 Another reason you want to have a separate system is because in order to
116 create the CD we make some directories and move very important files
117 around. In the event that something goes wrong, having at least your
118 initial working system (if not a backup of the live CD system) can be a
121 ----------------------------
122 | Setting up the environment |
123 ----------------------------
125 To make things easier, you should set the LIVECD variable to point to the
126 mount point of the system you are going to use for the CD:
128 export LIVECD=/mnt/livecd
130 You also might want to set CDDEV as well (actually, you should):
132 export CDDEV=/dev/your-drive+partition
134 Set the ISODIR to the location where you would like to keep the image:
136 export ISODIR=/where/you/have/space
138 --------------------------------
139 | Configuring the live CD system |
140 --------------------------------
142 Now that the environment is all set up, it's time to do some final
143 configuration of the live CD system. This includes making sure the kernel
144 is how you want it, and maybe adding some other programs if you so desire.
146 If you are absolutely sure that the kernel for your CD system will work for
147 the system you want the CD to run on, and you don't need to install any
148 other software for your CD, then you can skip to the section called
149 "Moving around the furniture".
151 Before we can configure and install a new kernel for the live CD, we have
152 to chroot into the system:
154 1. Mount your live CD system:
159 2. As per the directions of the LFS-6.0 book,
160 mount the virtual file systems prior to entering the chroot:
162 mount -t proc proc $LIVECD/proc
163 mount -t sysfs sysfs $LIVECD/sys
164 mount -f -t ramfs ramfs $LIVECD/dev
165 mount -f -t tmpfs tmpfs $LIVECD/dev/shm
166 mount -f -t devpts -o gid=4,mode=620 devpts $LIVECD/dev/pts
168 3. chroot into the live CD system with the command given at the end of
169 chapter 6. (NOT THE CHROOT COMMAND AT THE BEGINNING!!!):
171 chroot $LIVECD /usr/bin/env -i \
172 HOME=/root TERM=$TERM PS1='\u:\w\$ ' \
173 PATH=/bin:/usr/bin:/sbin:/usr/sbin \
176 4. mount ramfs and populate /dev
178 mount -n -t ramfs none /dev
181 5. Create essential symlinks and directories not created by udev:
183 ln -s /proc/self/fd /dev/fd
184 ln -s /proc/self/fd/0 /dev/stdin
185 ln -s /proc/self/fd/1 /dev/stdout
186 ln -s /proc/self/fd/2 /dev/stderr
187 ln -s /proc/kcore /dev/core
191 6. Perform the mounting of the proper virtual (kernel) file systems:
193 mount -t devpts -o gid=4,mode=620 none /dev/pts
194 mount -t tmpfs none /dev/shm
196 Now you are in the chroot, so you can configure and build the kernel for
197 the CD. If you are absolutely sure that the kernel for your CD system
198 will work for the system you want the CD to run on, then you can skip this
199 part and just install the software you need.
201 Building the kernel for your live CD is not something to be taken lightly
202 by even a veteran! There are options which need to be built in and others
203 which can be built as modules. I assume that if you feel you are ready to
204 create a live CD out of your system, that you know what you are doing. I do
205 suggest not using modules, because it can create issues if you aren't
206 familiar with all the work that goes into using modules with the 2.6 kernel
207 and udev. Be Careful! =)
209 When you are done installing your kernel you might want to test it to be
210 sure it works. Once you are all set, then you should exit the chroot and
211 run the rest of the commands from your working system, as root.
213 -----------------------------
214 | Moving around the furniture |
215 -----------------------------
217 A.K.A. Moving /dev /etc /home /root /tmp /var to /fake/needwrite
219 When I followed the "Easy" hint, I came to this point and wondered:
220 What is /fake/needwrite? (Actually it was more like "what the hell is this
221 /fake/needwrite stuff?") Here is an answer for those of you wondering the
224 The /fake/needwrite directory is used to hold files that must be writable
225 while the live CD is running. Obviously, these files can not remain on the
226 CD-ROM (due to the RO part of ROM), and so they are moved to a location from
227 which they will be copied into a ramdisk during the CD boot process. Having
228 the files in a ram disk allows us to modify them. Also, we can create files in
229 the directories which reside on the ram disk, such as in /tmp, or /home.
231 First we have to create the directory and a mount point for the ramdisk:
233 * NOTE: If you haven't already mounted your CD system, do so now:
234 * mount $CDDEV $LIVECD
235 * thanks Bernard, for pointing this out ;)
237 mkdir -p $LIVECD/fake/{needwrite,ramdisk}
239 Then we can move the directories which we want to have write access there:
242 mv dev/ etc/ home/ root/ tmp/ var/ fake/needwrite/
244 Now we have to create symlinks so that everything seems to be as before.
247 ln -s fake/needwrite/dev dev
248 ln -s fake/needwrite/var var
249 ln -s fake/needwrite/tmp tmp
250 ln -s fake/needwrite/root root
251 ln -s fake/needwrite/home home
252 ln -s fake/needwrite/etc etc
254 At this point, execute ls -l
255 The output should say:
257 dev -> fake/needwrite/dev
258 etc -> fake/needwrite/etc
259 home -> fake/needwrite/home
260 root -> fake/needwrite/root
261 tmp -> fake/needwrite/tmp
262 var -> fake/needwrite/var
264 ---------------------------------------------
265 | Tailoring the CD boot process for our needs |
266 ---------------------------------------------
268 Ok, we have /etc /dev /var /tmp /root /home linked to /fake/needwrite which
269 is read-only (because it's on the CD). To be able to login (and to run
270 services which need write access to /dev /var /tmp /root /home or /etc) we
271 must call a script from our /etc/rc.d/init.d/ directory which mounts a ram
272 disk on /fake/needwrite with write access.
274 The following script creates 2 ram disks, a temporary one, and one that
275 will house the directories which need write permission. It copies the files
276 from the CD to the temporary ram disk, and then from there to the final ram
279 The original hint used 1 ram disk, but this caused a serious problem for me.
280 First of all, the initrd which is loaded at boot time uses the first ramdisk
281 (/dev/ram0). Therefore trying to mount /dev/ram0 somewhere else leads to
282 "device already mounted" errors. Secondly, unmounting a ram disk causes
283 all of the files to be lost. The original hint unmounted the ram disk and
284 remounted it, assuming the files would still be there. That did not work for
285 me, which is why I made sure that /dev/ram{0,1,2} are all present, and
286 suggest that you do the same. If they aren't present you should check your
287 udev setup. You might try using mknod to create the files, but for now
288 I will say if you don't have /dev/ram devices, you are going to have to
289 do some research on your own to figure out why.
291 * Bernard suggested a for loop for doing this, which you might try:
292 * for i in 0 1 2 3 4 5 6 7; do
293 * mknod $LIVECD/dev/ram$i b 1 $i;
296 Copy and paste the script, and tailor it to your needs as necessary:
298 cat > $LIVECD/etc/rc.d/init.d/create_ramdisk << "EOF"
301 # SET UP SOME VARIABLES FOR DEVICES AND DIRECTORIES
305 dir_ramdisk=/fake/ramdisk
306 dir_needwrite=/fake/needwrite
308 # SOURCE THE FUNCTIONS FILE
310 source /etc/rc.d/init.d/functions
315 # CREATE THE RAM DISK
317 echo "Creating ext2fs on $dev_ram..."
318 /sbin/mke2fs -m 0 -i 1024 -q $dev_ram > /dev/null 2>&1
324 echo "Mounting ramdisk on $dir_ramdisk..."
325 mount -n $dev_ram $dir_ramdisk -t ext2
329 # COPY FILES TO THE RAM DISK
331 echo "Copying files to ramdisk..."
332 cp -a $dir_needwrite/* $dir_ramdisk > /dev/null 2>&1
336 # CREATE SECOND RAMDISK
338 echo "Creating second ramdisk"
339 /sbin/mke2fs -m 0 -i 1024 -q $dev_ram2 > /dev/null 2>&1
343 # MOUNT SECOND RAMDISK
345 echo "Mounting second ram disk"
346 mount -n $dev_ram2 $dir_needwrite -t ext2
350 # COPY FILES TO THE SECOND RAMDISK
352 echo "Copying files to the second ram disk"
353 cp -a $dir_ramdisk/* $dir_needwrite
357 # UNMOUNT THE FIRST RAMDISK
359 echo "Unmounting and clearing first ram disk"
360 umount -n $dir_ramdisk > /dev/null 2>&1
361 blockdev --flushbufs /dev/ram1
366 echo "Usage: $0 {start}"
373 Make the script executable with the following command:
375 chmod 0755 $LIVECD/etc/rc.d/init.d/create_ramdisk
377 Gabe Munoz pointed out that this symlink can be S11, where it used to be at
378 S00. Of course feel free to number the symlink as you see fit. (This is LFS
379 after all!) Just make sure that you dont start any scripts that will try to
380 write to one of the directories that needs write permission before the
381 files are copied over.
383 cd $LIVECD/etc/rc.d/rcsysinit.d
384 ln -s ../init.d/create_ramdisk S00create_ramdisk
387 Next we can install the bootloader, isolinux. It is available with the
388 syslinux package. You can find syslinux on freshmeat:
390 http://freshmeat.net/projects/syslinux/
392 The directions below assume the tarball syslinux-2.11.tar.bz2 is already
393 placed in $LIVECD/usr/src. (use the current version, 2.11 might be old by
394 the time you read this)
397 tar xzf syslinux-2.11.tar.gz
398 mkdir $LIVECD/isolinux
399 cp syslinux-2.11/isolinux.bin $LIVECD/isolinux
401 mv $LIVECD/boot/* $LIVECD/isolinux
406 The bootloader needs a configuration file. The cat command below creates
409 * Note: Be careful with your kernel name!
410 * See http://syslinux.zytor.com/errors.php
411 * Keep the name short and sweet.
413 cat > $LIVECD/isolinux/isolinux.cfg << "EOF"
418 append initrd=initrd.gz root=/dev/ram0 init=/linuxrc ramdisk_size=16384
422 At this point it's a good idea to change /etc/fstab of the live CD system.
423 Delete all of the entries that you don't need. (e.g. all /dev/hd* entries)
424 You only need proc and devpts.
428 Don't worry about mounting root filesystem "/".
429 This will be mounted by the linuxrc script from the initial ram disk.
431 You may find it helpful to remove the following links:
433 rm $LIVECD/etc/rc.d/rc3.d/S20network
434 rm $LIVECD/etc/rc.d/rc0.d/K80network
435 rm $LIVECD/etc/rc.d/rc6.d/K80network
436 rm $LIVECD/etc/rc.d/rcsysinit.d/S40mountfs
437 rm $LIVECD/etc/rc.d/rcsysinit.d/S30checkfs
439 Ok! So far so good right?! In order for the boot process to work the way
440 we want, we create an initial ramdisk. More information about initial
441 ram disks (initrd for short) can be found in your kernel documentation.
443 The directions below create the initial ram disk (initrd):
445 dd if=/dev/zero of=$LIVECD/boot/initrd bs=1024 count=6144
446 mke2fs -m 0 -i 1024 -F $LIVECD/boot/initrd
448 mount -o loop $LIVECD/boot/initrd $LIVECD/mnt
450 mkdir bin sbin lib dev proc mnt sys etc
452 cp -a $LIVECD/bin/{bash,mount,grep,umount,echo,ln,mkdir} bin/
453 cp -a $LIVECD/sbin/udev* sbin/
454 cp -a $(find $LIVECD -name "test" -type f) bin/
455 cp -a $(find $LIVECD -name "chroot" -type f) bin/
456 cp -a $(find $LIVECD -name "pivot_root" -type f) bin/
457 cp -H $LIVECD/lib/{libncurses.so.5,libdl.so.2,libc.so.6,ld-linux.so.2} lib/
458 cp -H $LIVECD/lib/{libreadline.so.5.0,libhistory.so.5.0} lib/
459 cp -a $LIVECD/dev/{console,null,ram{0,1,2}} dev/
460 cp -a $LIVECD/etc/{udev,dev.d,hotplug.d} etc/
466 The first program executed by the kernel is /linuxrc. As it does not
467 exist we create it. Our script will find the CD in the correct CD-ROM drive
468 and then mount it as the root file system / and run /sbin/init 3.
470 Copy and paste the script and tailor it to your needs if necessary:
472 cat > $LIVECD/mnt/linuxrc << "EOF"
475 # ID is a file in root of the LFS boot CD, used to identify the CD.
481 PATH="/bin:/sbin:/usr/bin:/usr/sbin"
483 CHECK_TYPE="try_mount"
485 # MOUNT KERNEL FILESYSTEMS
487 # Create the proc directory if it does not exist
489 if [ ! -d "/proc/" ]; then
493 # Mount the proc filesystem
495 mount -n proc /proc -t proc
497 # If sysfs is listed as a valid filesystem type in /proc
498 # then mount it (if it doesnt then udev wont work
499 # and you wont have the devices you need)
501 if grep -q '[[:space:]]sysfs' /proc/filesystems; then
502 if [ ! -d /sys/block ]; then
503 mount -n sysfs /sys -t sysfs
507 # Create some things that sysfs does not, and should not export for us. Feel
508 # free to add devices to this list.
511 ln -s /proc/self/fd /dev/fd
512 ln -s /proc/self/fd/0 /dev/stdin
513 ln -s /proc/self/fd/1 /dev/stdout
514 ln -s /proc/self/fd/2 /dev/stderr
515 ln -s /proc/kcore /dev/core
521 if [ ! -x /sbin/hotplug ]; then
522 echo /sbin/udev > /proc/sys/kernel/hotplug
525 # Mount a temporary file system over /dev, so that any devices
526 # made or removed during this boot don't affect the next one.
527 # The reason we don't write to mtab is because we don't ever
528 # want /dev to be unavailable (such as by `umount -a').
530 mount -n ramfs /dev -t ramfs
536 # Detecting the live CD is pretty complicated,
537 # but is a very logical process
539 # Search for cdrom devices and add them to CDROM_LIST
543 # Search in proc tree for ide cdrom devices
544 # There used to be a section for devfs, but this was
545 # edited for udev. Actually we should probably not
546 # use /proc anymore, but use sysfs instead...
547 # Perhaps in the future;)
549 # Check for ide channels.
551 for ide_channel in /proc/ide/ide[0-9]
554 # If there are no ide channels found, then skip this
556 if [ ! -d "$ide_channel" ]; then
560 # Try each ide device to see if we can find the cd-rom drive
562 for ide_device in hda hdb hdc hdd hde hdf hdg hdh hdi hdj hdk hdl hdm hdn
564 device_media_file="$ide_channel/$ide_device/media"
565 if [ -e "$device_media_file" ]; then
566 grep -i "cdrom" $device_media_file > /dev/null 2>&1
567 if [ $? -eq 0 ]; then
568 CDROM_LIST="$CDROM_LIST /dev/$ide_device"
576 for scsi_cdrom in /dev/scd[0-99]
578 if [ -e "$scsi_cdrom" ]; then
579 CDROM_LIST="$CDROM_LIST $scsi_cdrom"
583 # Now we try to find the LFS boot CD (we use ID as identification)
587 for cdrom_device in $CDROM_LIST
589 if [ "$CHECK_TYPE" = "try_mount" ]; then
590 mount -n -t iso9660 ${cdrom_device} $TMP_MOUNT
595 if [ $media_found -eq 0 ]; then
596 echo -n "media found"
597 if [ "$CHECK_TYPE" = "try_mount" ]; then
598 [ -e "$TMP_MOUNT/$ID" ]
602 if [ "$CHECK_TYPE" = "try_mount" ]; then
603 umount -n $cdrom_device > /dev/null 2>&1
606 if [ $media_lfs -eq 0 ]; then
607 echo ", LFS boot CD found. Ready!"
608 LFS_CDROM_DEVICE="$cdrom_device"
611 echo ", not LFS boot CD."
619 # Mount LFS CD as / (root fs)
620 if [ "$LFS_CDROM_DEVICE" = "" ]; then
621 echo "No LFS boot CD found!!!"
624 echo "Booting from $LFS_CDROM_DEVICE..."
626 # This is the magical part that makes a live CD live!
627 # The cd is mounted and pivot_root+chroot commands
628 # are used to start the system.
629 # If you really want to know what is going on here,
630 # You should read the chroot and pivot_root man pages
632 mount -n -o ro -t iso9660 $LFS_CDROM_DEVICE $TMP_MOUNT
635 umount -n /mnt/proc >/dev/null 2>&1
636 exec chroot . sh -c 'umount -n /mnt >/dev/null 2>&1;
637 exec -a init.new /sbin/init 3' <dev/console >dev/console 2>&1
644 To make this script executable run:
646 chmod 0755 $LIVECD/mnt/linuxrc
648 Ok, that's it. Unmount the image and compress it.
652 gzip $LIVECD/boot/initrd
654 ---------------------
655 | Burning the live CD |
656 ---------------------
658 If you have a CD-RW you should use it for testing. When you know for sure
659 that it works well from the CD-RW you can burn it on a CD-R.(I give you
660 this advice, because I got the clue after burning about 10 CD-Rs that
661 didn't work ;-) [that goes for myself, and the author of the "Easy" hint ;)]
663 Before you start burning, check the size of your LFS tree:
667 Delete all the stuff you don't need on a live CD. (e.g. /usr/src/*)
669 Because linuxrc must be able to identify the CD you have to create a file
674 Now burn the LFS system on CD
678 * dev=/dev/hdc is the device number of your CD-Writer
679 * Check your devices with "cdrecord -scanbus"
680 * (as of this writing scsi-emulation is no longer required)
681 * speed=4 should be changed to (max) speed of your CD-Writer.
682 * If you are not using a CD-RW remove blank=fast from the cdrecord-command!
685 mkisofs -R -l -L -D -b isolinux/isolinux.bin -o $ISODIR/livecd_image.iso \
686 -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -V \
687 "livecd" $LIVECD && cdrecord -v -eject dev=/dev/hdc blank=fast \
688 $ISODIR/livecd_image.iso
696 * Thomas Foecking <thomas@foecking.de> and Christian Hesse <mail@earthworm.de>
697 For writing the "Easy Boot CD of your LFS" hint which confused me so much
698 when I tried it with udev that I decided to write this one =o)
700 * Gabriel Aneceto Munoz
701 For bring syntax errors and other updates to my attention
704 For giving some good suggestions
706 Feel free to email me comments, point out typos, etc.
710 * Initial hint completed.
712 * Added environment variables for more flexibility
714 * Finally found the time to proofread and submit the hint;)
716 * Fixed syntax errors and made other updates
718 * Tons of text changes and typo fixes, and various other stuff
720 * Edited to include Bernard's suggestions