Chapter 13. Using removable media

Table of Contents

13.1. Initializing and using USB flash drives
13.2. Initializing and using floppy disks
13.3. How to use a ZIP disk
13.4. Reading data CDs with NetBSD
13.5. Reading multi-session CDs with NetBSD
13.6. Allowing normal users to access CDs
13.7. Mounting an ISO image
13.8. Using video CDs with NetBSD
13.9. Using audio CDs with NetBSD
13.10. Creating an MP3 (MPEG layer 3) file from an audio CD
13.11. Using a CD-R writer with data CDs
13.12. Using a CD-R writer to create audio CDs
13.13. Creating an audio CD from MP3s
13.14. Copying an audio CD
13.15. Copying a data CD with two drives
13.16. Using CD-RW rewritables
13.17. DVD support
13.18. Creating ISO images from a CD
13.19. Getting volume information from CDs and ISO images

13.1. Initializing and using USB flash drives

USB flash drives can be used to share data among machines. After attaching it we can see via dmesg(8) that it is recognised as sd0:

# dmesg
[...]
sd0 at scsibus0 target 0 lun 0: <Kingston, DataTraveler 3.0, > disk removable
sd0: fabricating a geometry
sd0: 14755 MB, 14755 cyl, 64 head, 32 sec, 512 bytes/sect x 30218842 sectors
sd0: fabricating a geometry
[...]

Warning

Please note that the following commands will erase all the previous contents on the USB flash drive!

To initialize it we can write zeros in the first 1MB of the USB flash drive:

# dd if=/dev/zero of=/dev/rsd0d bs=1m count=1
1+0 records in
1+0 records out
1048576 bytes transferred in 0.118 secs (8886237 bytes/sec)

Via fdisk(8) we can create a partition table. MS-DOS partition and filesystem is supported by most operating systems and devices that accept an USB disk, so let's update the partition table (-u), creating an MS-DOS partition and set the new partition as active (-a):

# fdisk -au sd0
fdisk: primary partition table invalid, no magic in sector 0
fdisk: Cannot determine the number of heads
Disk: /dev/rsd0d
NetBSD disklabel disk geometry:
cylinders: 14755, heads: 64, sectors/track: 32 (2048 sectors/cylinder)
total sectors: 30218842, bytes/sector: 512

BIOS disk geometry:
cylinders: 1023, heads: 255, sectors/track: 63 (16065 sectors/cylinder)
total sectors: 30218842

Partitions aligned to 16065 sector boundaries, offset 63

Do you want to change our idea of what BIOS thinks? [n] 

Partition table:
0: <UNUSED>
1: <UNUSED>
2: <UNUSED>
3: <UNUSED>
Bootselector disabled.
No active partition.
Drive serial number: 0 (0x00000000)
Which partition do you want to change?: [none] 0
The data for partition 0 is:
<UNUSED>
sysid: [0..255 default: 169] 11
start: [0..1881cyl default: 63, 0cyl, 0MB] 
size: [0..1881cyl default: 30218779, 1881cyl, 14755MB] 
bootmenu: [] (space to clear) 

Partition table:
0: Primary DOS with 32 bit FAT (sysid 11)
    start 63, size 30218779 (14755 MB, Cyls 0-1881/9/10)
        PBR is not bootable: All bytes are identical (0x00)
1: <UNUSED>
2: <UNUSED>
3: <UNUSED>
Bootselector disabled.
No active partition.
Drive serial number: 0 (0x00000000)
Which partition do you want to change?: [none] 
Do you want to change the active partition? [n] y
Choosing 4 will make no partition active.
active partition: [0..4 default: 4] 0
Are you happy with this choice? [n] y

We haven't written the MBR back to disk yet.  This is your last chance.
Partition table:
0: Primary DOS with 32 bit FAT (sysid 11)
    start 63, size 30218779 (14755 MB, Cyls 0-1881/9/10), Active
        PBR is not bootable: All bytes are identical (0x00)
1: <UNUSED>
2: <UNUSED>
3: <UNUSED>
Bootselector disabled.
First active partition: 0
Drive serial number: 0 (0x00000000)
Should we write new partition table? [n] y

Then we can see via disklabel(8):

# disklabel sd0
# /dev/rsd0d:
type: SCSI
disk: DataTraveler 3.0
label: fictitious
flags: removable
bytes/sector: 512
sectors/track: 32
tracks/cylinder: 64
sectors/cylinder: 2048
cylinders: 14755
total sectors: 30218842
rpm: 3600
interleave: 1
trackskew: 0
cylinderskew: 0
headswitch: 0           # microseconds
track-to-track seek: 0  # microseconds
drivedata: 0

5 partitions:
#        size    offset     fstype [fsize bsize cpg/sgs]
 d:  30218842         0     unused      0     0        # (Cyl.      0 -  14755*)
 e:  30218779        63      MSDOS                     # (Cyl.      0*-  14755*)
disklabel: boot block size 0
disklabel: super block size 0

that an sd0e MSDOS partition is present.

We can finally create an MS-DOS filesystem via newfs_msdos(8):

# newfs_msdos /dev/rsd0e
/dev/rsd0e: 30189264 sectors in 1886829 FAT32 clusters (8192 bytes/cluster)
MBR type: 11
bps=512 spc=16 res=32 nft=2 mid=0xf0 spt=32 hds=64 hid=0 bsec=30218779 bspf=14741 rdcl=2 infs=1 bkbs=2

It is ready to be used and mounted via mount_msdos(8).

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

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

13.3. How to use a ZIP disk

  1. See if your system has a ZIP drive:

    # dmesg | grep -i zip
    sd0 at atapibus0 drive 1: <IOMEGA  ZIP 100       ATAPI, , 14.A> type 0 direct removable

    Seems it has one, and it's recognized as sd0, just like any SCSI disk. The fact that the ZIP here is an ATAPI one doesn't matter - a SCSI ZIP will show up here, too. The ZIP is marked as "removable", which means you can eject it with:

    # eject sd0
  2. Insert ZIP disk

  3. Check out what partitions are on the ZIP:

    # disklabel sd0
    # /dev/rsd0d:
    type: ATAPI
     ...
    8 partitions:
    #        size   offset     fstype   [fsize bsize   cpg]
      d:   196608        0     unused        0     0         # (Cyl.    0 - 95)
      h:   196576       32      MSDOS                        # (Cyl.    0*- 95)
    disklabel: boot block size 0
    disklabel: super block size 0
    Partition d

    is the whole disk, as usual on i386.

    Partition h

    is what you want, and you can see it's a msdos filesystem even.

    Hence, use /dev/sd0h to access the zip's partition.

  4. Mount it:

    # mount -t msdos /dev/sd0h /mnt
  5. Access your files:

    # ls -la /mnt
    total 40809
    drwxr-xr-x   1 root  wheel     16384 Dec 31  1979 .
    drwxr-xr-x  28 root  wheel      1024 Aug  2 22:06 ..
    -rwxr-xr-x   1 root  wheel   1474560 Feb 23  1999 boot1.fs
    -rwxr-xr-x   1 root  wheel   1474560 Feb 23  1999 boot2.fs
    -rwxr-xr-x   1 root  wheel    548864 Feb 23  1999 boot3.fs
    -rwxr-xr-x   1 root  wheel  38271173 Feb 23  1999 netbsd19990223.tar.gz
  6. Unmount the ZIP:

    # umount /mnt
    #
  7. Eject the ZIP:

    # eject sd0
    #

13.4. 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[0-9]*:'
         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

    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) and mount_cd9660(8) 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.

13.5. Reading multi-session CDs with NetBSD

Use mscdlabel(8) 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

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

Please also see mount(8) and as an alternative the auto mount daemon amd(8), for which example config files can be found in /usr/share/examples/amd.

13.7. 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) and vnconfig(8) man pages for more information.

13.8. 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 Section 13.4, “Reading data CDs with NetBSD”), then use the multimedia/xine-ui, multimedia/mplayer or multimedia/gmplayer package to play the mpeg files stored on the CD.

13.9. 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), audio/xmcd, "kscd" from the multimedia/kdemultimedia3 package, mixer programs like mixerctl(1), audio/xmix, audio/xmmix, the Curses based audio/cam, or kmix, which is part of multimedia/kdemultimedia3.

    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 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 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 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 Section 13.10, “Creating an MP3 (MPEG layer 3) file from an audio CD”) or by writing them to CD-Rs (see Section 13.12, “Using a CD-R writer to create audio CDs”).

    • To streamline the process, from obtaining audio to populating the metadata for a track to normalising audio and such, the audio/abcde package can be used.

      # abcde -d /dev/rcd0d -o mp3 -p -P

      This will encode the disc track-by-track padding the tracknumbers with a leading 0 and using UNIX pipes to read+encode without leaving the WAV files

13.10. 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) are:

  1. Extract (rip) the audio data of the CD as shown in Section 13.9, “Using audio CDs with NetBSD”.

  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 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 signed 16-bit words with 2 channels at a sampling rate of 44100kHz.

  3. Encode the WAV file into MP3 format.

    • Using the audio/bladeenc 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 128kBit/sec. The documentation for bladeenc describes bit-rates in more detail.

    • Using the audio/lame 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, audio/maplay, audio/mpg123 or audio/splay packages.

13.11. 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 a pre-existing ISO image

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

    Alternatively, you can create a new ISO image yourself:

  2. Generating 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 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 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 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 Section 13.4, “Reading data CDs with NetBSD”.

13.12. 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 Section 13.9, “Using audio CDs with NetBSD” to get a couple of .wav files.

  2. Write the .wav files using cdrecord command from the sysutils/cdrtools package:

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

13.13. 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 Section 13.12, “Using a CD-R writer to create audio CDs”.

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

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

13.16. Using CD-RW rewritables

You can treat a CD-RW drive like a CD-R drive (see Section 13.11, “Using a CD-R writer with data CDs”) 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.

13.17. DVD support

Currently, NetBSD supports ISO 9660 and UDF DVD media. Information about mounting ISO 9660 and UDF filesystems can be found in the mount_cd9660(8) and mount_udf(8) manual pages respectively. DVDs, DivX and many avi files be played with multimedia/ogle or multimedia/gmplayer.

For some hints on creating DVDs, see this postings about growisofs and this article about recording CDs and DVDs with NetBSD.

13.18. 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/

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