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.)

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)

t Note that collecting the software usually takes up 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, harddisk 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 usal utilities for creating filesystems on harddisk partitions write an empty filesystem onto them, which is then mounted and filled with files by the user as he needs it. A writable CD is only writable once so if we would write an empty filesystem to it, it would get formated - but remain completely empty forever. This is also true for rewritable media, as you cannot change arbritary sectors, but 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 Rock Ridge extensions. That is what one usually wants and use of this option is recommended until you 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 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. This has two reasons:

The method to write a CD-R in one go 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, its 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. (If you have enough disk-space, an extra partition may save you the time of deleting the image.)

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 more recent kernels like 2.0.36. The option -pad for cdrecord applies to audio-CDs only and the optional 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 it is a hint to upgrade your Linux-system.

Several people already suggested to put 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 had included all the information that is necessary to work around bugs in bad 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 didn't already tried, its 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 have only small data-buffers. So the process of writing the CD-image to the CD must not be interupted or a corrupt CD will be the result. Its 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 a few seconds. However, reading mail, browsing the web or even compiling a kernel generally do not affect the writing process on modern machines.

Please note that no writer can re-position its laser and can't 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 speaking names: SCSI_BUS, SCSI_ID, SCSI_LUN. The option -data is non-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 sized must be passed as a tsize-parameter to cdrecord (its 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

To write audio-CDs is very similar compared to the steps described above. The two main differences are, that the audio-CD consists of audio-tracks, which are organized in 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 soundfiles 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 conversion takes up much diskspace, 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 the 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). Three examples all doing the same, but reading the tracks from different soundfile-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...

A special case are the MPEG3-files, which can be converted to the required 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 to cdrecord.) 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). Currently you will produce an audio-CD, which has a 2 seconds pause between two 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 in opposite to the (individual) track-at-once (TAO) recording described above. Support for DAO is currently most advanced in cdrdao. Please see it's 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

Not much left to say. Just indicate the type of the (subsequent) images with the options -data and -audio. Example:

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


Next Previous Contents