Navigation:

Search



Our Friends

Articles CD Duplication and Mastering under …
 

CD Duplication and Mastering under Linux

Quick useful examples on how to record CDs in Linux.

This was written by David L. Cantrell Jr. and given on Sat Sep 14 2002.

Table of Contents


Note: Throughout this document, I refer to CD-RW drives for the recordable drives. I mean CD-RW and CD-R drives though, it's just easier to say CD-RW when referring to both types of drive.
1. Hardware

CD-RW drives were originally only available as SCSI devices. This has many advantages over other interfaces, but price isn't one of them. ATAPI CD-RW drives are now to a point where they perform equal to or, in some cases, better than comparable SCSI drives. My first two burners were SCSI, now I use an ATAPI one.

It is important to understand that all CD-RW drives built for a personal computer speak the SCSI protocol. ATAPI is a way of sending SCSI commands over IDE. It's how we have IDE CD-ROM drives, for example.

Since IDE CD-RW drives speak SCSI commands, we can easily make them work under Linux by using the SCSI emulation layer.

1.1. SCSI

If you prefer SCSI equipment, look no further than Plextor. These guys make great drives and they work under all operating systems. The other SCSI brand I don't have trouble recommending is Ricoh. My first burner was a Ricoh SCSI one and it lasted for four years before just giving out after continuous use. HP also makes reasonable SCSI drives.

If you do go with SCSI, don't forget the SCSI host adapter. If you don't have one of these, you'll need to get one. It can add anywhere from $50 to $200 to the price of the drive. You will want a good host adapter. Buying a top of the line professional $1000 SCSI burner and driving it off an Adaptec 1502 ISA host adpater would be stupid. Generally speaking, go with ATAPI unless you already have SCSI equipment.

1.2. ATAPI (IDE)

ATAPI drives work on most all IDE controllers. Plextor and Teac both math nice ATAPI drives.

Isolating your burner on its own IDE channel is also a good idea. This way, your hard disks and burner can operate at their maximum speeds. Also note that if you plan to direct CD to CD-R transfers, the two devices should not be on the same channel.

1.3. Firewire and USB

Newcomers to the world of burners. I have not used a Firewire or USB burner, but I hear that the ones that do work under Linux work like an ATAPI device. You load the module for the device and then communicate with it over the SCSI emulation layer.

1.4. Miscellaneous Recommendations

I was once told two good arguments for paying the extra money for an external caddy-loading CD-RW drive. Caddy-loading drives have fewer moving parts, which means there's a less likely chance that the drive will die from mechanical failure. And an external drive can be turned off, which means it won't be sucking dust from the inside of your case when the drive is not in use.

Something to consider, but honestly, CD-RW drives are so cheap these days that there's really not much point in seeking out hardware that will last forever. We all get new computers every few years anyway (or parts or whatever).

1.5. Kernel Stuff

For a SCSI drive, you will want to enable the driver for your host adapter. Virtually all Adaptec PCI adapters use the aic7xxx driver. Symbios cards are the next most popular, and the sym53c8xx driver will take care of most cards in that family.

The drive acts as both a reader and writer. For reading, we need to enable the SCSI CD-ROM driver. To write to the drive, we enable the SCSI generic driver and turn on the write permissions with the chmod command on the device node. With both the CD-ROM and generic driver enabled, your drive will be available as a /dev/srX and a /dev/sgX device, where X is a number or letter depending on the driver. If you have only one device, you would have /dev/sr0 and /dev/sga. The sr device is your SCSI Read device, and the generic device (sg) will be what we use for writing (after doing a "chmod +x" on it). Lastly, you should enable vendor specific extensions under the SCSI configuration section of the kernel.

For an IDE drive, you don't need a SCSI host adapter driver (not even the dummy on). You do, however, need to enable SCSI emulation support. You can leave the IDE CD-ROM support enable, but I recommend disabling it as it has been known to cause conflicts with the SCSI emulation driver.

With SCSI emulation support on, your IDE CD-ROM drive will be identified as a /dev/srX and /dev/sgX device, just as the SCSI drives are. If you leave the IDE CD-ROM driver enabled, you can pass the "ide-scsi=hdX" boot parameter to tell the SCSI emulation driver which IDE drive you want emulated as a SCSI drive.

2. Standards and Terminology

There are some specific terms associated with CD recording. Below are some of the common ones.

2.1. Standards
2.1.1. ISO 9660

Also known as the High Sierra filesystem, this is the standard filesystem format for CD-ROM data discs. It's supported under all operating systems that support CD-ROM drives. This filesystem has certain extensions by various groups and companies that make it more usable on specific operating systems. Most people this to "ISO" when naming ISO 9660 CD-ROM images. This is incorrect, but people still insist on it.

2.1.2. Rockridge

UNIX extensions to ISO 9660. These allow us to have permissions and ownerships for files on a CD-ROM, among other things. The name for this comes from an area of Oakland, CA (kind of like Midtown is to Atlanta). There is a Rockridge BART station and a Rockridge public library.

2.1.3. Joliet

Microsoft extensions to ISO 9660. Filenames are stored in Unicode. Making a Joliet-only CD usually results in read problems for non-Microsoft operating systems.

2.1.4. El Torito

The bootable CD-ROM specification created for Intel PCs. It's name comes from a chain of Mexican restaurants in the San Francisco Bay Area.

2.2. Terminology
2.2.1. disc-at-once (DAO)

Recording an entire disc without turning off the laser. Offers more control over the disc layout. DAO will also eliminate the 2 second gap between tracks on an audio CD.

2.2.2. track-at-once (TAO)

Recording a disc one track at a time, with the laser turning off then on again between each track.

2.2.3. buffer underrun

This happens when your computer does not send data to the burner fast enough. It results in a bad burn and is typically due to a slow computer, not enough buffer memory on the CD-RW drive, bad software, or a combination of any of the above.

2.2.4. pregap

Silence between audio tracks. CDs created in track-at-once mode have a default 2 second pregap.

2.2.5. coaster

The physical media resulting from a bad burn. A coaster cannot be used or repaired. I think it's a bad name to use because CDs make terrible coasters.

2.2.6. burn/roast/toast

To record data on a blank CD-R or CD-RW media.

3. Software

Under Linux, we use a collection of command line tools to record CDs. There are numerous graphical frontends out there, but becoming familiar with the actual command line tools will give you a better understanding of the process of creating a CD under Linux.

3.1. cdwrite (historical)

The original CD recording tool for Linux. Only worked with a handful of writers, and only SCSI ones at that. The original author was Adam Richter (of Yggdrasil Computing, Inc.). Interestingly, it shares a syntax similar to what cdrecord offers.

This program is deprecated, obsolete, broken, and should not be used.

3.2. cdrtools (cdrecord)

Cdrtools is the name of the software collection that includes cdrecord, mkisofs, mkhydrib, and cdda2wav. You get mastering, writing, and ripping tools all in one. Cdrecord is arguably the best writing program available for UNIX. Cdrecord can speak with SCSI and ATAPI burners and supports DAO and TAO recording. Many other options are available.

Cdda2wav is the audio ripper included with cdrtools. Most people prefer to use cdparanoia for ripping as it tends to work with a wider range of hardware. I find cdda2wav to be extremely fast on SCSI hardware, but your mileage may vary.

3.3. cdrdao

Cdrdao was written to specialize in writing disc-at-once CDs. Support for the command ".BIN/.CUE" format is supported by this program as well. Though the build procedure seems a bit anti-GNU, it does work well and is handy when cdrecord isn't playing nice.

3.4. cdparanoia

Cdparanoia seems to be the preferred audio ripper amongst the Linux community. It's known for being able to rip scratched CDs successfully.

3.5. sox/wav2cdr

If you'll be creating audio CDs from MP3 files, you have to send the decoded MP3s through a swabbing process. Cdrecord claims to do this transparently, though I have had varying success with it. I find sox to be a much better solution. Sox can transform a .wav file in to a CD audio track file, which can then be written to a CD. Wav2cdr offers similar functionality.

3.6. mkisofs/mkhybrid

Since CDs are read-only, we need to create a self-contained filesystem to write to the media. Under Linux we use the mkisofs tool to create ISO 9660 CDs. The mkhybrid tool is a special version of mkisofs used for creating Macintosh format CDs, but it's functionality has been merged in to the larger and way more complex mkisofs program.

3.7. lame

There are many MP3 encoders out there. The LAME program is a nice encoder if you want to rip an audio CD and encode it to MP3s.

3.8. Ogg Vorbis

Ogg Vorbis is a fully open standard for compressed audio. An entire presentation can be done on Ogg Vorbis alone, so I won't go into those details here. If you want to rip a CD to Ogg Vorbis files or make a CD from a collection of Ogg Vorbis files, you will need the Ogg Vorbis utilities. Most major distributions now ship with the necessary components (libogg, libvorbis, libao, vorbis-tools).

3.9. Graphical Front Ends

There are a lot of graphical frontends for burning CDs. All of them use the tools described above to get the job done. Some are tailored to a specific task (burning DAO audio CDs, for example), others aim to be the most full featured programs around.

Some of the major burning frontends are:

X-CD-Roast http://www.xcdroast.org/
gcombust http://www.abo.fi/~jmunsin/gcombust/
KisoCD http://kisocd.sourceforge.net/kisocd.htm
Gnome Toaster http://gnometoaster.rulez.org/
KOnCD http://www.koncd.de/
KreateCD http://www.kreatecd.de/

It may be a good idea to understand the underlying commands these frontends use, even if you plan on using a recording frontend all the time. Sometimes the frontends fail and you have to resort to the command line tools.

4. Examples

The following examples assume you're CD-R(W) is /dev/sga and your CD-ROM is /dev/sr0. Change the device nodes to correctly reference your system.

4.1. Finding your CD-RW device

Your burner should be accessible through the SCSI subsystem. The easiest way to find it is to run cdrecord and scan the bus:

cdrecord -scanbus

This will return output similar to the following:

Cdrecord 1.10 (sparc-sun-solaris2.8) Copyright (C) 1995-2001 Jrg Schilling
Using libscg version 'schily-0.5'
scsibus0:
  0,0,0     0) 'QUANTUM ' 'ATLAS 10K 36LWS ' 'UCP0' Disk
  0,1,0     1) *
  0,2,0     2) *
  0,3,0     3) 'PLEXTOR ' 'CD-ROM PX-40TX  ' '1.04' Removable CD-ROM
  0,4,0     4) 'PLEXTOR ' 'CD-R   PX-W124TS' '1.06' Removable CD-ROM
  0,5,0     5) *
  0,6,0     6) *
  0,7,0     7) HOST ADAPTER

My CD-RW is device 0,4,0. I will use this when recording CDs rather than the Linux device node (those may or may not change).

4.2. Erasing a CD-RW disc

Assuming my CD-RW device is 0,4,0, I use cdrecord with this command:

cdrecord -v dev=0,4,0 blank=fast

4.3. Making a bootable Linux CD on an x86 machine

This one requires knowing what's needed for a bootable root filesystem. But let's assume you have that and you've managed to fit it into 2.5MB of space (your root filesystem). First, you make an image file containing that filesystem. Make a 2.88MB file, format and mount it, and copy your root filesystem there. Call the image flop288.img. This is a 2.88MB floppy disk image.

The El Torito standard emulates either a 1.44MB or 2.88MB floppy disk through the CD-ROM. Your disk image will reside at the beginning of the drive and will be loaded and booted as though it were a floppy.

Create a temporary filesystem to hold everything for this CD, including your flop288.img file:

mkdir -p /tmp/cd-root
cp> stuff <flop288.img /tmp/cd-root

Now we can create the bootable image:

cd /tmp/cd-root
mkisofs -o /tmp/bootcd.img -R -V "My Boot CD" \
   -v -d -D -N -b flop288.img -c boot.cat -A "My Boot CD" .

The bootable CD image file is now /tmp/bootcd.img. There are several other options related to boot CDs. For instance, the -no-emul-boot option causes a 'no emulation' El Torito image to be created. This is like a normal El Torito image, but it doesn't emulate the floppy disk. There are also switches to add multiple boot images to the disc. And, you can specify a hard disk partition for the -b switch, provided you also specify the -hard-disk-boot option. Mkisofs will then use the hard disk to create the boot image for the CD.

4.4. Copying a data CD

I prefer using cat to make image files of CDs I want to copy:

cat /dev/sr0 </tmp/cd.img

The /tmp/cd.img file can now be burned to another disc using cdrecord, like this:

cdrecord -v dev=/dev/sga -data -eject image.img

Some people like doing direct CD to CD copies. You can do this, provided you have two drives. Say your burner is 0,4,0 and your reader is identified under Linux as /dev/sr1. This command will do a disc to disc copy:

cdrecord -v dev=0,4,0 -eject -isosize /dev/sr1

And you can always use dd to make the image file and then it using cdrecord.

4.5. Creating an audio CD from MP3 files

First you'll want to select either 74 minutes or 80 minutes worth of music. Count the time, not the file sizes. XMMS can create a playlist and give you total running time. There are plenty of other methods to calculate running time for a set of MP3s.

Once you have your MP3 files selected, decode them to WAV files. I use amp to do this:

amp -c -w MP3 fileWAV file

I run that command for each MP3 file and end up with a set of WAV files. The WAV files are almost ready for burning. I need to reverse the byte order so that they sound right on an audio CD player. For that, I use sox:

sox -w -x WAV fileCD audio track

Now my set of WAV files have been converted to CD audio tracks and I can burn them using cdrecord:

cdrecord -v dev=/dev/sga speed=12 -audio \
   -eject CD audio track ...

Just list the track files one after another at the end of the cdrecord command. The program will burn them to the disc in this order. This method burns the audio disc in track-at-once mode, which is somewhat nasty for music CDs. A better method is to record in disc-at-once mode to get rid of those pregaps:

cdrecord -v dev=/dev/sga speed=12 -audio -dao \
   -eject track ...

You can also use cdrdao to do the above steps. For the conversion from MP3 to CD audio, we can get a bit more creative. Instead of decoding to WAV then to CDDA, we can combine the two commands so it ones in one step:

amp -c -w MP3 file - | sox -w -x - CD audio track

So how about a for loop to do that?

for mp3file in *.mp3 ; do
   cddafile="`basename $mp3file .mp3`.cdda"
   amp -c -w $mp3file - | sox -w -x - $cddafile
done

4.6. Copying an audio CD

There are a couple ways to do this. Cdrdao is probably the easiest way to make a perfect copy of an audio CD, that's what I like to use:

cdrdao copy \
   --device 0,4,0 \
   --source-device 0,3,0 \
   --speed 12 \
   --eject

You can also use cdrdao to make a copy of the disc for burning later:

cdrdao read-cd --source-device 0,3,0

Using the copy command with cdrdao will cause the program to create an image file and then burn that. If you want no image file (like a cdrecord disc to disc copy), use the --on-the-fly option.

4.7. Making a Data CD

Creating a data CD for a Linux system is pretty much the same as creating a bootable Linux CD, you just don't specify the -b or -c flags.

First, make a temporary directory and populate with the files you want on the CD. Assuming the temporary directory is /tmp/cd-root, issue this command to make an image:

cd /tmp/cd-root
mkisofs -o /tmp/datacd.img -R -V "My Data CD" \
   -v -d -D -N -A "My Data CD" .

This will make an ISO 9660 image file with Rockridge extensions. This is generally what you should use when making a Linux or UNIX CD image. If you will be using the disc on a Windows machine, consider using the -J flag on mkisofs to generate Joliet data. One method of creating a Windows CD is:

mkisofs -allow-lowercase -allow-multiboot -D -l -iso-devel 3 \
   -J -relaxed-filenames -no-iso-translate -R \
   -o /tmp/datacd.img .

Generating a Macintosh CD takes quite a bit more effort. Macs have traditonally used the HFS filesystem on CDs as well as hard disks. The mkisofs program can generate HFS CD images, but it can also generate CD images using Apple's extensions to ISO 9660 (kind of like Apple Joliet). There is a lot of information you need to gather before generating the CD. The positions of icons on the CD, type and creator codes for the files, and probably some other stuff. If you're really interested in making a CD for Macs that will be read under pre MacOS X, just use a Mac.

4.8. Creating an Audio CD from Ogg Vorbis Files

You can use the oggdec program to convert Ogg Vorbis files to CDDA files:

for oggfile in *.ogg ; do
   cddafile="`basename $oggfile .ogg`.cdda"
   oggdec -b 16 -e 1 -o $cddafile
done

You can then burn those CDDA files to a disc:

cdrecord -v dev=/dev/sga speed=8 -audio -dao pregap=0 -eject *.cdda

4.9. Making a VideoCD

There is a nice tutorial on creating VideoCDs under Linux at:

http://www.satlug.org/~bigjnsa/vcd-linux.html

There are some additional tools you will need installed in order to convert movie files to VCD images. Links are provided on the page above to get the additional tools.

4.10. Copying a PlayStation CD

Making backup copies of your Playstation games can be handy for several reasons. Educational purposes being the top one.

Cdrdao comes with a tool called psxcopy which can make copies of your Playstation games. There are two C programs, cdjob and psxdump, which handle copying the data and audio from the game disc. Two scripts are included which combine the usage of those programs with cdrdao to make copies of Playstation discs.

5. Resources

This article has external documents! Click here.