Command-line CD-burning

This section requires you to have your system configured to allow CD-burning. If you have not already built and installed a custom kernel, visit our section here and create a CD-burning capable kernel before continuing.

You might ask yourself, "why bother with command-line CD-burning when there are plenty of perfectly good graphical apps out there?" People have their reasons. One very good reason is the ability to automate virtually any command-line process by writing a script. Whatever your reasons, this section will walk you through some basic iso-creation and CD-burning tasks. . . using only the command-line.

Make sure you have your system configured to allow CD-Burning before continuing. You can visit our section on Setting up CD-Burning if you have not done so already.

Also, for these sections you will need to know the Bus ID of your CD/DVD-ROM drive and CD/DVD-Writer. To find this, you can run either the "cdrecord -scanbus" or the "cdrdao scanbus" command. For example:

fongsaiyuk% cdrdao scanbus
Cdrdao version 1.1.9 - (C) Andreas Mueller
  SCSI interface library - (C) Joerg Schilling
  Paranoia DAE library - (C) Monty
 
Check http://cdrdao.sourceforge.net/drives.html#dt for current driver tables.
 
Using libscg version 'schily-0.8'
 
1,0,0 : IDE     , DVD-ROM 16X     , 2.05
1,1,0 : LITE-ON , LTR-52246S      , 6S0D

In this example, "1,0,0", "IDE", is our DVD-ROM drive, and "1,1,0", "LITE-ON", is our CD-Writer. Make note of yours before continuing.

Copying a CD
Creating and burning a data CD
Copying an audio CD
Creating and burning a custom audio CD
Mounting an iso

Copying a CD

Copying a CD is a simple task. First, we need to know the ID of our CD/DVD-ROM drive and CD/DVD-Writer. If you forgot, go ahead and do a "cdrecord -scanbus" to find it.

Our DVD-ROM drive is "1,0,0". Our CD-Writer is "1,1,0". Now that we have this information, insert the CD you wish to copy (your "master CD") in your CD/DVD-ROM drive. Before we copy it, let's create a new directory to temporarily store the image files:

mkdir ~/cd_copy
cd ~/cd_copy

Now, to copy our master CD:

readcd dev=1,0,0 -clone f=super_copy

This will copy your master CD, outputting two files: super_copy and super_copy.toc. (Obviously you can specify something other than "super_copy" and your image files will be named accordingly). If for whatever reason your master CD contains unreadable sectors (not really that uncommon), try using the following command instead:

readcd dev=1,0,0 -clone -nocorr -retries=0 f=super_copy

Either way, once you have your copy, to burn it to a new CD-R,

cdrecord -eject dev=1,1,0 -raw96r -clone -v super_copy

That's it. Feel free to delete super_copy and super_copy.toc, or whatever you named your image files.

In the alternative, if you want to copy a master CD to an .iso file, you would use this command:

dd if=/dev/acd0 of=~/super_copy.iso bs=2048

Please note that this command will only work for creating an .iso of a data CD employing the ISO9660 filesystem. (Nearly all data CDs use this filesystem). If you have a master CD with a different filesystem, use the aforementioned cdrecord commands to copy it instead.

If you want to burn this .iso, use the following command:

cdrecord -eject speed=48 dev=1,1,0 ~/super_copy.iso

Creating and burning a data CD

This is a surprisingly straight-forward task. First, we need to create a directory to store the desired contents of our data CD in. In this example we'll create a directory named "data_cd" in our home directory.

mkdir ~/data_cd

Now we need to copy our desired data into our temporary directory. For example, if you wanted to include your Downloads directory. . .

cp -r ~/Downloads ~/data_cd/

You can also copy individual files in his manner.

cp /etc/rc.conf ~/data_cd/

NOTE that if you wish to copy your home directory, DO NOT create data_cd in your home directory! You'll end up with a never-ending loop as the data_cd subdirectory is copied to itself over and over again. Instead, create data_cd (or whatever you want to call this temporary directory) somewhere other than your home directory. I use /home/tmp, because this is outside of my home directory, but still happily within the /usr slice of my FreeBSD partition. (And thus big enough to hold the contents of my CD).

Once your data_cd directory contains everything you want to burn to CD, a simple command will create an .iso ready to burn. Before running it, cd up one directory level. (If you were in ~/data_cd, a simple "cd .." would suffice). Then:

mkisofs -J -allow-leading-dots -R -V "Data CD" -iso-level 4 -o ./data_cd.iso ./data_cd

This will take the contents of your data_cd directory and create data_cd.iso, a CD image file ready to burn. (If you named your temporary directory containing the files you want to include on your data CD something other than data_cd, you'll want to specify that in place of data_cd. You can name data_cd.iso whatever you want). -V "Data CD" specifies the volume label, which will appear as the name of the CD when you mount it under some other OSes. You can name it whatever you want, as long as it is less than or equal to 32 characters in length.

Now that we have our .iso, we no longer need the data_cd directory we created.

rm -r ~/data_cd

Make sure you specify the correct path to your data_cd directory. "rm-r" is a rather powerful command so you should be very careful using it! Now that we're done with that, burning our .iso to a new CD-R is a simple task. First, make sure your CD-Writer is turned on.

cdrecord -eject speed=48 dev=1,1,0 ~/data_cd.iso

"-eject" tells cdrecord to eject your CD-R after recording is complete. You don't need the option, but I like it. "speed=52" tells cdrecord to burn your CD-R at 52x, or the fastest speed below that available. Obviously if your CD-Writer or CD-R media are only capable of recording at a lower speed, you might want to set that instead, i.e. "speed=48". "dev=1,1,0" is the number we learned from "cdrecord -scanbus". It is the ID of your CD-Writer. (In our example, Lite-on). Last but not least, "~/data_cd.iso" is the path to the .iso image we created earlier in this section. If you created this in a different path or with a different name, change accordingly.

That's it. Go ahead and test out your new CD-R.

Copying an audio CD

First, let's create a temporary directory to hold the songs we want to put on our audio CD.

mkdir ~/audio_cd
cd ~/audio_cd

If you want to copy tracks from a CD you own, insert the CD containing the songs you want. Then, if you do not already know the ID for your CD/DVD-ROM drive, do a "cdrdao scanbus" to find that information.

We want our DVD-ROM drive, which is "1,0,0". (As opposed to our CD-Writer at "1,1,0"). Now that we have this information, it's time to rip the tracks from our CD:

cdrdao read-cd --device 1,0,0 toc-file

It'll start out by generating a track list, then going through and copying each track. When it has finished successfully, you should see a message like this:

Reading of toc and track data finished successfully.

An "ls" of the current directory (~/audio_cd in this example) will reveal that the following two files have been created: data.bin and toc-file. To write to a new CD-R, type the following:

cdrdao write --device 1,1,0 toc-file

"1,1,0" is the ID of our CD-Writer. (Lite-on). This will write us a new audio CD containing the same songs in the same order as our original CD. Once you're finished, feel free to delete your temporary directory:

rm -r ~/audio_cd

Creating and burning a custom audio CD

If you want to create a custom audio CD, you'll need to first extract the CD tracks to .wav files on your system. To do so, you'll need a command-line tool such as cdda2wav. If you followed our CD Burning page, you already installed the cdrtools Port and as a result you already have cdda2wav.

After inserting an audio CD containing your desired songs,

mkdir ~/audio_cd
cd ~/audio_cd
cdda2wav -B dev=1,0,0

This will rip all of the songs off our CD and place them in the current directory, ~/audio_cd. "dev=1,0,0" indicates that we are reading the CD in our DVD-ROM drive, as gleaned from "cdrdao scanbus".

On the other hand, if you have songs stored on your hard drive as .ogg or .mp3 files and want to burn them to an audio CD, we have to first decode the files to .wav. First, we have to copy the desired .ogg and/or .mp3 files to our temporary directory.

cp ~/Music/Disturbed/"The Sickness"/* ~/audio_cd/

Once we have all of our desired songs in the directory, it's time to decode them to .wav format. For this task, we'll need oggdec to covert .ogg files to .wav and lame to convert .mp3 files to .wav. To get oggdec, we need to install the vorbis-tools Port, which contains tools to encode and decode .ogg files. (If you have Grip installed on your system you already have vorbis-tools installed).

cd /usr/ports/audio/vorbis-tools
make install clean

If you have Grip installed, you already have lame as well. To install lame:

cd /usr/ports/audio/lame
make install clean

If your songs are encoded as .ogg files, the following command will decode all files in the current directory to .wav files:

oggdec *.ogg

That's it. Every file ending with the extension .ogg contained in our current directory will be decoded to .wav format. Now, to delete the .ogg files we no longer need,

rm *.ogg

This will delete every file with the extension .ogg in the current directory, leaving the .wav files we created.

If your songs are encoded as .mp3 files, the following will decode your .mp3's to .wav. To convert the file "rise.mp3" to a .wav:

lame --decode rise.mp3

Go ahead and repeat that command, specifying each of the .mp3 files you wish to convert to .wav. Alternatively, if you want to convert all of the .mp3 files in the current directory to .wav files in one fell swoop. . .

for x in *.mp3; do lame --decode $x; done

This will convert every file in the current directory with the extension .mp3 to a .wav.

Once you're finished, delete the .mp3 files you no longer need:

rm *.mp3

This will delete every file with the extension .mp3 in the current directory, leaving the .wav files we created.

Now that we have the .wav files we want to put on our audio CD, it's time to burn them to CD-R.

cdrecord -eject dev=1,1,0 -v -dao -pad -audio *.wav

This will take all of the .wav files in the current directory and burn them to an audio CD. Note that dev=1,1,0 is the ID of our CD-Writer, as gleaned from the "cdrdao scanbus" or "cdrecord -scanbus" command.

Once you've successfully burned your audio CD, feel free to delete your temporary directory:

rm -r ~/audio_cd

Mounting an iso

If you have an .iso file that you want to test before burning or that you just want to grab a file off of, mounting your .iso is pretty straightforward. As root:

mdconfig -a -t vnode -f ~/5.4-RELEASE-i386-disc1.iso -u 0
mount -t cd9660 /dev/md0 /mnt

Treat /mnt as you would any mounted resource. Note that everything will be read-only (you're reading a CD image, after all). When you're finished and want to unmount the iso,

umount /mnt
mdconfig -d -u /dev/md0