File:  [NetBSD Developer Wiki] / wikisrc / guide / rmmedia.mdwn
Revision 1.1: download - view: text, annotated - select for diffs
Sun Mar 10 02:19:55 2013 UTC (13 months, 2 weeks ago) by jdf
Branches: MAIN
CVS tags: HEAD
Removable media Guide -> wiki

# Using removable media

## Reading data CDs with NetBSD

Data CDs can contain anything from programs, sound files (MP3, wav), movies 
(MP3, QuickTime) to source code, text files, etc. Before accessing these files, 
a CD must be mounted on a directory, much like hard disks are. Just as hard 
disks can use different filesystems (ffs, lfs, ext2fs, ...), CDs have their own 
filesystem, *cd9660*. The NetBSD cd9660 filesystem can handle filesystems 
without and with Rockridge and Joliet extensions.

CD devices are named /dev/cd0a for both SCSI and IDE (ATAPI).

With this information, we can start:

 1. See if your system has some CD drive:

        # dmesg | grep ^cd
             cd0 at atapibus0 drive 0: <CD-R/RW RW8040A, , 1.12> type 5 cdrom removable
             cd0: 32-bit data port
             cd0: drive supports PIO mode 4, DMA mode 0
             cd0(pciide0:1:0): using PIO mode 0, DMA mode 0 (using DMA data transfers)

	We have one drive here, "cd0". It is an IDE/ATAPI drive, as it is found on 
	atapibus0. Of course the drive (rather, its medium) is removable, i.e., you 
	can eject it. See below.

 2. Insert a CD

 3. Mount the CD manually:

        # mount -t cd9660 /dev/cd0a /mnt

	This command shouldn't print anything. It instructs the system to mount the 
	CD found on /dev/cd0a on /mnt, using the "cd9660" filesystem. The mountpoint 
	"/mnt" must be an existing directory.

 4. Check the contents of the CD:

        # ls /mnt
        INSTALL.html INSTALL.ps   TRANS.TBL    boot.catalog
        INSTALL.more INSTALL.txt  binary       installation

    Everything looks fine! This is a NetBSD CD, of course. :)

 5. Unmount the CD:

        # umount /mnt

	If the CD is still accessed (e.g. some other shell's still "cd"'d into it), 
	this will not work. If you shut down the system, the CD will be unmounted 
	automatically for you, there's nothing to worry about there.

 6. Making an entry in /etc/fstab:

	If you don't want to type the full "mount" command each time, you can put 
	most of the values into a line in /etc/fstab:

        # Device        mountpoint      filesystem  mount options
        /dev/cd0a       /cdrom          cd9660      ro,noauto    

	Make sure that the mountpoint, `/cdrom` in our example, exists:

        # mkdir /cdrom

	Now you can mount the cd with the following command:

        # mount /cdrom

    Access and unmount as before.

	The CD is not mounted at boot time due to the "noauto" mount option - this 
	is useful as you'll probably not have a CD in the drive all the time. See 
	[mount(8)](http://netbsd.gw.com/cgi-bin/man-cgi?mount+8+NetBSD-5.0.1+i386) 
	and 
	[mount\_cd9660(8)](http://netbsd.gw.com/cgi-bin/man-cgi?mount_cd9660+8+NetBSD-5.0.1+i386) 
	for some other useful options.

7.  Eject the CD:

        # eject cd0

	If the CD is still mounted, it will be unmounted if possible, before being 
	ejected.

## Reading multi-session CDs with NetBSD

Use 
[mscdlabel(8)](http://netbsd.gw.com/cgi-bin/man-cgi?mscdlabel+8+NetBSD-5.0.1+i386) 
to add all sessions to the CDs disklabel, and then use the appropriate device 
node to mount the session you want. You might have to create the corresponding 
device nodes in `/dev` manually. For example:

    # mscdlabel cd1
    track (ctl=4) at sector 142312
     adding as 'a'
    track (ctl=4) at sector 0
     adding as 'b'
    # ls -l /dev/cd1b
    ls: /dev/cd1b: No such file or directory
    # cd /dev
    # ls -l cd1*
    brw-r-----  1 root  operator       6,  8 Mar 18 21:55 cd1a
    brw-r-----  1 root  operator       6, 11 Mar 18 21:55 cd1d
    # mknod cd1b b 6 9

to create `/dev/cd1b`. Make sure you fix the permissions of any new device nodes 
you create:

    # ls -l cd1*
    brw-r-----  1 root  operator       6,  8 Mar 18 21:55 cd1a
    brw-r--r--  1 root  wheel          6,  9 Mar 18 22:23 cd1b
    brw-r-----  1 root  operator       6, 11 Mar 18 21:55 cd1d
    # chgrp operator cd1b
    # chmod 640 cd1b
    # ls -l cd1* 
    brw-r-----  1 root  operator       6,  8 Mar 18 21:55 cd1a
    brw-r-----  1 root  operator       6,  9 Mar 18 22:24 cd1b
    brw-r-----  1 root  operator       6, 11 Mar 18 21:55 cd1d

Now you should be able to mount it.

    # mount /dev/cd1b /mnt

## Allowing normal users to access CDs

By default, NetBSD only allows "root" to mount a filesystem. If you want any 
user to be able to do this, perform the following steps:

 * Give groups and other the access rights to the device.

        # chmod go+rw /dev/cd0a

 * Ask NetBSD to let users mounting filesystems.

        # sysctl -w vfs.generic.usermount=1

	Note that this works for any filesystem and device, not only for CDs with a 
	ISO 9660 filesystem.

To perform the mount operation after these commands, the user must own the mount 
point. So, for example:

    $ cd $HOME
    $ mkdir cdrom
    $ mount -t cd9660 -o nodev,nosuid /dev/cd0a `pwd`/cdrom

*Note*: The mount options `nodev` and `nosuid` are mandatory from NetBSD 4.0 on. 
They are not necessary on NetBSD 3.x systems.

Please also see 
[mount(8)](http://netbsd.gw.com/cgi-bin/man-cgi?mount+8+NetBSD-5.0.1+i386) and 
as an alternative the *auto mount daemon* 
[amd(8)](http://netbsd.gw.com/cgi-bin/man-cgi?amd+8+NetBSD-5.0.1+i386), for 
which example config files can be found in `/usr/share/examples/amd`.

## Mounting an ISO image

Sometimes, it is interesting to mount an ISO9660 image file before you burn the 
CD; this way, you can examine its contents or even copy files to the outside. If 
you are a Linux user, you should know that this is done with the special *loop* 
filesystem. NetBSD does it another way, using the *vnode* pseudo-disk.

We will illustrate how to do this with an example. Suppose you have an ISO image 
in your home directory, called `mycd.iso`:

 1. Start by setting up a new vnode, "pointing" to the ISO file:

        # vnconfig -c vnd0 ~/mycd.iso

 2. Now, mount the vnode:

        # mount -t cd9660 /dev/vnd0a /mnt

 3. Yeah, image contents appear under `/mnt`! Go to that directory and explore 
    the image.

 4. When you are happy, you have to umount the image:

        # umount /mnt

 5. And at last, deconfigure the vnode:

        # vnconfig -u vnd0

Note that these steps can also be used for any kind of file that contains a 
filesystem, not just ISO images.

See the [vnd(4)](http://netbsd.gw.com/cgi-bin/man-cgi?vnd+4+NetBSD-5.0.1+i386) 
and 
[vnconfig(8)](http://netbsd.gw.com/cgi-bin/man-cgi?vnconfig+8+NetBSD-5.0.1+i386) 
man pages for more information.

## Using video CDs with NetBSD

To play MPEG Video streams as many DVD players can play them under NetBSD, mount 
the CD as you would do with any normal (data) CD (see [[Reading data CDs with 
NetBSD|guide/rmmedia#cdrom]]), then use the 
[`multimedia/xine-ui`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/multimedia/xine-ui/README.html), 
[`multimedia/mplayer`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/multimedia/mplayer/README.html) 
or 
[`multimedia/gmplayer`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/multimedia/gmplayer/README.html) 
package to play the mpeg files stored on the CD.

## Using audio CDs with NetBSD

There are two ways to handle audio CDs:

 1. Tell the CD drive to play to the headphone or to a soundcard, to which 
	CDROMs are usually connected internally. Use programs like 
	[cdplay(1)](http://netbsd.gw.com/cgi-bin/man-cgi?cdplay+1+NetBSD-5.0.1+i386), 
	[`audio/xmcd`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/audio/xmcd/README.html), 
	"kscd" from the 
	[`multimedia/kdemultimedia3`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/multimedia/kdemultimedia3/README.html) 
	package, mixer programs like 
	[mixerctl(1)](http://netbsd.gw.com/cgi-bin/man-cgi?mixerctl+1+NetBSD-5.0.1+i386), 
	[`audio/xmix`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/audio/xmix/README.html), 
	[`audio/xmmix`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/audio/xmmix/README.html), 
	the Curses based 
	[`audio/cam`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/audio/cam/README.html), 
	or kmix, which is part of 
	[`multimedia/kdemultimedia3`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/multimedia/kdemultimedia3/README.html).

	This usually works well on both SCSI and IDE (ATAPI) CDROMs, CDRW and DVD 
	drives.

 2. To read ("rip") audio tracks in binary form without going through 
	digital-\>analog conversion and back. There are several programs available 
	to do this:

	 * For most ATAPI, SCSI and several proprietary CDROM drives, the 
	   [`audio/cdparanoia`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/audio/cdparanoia/README.html) 
	   package can be used. With cdparanoia the data can be saved to a file or 
	   directed to standard output in WAV, AIFF, AIFF-C or raw format. Currently 
	   the -g option is required by the NetBSD version of cdparanoia. A 
	   hypothetical example of how to save track 2 as a WAV file is as follows:

           $ cdparanoia -g /dev/rcd0d 2 track-02.wav

       If you want to grab all files from a CD, cdparanoia's batch mode is useful:

           $ cdparanoia -g /dev/rcd0d -B

	 * For ATAPI or SCSI CD-ROMs the 
	   [`audio/cdd`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/audio/cdd/README.html) 
	   package can be used. To extract track 2 with cdd, type:

           # cdd -t 2 `pwd`

       This will put a file called `track-02.cda` in the current directory.

	 * For SCSI CD-ROMS the 
	   [`audio/tosha`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/audio/tosha/README.html) 
	   package can be used. To extract track 2 with tosha, you should be able to 
	   type:

           # tosha -d CD-ROM-device -t 2 -o track-02.cda

	The data can then be post-processed e.g. by encoding it into MP3 streams 
	(see [[Creating an MP3 (MPEG layer 3) file from an audio 
	CD|guide/rmmedia#create-mpeg3]]) or by writing them to CD-Rs (see [[Using a 
	CD-R writer to create audio CDs|guide/rmmedia#cdr-audio]]).

## Creating an MP3 (MPEG layer 3) file from an audio CD

The basic steps in creating an MPEG layer 3 (MP3) file from an audio CD (using 
software from the [NetBSD packages 
collection](http://www.NetBSD.org/docs/pkgsrc/)) are:

 1. Extract (*rip*) the audio data of the CD as shown in
    [[Using audio CDs with NetBSD|guide/rmmedia#cdrom-audio]].

 2. Convert the CD audio format file to WAV format. You only need to perform 
	this job if your ripping program (e.g. tosha, cdd) didn't already do the job 
	for you!

     * Using the [`audio/sox`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/audio/sox/README.html) package, type:

           $ sox -s -w -c 2 -r 44100 -t cdr track-02.cda track-02.wav

	   This will convert `track-02.cda` in raw CD format to `track-02.wav` in 
	   WAV format, using **s**igned 16-bit **w**ords with 2 **c**hannels at a 
	   sampling **r**ate of 44100kHz.

 3. Encode the WAV file into MP3 format.

     * Using the [`audio/bladeenc`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/audio/bladeenc/README.html) package, type:

           $ bladeenc -128 -QUIT track-02.wav

	   This will encode `track-02.wav` into `track-02.mp3` in MP3 format, using 
	   a bit rate if **128**kBit/sec. The documentation for bladeenc describes 
	   bit-rates in more detail.

	 * Using the 
	   [`audio/lame`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/audio/lame/README.html) 
	   package, type:

           $ lame -p -o -v -V 5 -h track-02.wav track-02.mp3

	   You may wish to use a lower quality, depending on your taste and 
	   hardware.

The resultant MP3 file can be played with any of the 
[`audio/gqmpeg`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/audio/gqmpeg/README.html), 
[`audio/maplay`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/audio/maplay/README.html), 
[`audio/mpg123`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/audio/mpg123/README.html) 
or 
[`audio/splay`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/audio/splay/README.html) 
packages.

## Using a CD-R writer with data CDs

The process of writing a CD consists of two steps: First, a "image" of the data 
must be generated, which can then be written to CD-R in a second step.

 1. Reading an pre-existing ISO image

        # dd if=/dev/rcd0a of=filename.iso bs=2k

    Alternatively, you can create a new ISO image yourself:

 2. 2. 2.ating the ISO image

	Put all the data you want to put on CD into one directory. Next you need to 
	generate a disk-like ISO image of your data. The image stores the data in 
	the same form as they're later put on CD, using the ISO 9660 format. The 
	basic ISO9660 format only understands 8+3 filenames (max. eight letters for 
	filename, plus three more for an extension). As this is not practical for 
	Unix filenames, a so-called "Rockridge Extension" needs to be employed to 
	get longer filenames. (A different set of such extension exists in the 
	Microsoft world, to get their long filenames right; that's what's known as 
	Joliet filesystem).

	The ISO image is created using the mkisofs command, which is part of the 
	[`sysutils/cdrtools`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/cdrtools/README.html) 
	package.

	Example: if you have your data in /usr/tmp/data, you can generate a ISO 
	image file in /usr/tmp/data.iso with the following command:

        $ cd /usr/tmp
        $ mkisofs -o data.iso -r data
        Using NETBS000.GZ;1 for  data/binary/kernel/netbsd.INSTALL.gz (netbsd.INSTALL_TINY.gz)
        Using NETBS001.GZ;1 for  data/binary/kernel/netbsd.GENERIC.gz (netbsd.GENERIC_TINY.gz)
          5.92% done, estimate finish Wed Sep 13 21:28:11 2000
         11.83% done, estimate finish Wed Sep 13 21:28:03 2000
         17.74% done, estimate finish Wed Sep 13 21:28:00 2000
         23.64% done, estimate finish Wed Sep 13 21:28:03 2000
         ...
         88.64% done, estimate finish Wed Sep 13 21:27:55 2000
         94.53% done, estimate finish Wed Sep 13 21:27:55 2000
        Total translation table size: 0
        Total rockridge attributes bytes: 5395
        Total directory bytes: 16384
        Path table size(bytes): 110
        Max brk space used 153c4
        84625 extents written (165 Mb)

	Please see the mkisofs(8) man page for other options like noting publisher 
	and preparer. The [Bootable CD ROM 
	How-To](http://www.NetBSD.org/docs/bootcd.html) explains how to generate a 
	bootable CD.

 3. Writing the ISO image to CD-R

	When you have the ISO image file, you just need to write it on a CD. This is 
	done with the "cdrecord" command from the 
	[`sysutils/cdrtools`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/cdrtools/README.html) 
	package. Insert a blank CD-R, and off we go:

        # cdrecord -v dev=/dev/rcd0d data.iso
        ...

	After starting the command, 'cdrecord' shows you a lot of information about 
	your drive, the disk and the image you're about to write. It then does a 10 
	seconds countdown, which is your last chance to stop things - type \^C if 
	you want to abort. If you don't abort, the process will write the whole 
	image to the CD and return with a shell prompt.

	Note that cdrecord(8) works on both SCSI and IDE (ATAPI) drives.

 4. Test

	Mount the just-written CD and test it as you would do with any "normal" CD, 
	see [[Reading data CDs with NetBSD|guide/rmmedia#cdrom]].

## Using a CD-R writer to create audio CDs

If you want to make a backup copy of one of your audio CDs, you can do so by 
extracting ("ripping") the audio tracks from the CD, and then writing them back 
to a blank CD. Of course this also works fine if you only extract single tracks 
from various CDs, creating your very own mix CD!

The steps involved are:

 1. Extract ("rip") the audio tracks as described as in
    [[Using audio CDs with NetBSD|guide/rmmedia#cdrom-audio]]
	to get a couple of .wav files.

 2. Write the .wav files using cdrecord command from the 
    [`sysutils/cdrtools`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/cdrtools/README.html) 
    package:

        # cdrecord -v dev=/dev/rcd0d -audio -pad *.wav

## Creating an audio CD from MP3s

If you have converted all your audio CDs to MP3 and now want to make a mixed CD 
for your (e.g.) your car, you can do so by first converting the .mp3 files back 
to .wav format, then write them as a normal audio CD.

The steps involved here are:

 1. Create .wav files from your .mp3 files:

        $ mpg123 -w foo.wav foo.mp3

	Do this for all of the MP3 files that you want to have on your audio CD. The 
	.wav filenames you use don't matter.

 2. Write the .wav files to CD as described under [[Using a CD-R writer to 
    create audio CDs|guide/rmmedia#cdr-audio]].

## Copying an audio CD

To copy an audio CD while not introducing any pauses as mandated by the CDDA 
standard, you can use cdrdao for that:

    # cdrdao read-cd --device /dev/rcd0d data.toc
    # cdrdao write   --device /dev/rcd1d data.toc

## Copying a data CD with two drives

If you have both a CD-R and a CD-ROM drive in your machine, you can copy a data 
CD with the following command:

    # cdrecord dev=/dev/rcd1d /dev/rcd0d

Here the CD-ROM (cd0) contains the CD you want to copy, and the CD-R (cd1) 
contains the blank disk. Note that this only works with computer disks that 
contain some sort of data, it does *not* work with audio CDs! In practice you'll 
also want to add something like `speed=8` to make things a bit faster.

## Using CD-RW rewritables

You can treat a CD-RW drive like a CD-R drive (see [[Using a CD-R writer with 
data CDs|guide/rmmedia#cdr]]) in NetBSD, creating images with mkisofs(8) and 
writing them on a CD-RW medium with cdrecord(8).

If you want to blank a CD-RW, you can do this with cdrecord's `blank` option:

    # cdrecord dev=/dev/rcd0d blank=fast

There are several other ways to blank the CD-RW, call cdrecord(8) with 
`blank=help` for a list. See the cdrecord(8) man page for more information.

## DVD support

Currently, NetBSD supports DVD media through the ISO 9660 also used for CD-ROMs. 
The new UDF filesystem also present on DVDs has been supported since NetBSD 4.0. 
Information about mounting ISO 9660 and UDF filesystems can be found in the 
[mount\_cd9660(8)](http://netbsd.gw.com/cgi-bin/man-cgi?mount_cd9660+8+NetBSD-5.0.1+i386) 
and 
[mount\_udf(8)](http://netbsd.gw.com/cgi-bin/man-cgi?mount_udf+8+NetBSD-5.0.1+i386) 
manual pages respectively. DVDs, DivX and many avi files be played with 
[`multimedia/ogle`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/multimedia/ogle/README.html) 
or 
[`multimedia/gmplayer`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/multimedia/gmplayer/README.html).

For some hints on creating DVDs, see this [postings about 
growisofs](http://mail-index.NetBSD.org/current-users/2004/01/06/0021.html) and 
[this article about recording CDs and DVDs with 
NetBSD](http://www.mreriksson.net/blog/archive/15/).

## Creating ISO images from a CD

To create an ISO image and save the checksum do this:

    # readcd dev=/dev/cd0d f=/tmp/cd.iso

Here is an alternative using dd(1):

    # dd if=/dev/cd0d of=/tmp/cd.iso bs=2048

If the CD has errors you can recover the rest with this:

    # dd if=/dev/cd0d of=/tmp/cd.iso bs=2048 conv=noerror

To create an ISO image from a mounted data CD first, mount the CD disk by:

    # mount -t cd9660 -r /dev/cd0d /mnt/cdrom

Second, get the image:

    # mkhybrid -v -l -J -R -o /tmp/my_cd.iso /mnt/cdrom/

## Getting volume information from CDs and ISO images

You can read the volume data from an unmounted CD with this command:

    # file -s /dev/cd0d

You can read the volume data from an ISO image with this command:

    # isoinfo -d -i /tmp/my_cd.iso

You can get the unique disk number from an unmounted CD with this:

    # cd-discid /dev/cd0d

You can read the table of contents of an unmounted CD with this command:

    # cdrecord -v dev=/dev/cd0d -toc

## Initializing and using floppy disks

PC-style floppy disks work mostly like other disk devices like hard disks, 
except that you need to low-level format them first. To use an common 1440 KB 
floppy in the first floppy drive, first (as root) format it:

    # fdformat -f /dev/rfd0a

Then create a single partition on the disk using 
[disklabel(8)](http://netbsd.gw.com/cgi-bin/man-cgi?disklabel+8+NetBSD-5.0.1+i386):

    # disklabel -rw /dev/rfd0a floppy3

Creating a small filesystem optimized for space:

    # newfs -m 0 -o space -i 16384 -c 80 /dev/rfd0a

Now the floppy disk can be mounted like any other disk. Or if you already have a 
floppy disk with an MS-DOS filesystem on it that you just want to access from 
NetBSD, you can just do something like this:

    # mount -t msdos /dev/fd0a /mnt

However, rather than using floppies like normal (bigger) disks, it is often more 
convenient to bypass the filesystem altogether and just splat an archive of 
files directly to the raw device. E.g.:

    # tar cvfz /dev/rfd0a file1 file2 ...

A variation of this can also be done with MS-DOS floppies using the 
[`sysutils/mtools`](http://ftp.NetBSD.org/pub/pkgsrc/current/pkgsrc/sysutils/mtools/README.html) 
package which has the benefit of not going through the kernel buffer cache and 
thus not being exposed to the danger of the floppy being removed while a 
filesystem is mounted on it.


CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb