Jörgs LinuxPages: CD Writing

Tux!

This page summarizes a few observations about writing ("burning") CD and DVD media under Linux.

Note: The material on this page is no longer updated. I keep this page merely as reference material: With a recent Linux distribution, burning CD and DVD is easy and straightforward.

Backup on CDs

Backup on multisession CD-R

If you use your computer for serious work, the price of lost data is certainly more expensive than any backup medium. That's why I perform regular backups onto CD-RW media (see below) and from time to time I archive my complete /home directory tree onto CD-R. The media are nowadays really cheap (you can find quality CD-R for 0.50 CHF). If I have less than 650 MB to save, I use multi-session CDs and add several sessions onto the same disk, but in different directories. First, create an ISO image:

mkisofs -L -J -R -V Backup_Home -m '*cache*' -o /tmp/img.iso
-graft-points home.20010704/=/home

The trick for differentiating backups from different dates lies in the option -graft-points. I use this to relocate the directory /home to the new directory home.20010704 on the CD - the extension after the dot is the date when the backup was performed, here it is the 4th of July 2001.

The other options stem from practical considerations: -R preserves file ownership and permissions, -J enables the Joliet filesystem, so that all filenames are readable on Linux and MS-DOS systems. -L allows filenames with a leading dot (think hidden files ;-) and -V is followed by the volume name. -o gives the name of the output file (here, /tmp/img.iso), and -m excludes cache files - I do not need a backup of that.

Make sure you use mkisofs 1.13 or later. The earlier versions had problems with multi-session disks. -- For 650-MB CD media, don't let the image file to grow bigger than 332500 blocks (each 2048 bytes, makes 680960000 bytes in total). Some CD-writing programs and/or CD-R disks don't allow bigger images. Then, burn the disk using the option -multi for a multi-session disk:

cdrecord -v -multi -pad speed=6 dev=0,6,0 /tmp/img.iso 

When the next backup is due, we need to recover the information of the previous session. This is done by inserting the CD and launching mkisofs with the options -C and -M (see the manpage for details):

mkisofs -L -J -R -V Backup_Home -C `cdrecord dev=0,6,0 -msinfo`
-M /dev/scd0 -m '*cache*' -o /tmp/img.iso -graft-points home.20010710/=/home 

mkisoifs will report the size of the final data, including the first session. Verify that it fits on the CD, then burn the disk with the same command as above. If this is the last session on the disk, close the disk by simply omitting the -multi option from cdrecord:

cdrecord -v -pad speed=6 dev=0,6,0 /tmp/img.iso 

If you mount this disk in /cdrom, you will see two directories, /cdrom/home.20010704 and /cdrom/home.20010710, holding the data from the different backups.

Note that there is a certain risk if you use multisession CD-R as your only backup medium: If, for any reason, writing the CD fails, all data on that CD are gone. So, use at least two independent techniques for backup, and verify that you are able to restore your data.

Note also that mkisofs does not follow symbolic links. If you have a symlink called /home/audio that points to /mnt/audio, these data will not be included!

Backup on CD-RW and DVD-RW

Besides the "permanent" backup to tape and/or CD-R media, I use CD-RW to perform quick, systematic backups of the most important system directories, such as /home, /root, /etc and /var.

The script saveset.sh meets several of my requirements: It allows inclusion and exclusion based on patterns, verifies that the data fit on a single CD and that enough disk space for the temporary file exists, it uses relocation to avoid merging of directories, and it preserves access rights. The script also allows to read an external configuration file, e.g. to use with multiple data sets or multiple CD-RW devices. - This script has also been the subject of an article on the Pro-Linux site, written by Hendric Stattmann (in German) and has been extended for use with a DVD writer by the same author.

bullet Download the saveset.sh script. Instructions for configuration are in the file.

bullet Download the saveset-dvd.sh script. Instructions for configuration are in the file.

If you are sure that you regularly exceed the capacity of one disk, you might be interested in the cdtape script below.

Use your CD-RW
like a tape drive

The script cdtape lets you use a CD-RW drive similar to a tape drive. It actually is a filter, redirecting the output from your favourite archive/compression/whatever utility to cdrecord. A few usage examples:

To save a directory to CD: tar cf - /DirectoryToBeSaved | cdtape save
To obtain a listing of the archive: cdtape restore | tar tvf -
To extract a certain file from the archive: cdtape restore | tar xvf - FileToRestore

bullet Click here to download the cdtape script.

Copy CD and DVD media

Copy audio CD

To copy an audio CD which does not have the traditional two seconds of pause between tracks (which is often the case with Live CDs), cdrdao is the software of choice. To copy an audio CD "1:1", first run

cdrdao read-cd --device 0,5,0 --paranoia-mode 3 audio-cd.toc

which writes a binary image file data.bin into the current directory, correcting any scratches or errors as far as possible. To write this image back onto a fresh CD, use

cdrdao write --device 0,6,0 --eject audio-cd.toc

If you want to do a test run before burning, add --simulate. Note that the filesize reported here is not strictly correct as the CD is written in audio format - what counts is the number of blocks writte and this should not exceed 332500.

Recent versions of cdrdao have this functionality built in:

cdrdao copy --source-device ATA:1,1,0 --device ATA:1,0,0 --buffers 64

Copy data CD

So what is the "best" way to copy a data CD?

The problem lies in the extraction of the "raw" data as an ISO image from the CD. Inspired by an article from Steve Litt, I took the first disk from my SuSE Linux 7.3 and ran a number of different commands on this disk.

First of all, some statistics from isoinfo -d -i /dev/scd0:

CD-ROM is in ISO 9660 format
System id: LINUX
Volume id: SU7300.001
[snip]
Volume set size is: 1
Volume set seqence number is: 1
Logical block size is: 2048
Volume size is: 330704
Joliet with UCS level 3 found
Rock Ridge signatures version 1 found

The output of isosize -x /dev/scd0 is coherent with this:

sector count: 330704, sector size: 2048

Then I ran a few different commands that are frequently used to copy disks:

temp/iso> cat /dev/scd0 > ./img_cat.iso
temp/iso> dd if=/dev/scd0 of=./img_dd_bare.iso
1323416+0 records in
1323416+0 records out
temp/iso> dd if=/dev/scd0 of=./img_dd_bare_corr.iso conv=notrunc,noerror
1323416+0 records in
1323416+0 records out
temp/iso> dd if=/dev/scd0 of=./img_dd_bs.iso bs=2048
330854+0 records in
330854+0 records out
temp/iso> dd if=/dev/scd0 of=./img_dd_count_corr.iso  bs=2048 count=330704 conv=notrunc,noerror
330704+0 records in
330704+0 records out
temp/iso> dd if=/dev/scd0 of=./img_dd_count_nocorr.iso bs=2048 count=330704
330704+0 records in
330704+0 records out

Up to this point, all six techniques seem to work fine. But then, a look at the file size (here: in blocks of 2k) reveals a surprise:

temp/iso> ls -s -1 --block-size=2048
total 1655600
331180 img_cat.iso
331180 img_dd_bare.iso
331180 img_dd_bare_corr.iso
331180 img_dd_bs.iso
331030 img_dd_count_corr.iso
331030 img_dd_count_nocorr.iso

None of these files is 330704 2k-blocks in size and there is a difference of 150 blocks (of 2048 bytes each) between the "raw" files and the two files generated using the bs= and count= options!

A comparison all files to the CD-ROM (diff -r $file /dev/scd0 as well as the checksums generated with md5sum show that the latter two files differ from the original CD-ROM (first line):

993b97cd78ce73ac37f5ac8db50824f5  /dev/scd0
993b97cd78ce73ac37f5ac8db50824f5  img_cat.iso
993b97cd78ce73ac37f5ac8db50824f5  img_dd_bare.iso
993b97cd78ce73ac37f5ac8db50824f5  img_dd_bare_corr.iso
993b97cd78ce73ac37f5ac8db50824f5  img_dd_bs.iso
8acfbbfac2b4bdb8113d774ca29b7d5e  img_dd_count_corr.iso
8acfbbfac2b4bdb8113d774ca29b7d5e  img_dd_count_nocorr.iso

It gets even more surprising: The data reported by isoinfo -d -i $file or isosize -x $file do not refer to the complete file but are only the header information! If you don't believe this, then just run one of the two programs on the ISO file while the CD is being extracted, i.e. when the image file is guaranteed not to be complete. The size reported will always be that of the "theoretical" file ... !

Thus, my recommendation to copy a data CD "1:1" is as follows:

  1. use cat or - probably better? - dd on the device, without specifying the block count. It does not matter if the disc is mounted or not.
  2. verify that the data match the original CD using diff -r or md5sum.
  3. when you have successfully verified the identify of the image, burn it to CD-R as described above (cdrecord -v -pad speed=6 dev=0,6,0 /tmp/img.iso ).
  4. verify again that the CD matches the ISO image (or the original CD).

Specialties

Writing mixed-mode CDs

(Note: For the following discussion I assume that "Mixed Mode" is identical with "CD+" (CDplus), i.e. Music tracks with added data. Please correct me if I'm wrong!)

You cannot just write audio and data tracks "mixed" on a CD-R, as (1) an audio CD-Player will identify one more audio track than there are (it's the data track) and (2) the data track is no longer accessible. It is there, but the CD cannot be mounted, neither under Linux nor under Windows! So, here's how to do it:

First, write your audio files onto a multi-session disk:

cdrecord -multi -pad speed=6 dev=0,6,0 -audio *.wav 

Then, retrieve the multi session info:

cdrecord dev=0,6,0 -msinfo 

This will print the session start positions of the last and the next session, generally something like "0,121550". Now, create an ISO filesystem with the data files that includes the above "offset" for the multi-session audio part:

mkisofs -R -J -o /tmp/fs.iso -C 0,121550 temp/ 

In this example, the ISO image will be created in /tmp and we read the data files from a directory "temp" which is located below the actual working directory. We specify -J and -R to obtain a CD that is readable both under Linux and MS-Windows. - mkisofs will issue a warning that option -M was not specified, however, we don't need this feature here - just ignore the message.

The final size reported in the mkisofs output is that of the complete CD, including the audio tracks we recorded before. Note that you cannot "dry-run" mkisofs with the option -print-size in this mode; it will report a wrong size.

Next, burn the image onto the CD. We do not specify "multi" here as we will close the CD:

cdrecord -pad speed=6 dev=0,6,0 -v -data /tmp/fs.iso 

... and you're done. This CD can be played in an audio CD player and be read as a normal data CD in your computer. I could not find any difference between a CD made this way and a commercial CD+ (well, the only one I have ;-).

(Based on Jörg Schilling's pages around cdrecord).

Bootable CD

To create a bootable ISO9660 cdrom image file with Joliet and Rock Ridge support, change directory to the place that will be the root directory of the cd. Then:

mkisofs -v -J -r -T \
-b images/boot.img \  (boot image)
-c boot.catalog \     (boot catalog, will be created by mkisofs)
-p My_Name \          (Preparer, <128 characters, optional)
-A My_boot_CD \       (Application ID, <128 characters, optional)
-V Linuxboot \        (CD ID, optional but recommended)
-o /tmp/img.iso .     (ISO image to be written)

Then, test your new disk image by mounting it. If you forgot to fix file permissions or set the rock ridge extensions then the error will be obvious here since the file names and directory structure will be wrong:

mount -t iso9660 -o ro,loop=/dev/loop0 /tmp/img.iso /cdrom

When you're done, unmount it and burn the disk.

(I captured this somewhere on Internet but I admit I have lost the source!
See also Martin L. Purschkes page about creating your own rescue CD.)

Using an IDE CD-RW

Note: This section is nowadays obsolete since all recent Linux distributions can handle such drives "out of the box". However, I'm leaving this information online for reference purposes.

CD-R and CD-RW drives with SCSI are supported natively by Linux. Using an IDE (ATAPI) CD-RW is not difficult either:

In case you are using Kernel 2.4, the latter procedure also works if you use a "real" SCSI adapter together with an ATAPI-CD-writer. In /etc/init.d/boot.local, add a second line that may look like this:

/sbin/modprobe ide-scsi
/sbin/modprobe aic7xxx

In this example, the second line loads the kernel module for an Adaptec 2940 card after the ide_scsi module. - In such a system, the IDE-SCSI device will show up as host scsi0, and the "real" SCSI adapter will appear as scsi1. Here is the output of cat /proc/scsi/scsi on a system that uses IDE harddisks, an IDE CD-RW plus a SCSI tape:

Attached devices: 
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: PLEXTOR  Model: CD-R   PX-W1610A Rev: 1.03
  Type:   CD-ROM                           ANSI SCSI revision: 02
Host: scsi1 Channel: 00 Id: 03 Lun: 00
  Vendor: Quantum  Model: DLT4000          Rev: CC37
  Type:   Sequential-Access                ANSI SCSI revision: 02