Next Previous Contents

3. Burning CD-Rs

"If to smoke you turn I shall not cease to fiddle while you burn." (Roman emperor Nero about burning his own classic-CDs, AD64. He misunderstood it completely and burned Rome down.)

Writing CD-ROMs consists of two steps under Linux:

This chapter describes the steps for data and audio CDs in greater detail.

3.1 Writing CD-ROMs (pure data)

Note that collecting the data to put onto a CD usually takes longer than one expects. Consider that missing files cannot be added once the CD is written and fixated. Also keep in mind that a certain amount of the free space of a CD is used for storing the information of the ISO-9660-filesystem (usually a few MB). 620 MB data will always fit on a 650 MB CD-R.

Creating an image of the later CD-ROM

Before any storage medium (e.g. floppy disk, hard disk or CD) can be used, it must get a filesystem (DOS speak: get formatted). The filesystem is responsible for organizing and incorporating the files that should be stored on the medium.

The usual utilities for creating filesystems on hard disk partitions write an empty filesystem onto them, which is then mounted and filled with files by the user as they need it. A writable CD is only writable once so if we wrote an empty filesystem to it, it would get formatted and remain completely empty forever. This is also true for rewritable media as you cannot change arbitrary sectors; you must erase their whole content.

So what we need is a tool that creates the filesystem while copying the files to the CD. This tool is called mkisofs. A sample usage looks as follows:

mkisofs  -r   -o cd_image   private_collection/
              `---------'   `-----------------'
                   |               |
          write output to   take directory as input

The option '-r' sets the permissions of all files to be public readable on the CD and enables RockRidge-extensions. You probably want to use this option unless you really know what you're doing (hint: without '-r' the mount point gets the permissions of private_collection!).

mkisofs will try to map all filenames to the 8.3 format used by DOS to ensure the highest possible compatibility. In case of naming conflicts (different files have the same 8.3 name), numbers are used in the filenames and information about the chosen filename is printed via STDERR (usually the screen). Don't panic: Under Linux you will never see these 8.3 filenames because Linux makes use of the Rock Ridge extensions which contain the original file information (permissions, filename, etc.).

Now you may wonder why the output of mkisofs is not directly sent to the writer device. There are three reasons:

There is a method to write a CD-R in one go which will be described below.

One also could think of creating an extra partition and writing the image to that partition instead to a file. I vote against such a strategy because if you write to the wrong partition (due to a typo), you can lose your complete Linux system (read: that happened to me...). Furthermore, it is a waste of disk space because the CD-image is temporary data that can be deleted after writing the CD. However, using raw partitions saves you the time for deleting files of 650 MB size.

Test the CD-image

Linux has the ability to mount files as if they were disk partitions. This feature is useful to check that the directory layout and file access permissions of the CD image matches your wishes. Although media is very cheap today, the writing process is still time consuming, and you may at least want to save your time by doing a quick test.

To mount the file cd_image created above on the directory /cdrom, give the command

mount -t iso9660 -o ro,loop=/dev/loop0 cd_image /cdrom

Now you can inspect the files under /cdrom -- they appear exactly as they were on a real CD. To umount the CD-image, just say umount /cdrom. (Warning: On Linux kernels prior to 2.0.31 the last file on /cdrom may not be fully readable. Please use a more recent kernel like 2.0.36. The option -pad for cdrecord applies to audio CDs only and the option -pad for mkisofs requires a patch, which is as much work to apply than to upgrade to a bug-free Linux kernel.)

Note:

Some ancient versions of mount are not able to deal with loopback devices. If you have such an old version of mount, then upgrade your Linux-system. Several people have already suggested putting information about how to get the newest mount utilities into this HOWTO. I always refuse this. If your Linux distribution ships with an ancient mount, report it as a bug. If your Linux distribution is not easily upgradable, report it as a bug.

If I include all the information that is necessary to work around bugs in badly designed Linux distributions, this HOWTO would be a lot bigger and harder to read.

Write the CD-image to a CD

Not much more left to do. If you haven't already tried, it's a good time for the command

cdrecord -scanbus

This will tell you to which SCSI device your CD-writer is attached to. All other methods of guessing the information printed so nicely by cdrecord have been removed from the HOWTO (especially the somewhat dangerous naming scheme for generic SCSI devices).

Before showing you the last command, let me warn you that CD-writers want to be fed with a constant stream of data because they only have small data buffers. So the process of writing the CD image to the CD must not be interupted or a corrupt CD will result. It's easy to interrupt the data stream by deleting a very large file. Example: if you delete a previous image of 650 Mbyte size, the kernel must update information about 650,000 blocks on the harddisk (assuming you have a blocksize of 1 Kbyte for your filesystem). That takes some time and is very likely to slow down disk activity long enough for the data stream to pause for a few seconds. However, reading mail, browsing the web, or even compiling a kernel generally will not affect the writing process (on modern machines).

Please note that no writer can re-position its laser and continue at the original spot on the CD when it gets disturbed. Therefore any strong vibrations or other mechanical shocks will probably destroy the CD you are writing.

When you are mentally prepared, dress up in a black robe, multiply the SCSI-id of the CD-writer with its SCSI-revision and light as many candles, speak two verses of the ASR-FAQ (newsgroup alt.sysadmin.recovery) and finally type:

shell> SCSI_BUS=0   # taken from listing 1 "scsibus0:"
shell> SCSI_ID=6    # taken from listing 1 "TOSHIBA XM-3401"
shell> SCSI_LUN=0
shell> cdrecord -v speed=2 dev=$SCSI_BUS,$SCSI_ID,$SCSI_LUN \
                   -data  cd_image

# same as above, but shorter:
shell> cdrecord -v speed=2 dev=0,6,0  -data  cd_image

For better readability, the coordinates of the writer are stored in three environment variables with natural names: SCSI_BUS, SCSI_ID, SCSI_LUN. The option -data is not mandatory but is given to make the commandline comparable to the one used for writing audio CDs.

If you use cdrecord to overwrite a CD-RW, you must add the option "blank=..." to erase the old content. Please read the man page to learn more about the various methods to blank the CD-RW.

In times where everybody except me owns a 400 Mhz machine, people feed the output of mkisofs directly into cdrecord:

shell> IMG_SIZE=`mkisofs -R -q -print-size private_collection/  2>&1 \
| sed -e "s/.* = //"`
shell> echo $IMG_SIZE
shell> [ "0$IMG_SIZE" -ne 0 ] && mkisofs -r  private_collection/  \
|cdrecord  speed=2  dev=0,6,0
            tsize=${IMG_SIZE}s  -data  -
#       don't forget the s --^         ^-- read data from STDIN

The first command is an empty run to determine the size of the image (you need the mkisofs from the cdrecord distribution for this to work). Maybe your writer does not need to know the size of the image to be written, so you can leave this out. The printed size must be passed as a tsize-parameter to cdrecord (it is stored in the enviroment variable IMG_SIZE). The second command is a sequence of mkisofs and cdrecord, coupled via a pipe.

3.2 Writing audio CDs

Writing audio CDs is very similar to the steps described above for data CDs. There are two main differences. One, the audio CD consists of audio tracks, which are organized as separate images. So if you want to have ten tracks on your CD, you have to make ten images. The other differnce is that the format of the images is not ISO-9660 (or whatever filesystem you prefer), but it is "16 bit stereo samples in PCM coding at 44100 samples/second (44.1 kHz)".

One utility to convert your sound files into the required format is sox. Its usage is straightforward:

shell> sox  killing-my-software.wav  killing-my-software.cdr

This command would convert the song killing-my-software from the WAV-format into the CDR audio-format. See the man page for sox for more details about formats and filename-extensions sox recognizes. Because the output of the conversion takes up much disk space, it was made a built-in feature of cdrecord for the formats WAV and AU. So as long as your soundfiles have the extensions .wav or .au (and the samplerate "stereo, 16 bit, 44.1 kHz"), you can use them as images without manual conversion.

Cdrecord writes CD images as audio tracks if the option -audio is specified. The other options are identical to those used for writing data-CDs (unless you have very special requirements). These three examples all do the same thing, but read the tracks from different sound file formats:

shell> cdrecord -v speed=2 dev=0,6,0  -audio  track1.cdr track2.cdr...
shell> cdrecord -v speed=2 dev=0,6,0  -audio  track1.wav track2.wav...
shell> cdrecord -v speed=2 dev=0,6,0  -audio  track1.au  track2.au...

One notable exception are MPEG Layer 3 files, which can be converted to the CD format with the command "mpg123 -s track1.mp3 > track1.cdr". (Warning: this creates a file with a byte order which needs to be reversed by using the option -swab in cdrecord.) Converting the other direction can be done with "8hz-mp3" for WAV-files (extract the track with cdda2wav from the audio CD and encode it into MP3 with 8hz-mp3). To create a CD-R from a whole bunch of MP3-files, you can use the following command sequence:

for I in *.mp3
do
    mpg123 -s $I | cdrecord -audio -pad -swab -nofix -
done
cdrecord -fix

Depending on the speed of your machine, you may want to slow down writing to "speed=1" (cdrecord-Option). If you use "speed=4", your machine must be able to play the MP3-file at quadruple speed. mpg123 consumes much CPU-time! If you are in doubt, try an empty run with -dummy (keeps the laser switched off). By doing this, you will produce an audio CD which has a 2 seconds of pause between audio tracks.

DAO

If you want to get rid of the pauses between the audio tracks, you have to use disk-at-once (DAO) recording versus the (individual) track-at-once (TAO) recording described above. Support for DAO is currently most advanced in cdrdao. Please see its homepage for details.

Using the parameter read-cd, you are even able to produce 1:1 copies of audio CDs.

3.3 Mixed mode CD-ROMs

There is not much to say about this topic. Just indicate the type of the (subse quent) images with the options -data and -audio. Example:

cdrecord  -v dev=0,6,0  -data  cd_image  -audio  track*.cdr


Next Previous Contents