Complete instructions for storing your data on CD.
by Alex Withers
These days, everyone seems to have a CD writer (CD-R). They are great pieces of hardware, and prices are going down all the time. CDs are great for keeping static data. Your favorite downloads contained on piles of floppies can now be transferred to one CD, lowering your risk of losing the data. With your own CD writer, you can make custom CDs of your favorite Linux distribution, crafted for your needs. You can also make CD archives of your favorite FTP site and pass it along to friends to avoid bandwidth problems.
If you are currently looking for a CD-R, get SCSI. SCSI CD-Rs have been around far longer than ATAPI (IDE) CD-Rs and, as a result, are better supported. However, if you do have an ATAPI CD-R or just can't afford SCSI, don't worry--you can still make CDs.
In order to write with a SCSI CD-R, you must have ``SCSI support'', ``SCSI CD-ROM support'' and ``SCSI generic support'' compiled in the kernel. Also be sure you have ``ISO9660 cdrom filesystem'' support. Having ``Loopback device support'' compiled in the kernel is a good idea, but not required. For this article, I will assume you know how to build your own kernel; if not, refer to the Kernel-HOWTO (see Resources).
ATAPI CD-Rs require a bit more effort. You must have at least kernel version 2.0.35; anything below this requires patches. Recompile your kernel with ``IDE/ATAPI CD-ROM support'' disabled and ``SCSI Emulation support'' enabled. Along with these options, you must also enable those options mentioned above (yes, even the SCSI support). The result is your CD-R will look and act like a SCSI device, even though it is an ATAPI. Your CD-writing software also needs to support ATAPI writing; I will discuss this later. Note that when you use SCSI emulation, all IDE CD-ROMs change to a SCSI prefix, so your first CD-ROM would be /dev/scd0.
CD writing in Linux requires two utilities: mkisofs and cdrecord. The first, mkisofs, is required to make an image of the files you wish to burn. Most major distributions come with this utility, but in any case, getting the latest version would be wise. The latest version can be found at ftp://tsx-11.mit.edu/pub/linux/packages/mkisofs/. The second utility, cdrecord, is the software used to burn the image made with mkisofs to a blank CD. You can find cdrecord at http://metalab.unc.edu/pub/Linux/utils/disk-management, and again, I highly recommend getting the latest version (1.6.1 at the time of this writing). Another utility for burning CDs is cdwrite which can also be found at that URL.
Aside from these, some free and commercial GUI programs for making CDs are also available. X-CD-Roast (see ``X-CD-Roast: CD Writer Software'' by Thomas Niederreiter, LJ, January 1998) is probably the most famous and is freely available; however, it is based upon the cdwrite utility. There are also X applications which act as a shell and call upon the appropriate utilities to do the job.
Once you have the appropriate utilities installed, you can begin to write your own CDs. Before you begin, collect the files to be burned under one directory. I'm going to use the downloading and burning of a Linux distribution as an example throughout this article. First, let's say you anonymously log in to ftp.some_server.com and switch to the directory /pub/linux. This hypothetical directory contains three different distributions; the list output ls looks like Listing 1.
Assuming distribution_b is the one you want, proceed to download the entire distribution by typing get distribution_b.tar at the ftp prompt. Of course, this directory contains everything you need and nothing else. After all, you wouldn't want to download the distribution for several different architectures. Thus, when you download an entire distribution, make sure you go deep enough into the directory to get only what you need. For example, if you wanted to download the entire Red Hat distribution for the i386 architecture on ftp.cdrom.com, you would download everything in the /pub/linux/redhat/redhat-6.0/i386 directory and below.
Once the download is finished, create a directory for storing the files. In this example, I'll create a directory called cdimage, then place the contents of distribution_b.tar into that directory by executing the following commands:
mkdir cdimage tar vxf distribution_b.tar -C cdimageSince the verbose (v) option is requested, the contents of distribution_b.tar will fly by on your screen and its contents will be located in cdimage. If everything went well, you can now delete distribution_b.tar to save space.
If you want to burn some collection of files other than a Linux distribution, just place them in the cdimage directory.
Remember when creating CDs, the root of the CD is relative to the created directory; in this case, cdimage. Once you have your files in this directory, you are ready to create the iso9660 image using the mkisofs command. To create the basic image, use the following command:
mkisofs -r -o cdimage.iso cdimageThe -r option ensures the image contains additional file description data by way of the Rock Ridge protocol, preserving the original file name and setting permissions optimally for CD-ROMs such that read/execute permissions become global, write permissions are cleared, and special mode bits are also cleared since they do not apply on CD-ROMs. The -o option designates the output file (cdimage.iso). The last value is the directory in which the files are located.
Many commercially manufactured Linux CDs, such as Red Hat, are bootable. This isn't difficult to do using the ``El Torito'' standard. Most newer BIOSes today support the bootable CD feature, and most bootable CDs for the PC are based on El Torito. El Torito makes your CD appear as a floppy, and thus your BIOS can boot it.
If you want a bootable CD, you'll need a 1.44MB boot image intended for a boot floppy. In our distribution example, we could use the boot image used for installation. For distribution_b, the name of the boot image is boot.img. The process by which we make a CD bootable takes place in the creation of the iso9660 image (International Organization for Standardization specification for compact disk read-only memory). Thus, before we create our image, we need to create a directory inside of cdimage to hold the boot image; a directory called boot would work fine. So, we place the image boot.img into cdimage/boot and create the iso9660 image by executing the following command:
mkisofs -r -b boot/boot.img -c boot/boot.cat -o\ cdimage.iso cdimageHere we have two new options, both of which are used to make the CD bootable. The -b option is followed by the name of the boot image to be booted. Note that the file is relative to the root of the CD. The -c option is followed by the name of the boot catalog required by El Torito; this file is automatically created by mkisofs. Only the more recent versions of mkisofs allow for the automatic creation of the boot catalog; older versions require you to create it yourself.
Before actually burning the CD, take a look at your image layout by mounting it. This is done using a loopback device, so this must be supported in the kernel. The following command will mount your image:
mount -r -t iso9660 -o loop cdimage.iso /mnt
Once you've created your image, bootable or not, you are ready for the final process of burning it onto the CD using either cdrecord or cdwrite. Take the following into consideration before you start:
cdrecord -eject -v -isosize speed=2 dev=0,0\ cdimage.iso cdwrite -ev --device /dev/??? -s 2 cdimage.isoThe first two options for both utilities are eject and verbose. Thus, the CD will eject after the burning process is finished, and the program will run in verbose mode. The option -isosize for cdrecord limits the size of the CD to the size of the iso9660 image. The options speed=2 and -s 2 indicate the speed at which to write to the CD; in this case, the 2 means at 2x. Finally, the options --device, dev=0,0 and /dev/??? set the target device, where /dev/??? should be your CD-R (i.e., /dev/scd0) and 0,0 stands for the SCSI ID and bus in that order.
If all worked out well, congratulations. You now have a full-fledged, iso9660 CD. You can make a CD with other formats just as well. The utility mkhybrid, included with mkisofs, can make images of Joliet and HFS format. It is also possible to make a CD with the EXT2 file system format. Have fun burning!
Alex Withers has been using Linux since 1.1.59. He is currently studying computer science at Gonzaga University and can be reached at awithers@gonzaga.edu.