On Linux and UNIX® systems, all files are accessed as part of a single large tree that is rooted at /. To access the files on a CD-ROM, you need to mount the CD-ROM device at some mount point in the file tree. On current distributions, this is usually automated for you; you need only insert the disc and it will be recognized and mounted. Once mounted, it is important to properly unmount the device to avoid data loss.
When you insert a CD or DVD disc into a SUSE 11.3 system, or attach a USB drive, you will see a pop-up window similar to Figure 21. If you miss the pop-up before it closes, you can use the panel icon that we've shown in the top left of the figure to open it again. Hovering over the icon shows the tooltip indicating that the device is not yet mounted.
Figure 21. Pop-up when CD or DVD loaded on SUSE system
Hovering over the plug icon to the right of the image reveals a tooltip saying "Click to access this device from other applications". Clicking over the text "4 actions for this device" expands the image to look like Figure 22. The available actions may differ on your system if you have installed different software packages.
Figure 22. Available actions for CD or DVD
If you click the "Open with File Manager" selection, you will see a window something like Figure 23. From this window you can navigate around the DVD, open files, or run applications. Hover over an item to see a description in the right part of the window.
Figure 23. SUSE File Manager
On Ubuntu and Fedora systems, the default action on inserting a disc is slightly different. Usually, an icon similar to that shown in Figure 24 will appear on your desktop. The file manager (Nautilus) may also open automatically. If it does not, you can double-click the icon to open the file manager. Depending on your system, you may also be prompted as to whether to run the autorun file in the root of the disc or not.
Figure 24. Inserted CD on Ubuntu
If you clicked to access the device from other applications (OpenSUSE),
or opened it with File Manager or another application on any
distribution, then the device will have been mounted for you. It will
usually be mounted in the /media directory and will probably use the
disc label as a mount point. You can verify by opening a terminal
window and running the
mount command as
shown for our OpenSUSE system in Listing 11. Other systems are very similar,
although the mount options may differ slightly.
Listing 11. Using the mount command
ian@lyrebird:~> mount /dev/sdb12 on / type ext4 (rw,acl,user_xattr) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) ... /dev/sr0 on /media/openSUSE-DVD-x86_64.0702..001 type iso9660 (ro,nosuid,nodev,uid=1000, utf8)
In this case our disc is mounted at /media/openSUSE-DVD-x86_64.0702..001 and you can explore it or change to a directory on the disc from the terminal window. Note that the CD/DVD device is /dev/sr0. Devices on a Linux system also appear in the filesystem tree. To put this another way, the filesystem that is on the media in the device /dev/sr0 is accessible to applications starting at the mount point /media/openSUSE-DVD-x86_64.0702..001. If you'd like to learn more about how devices are mounted, see our article Learn Linux, 101: Control mounting and unmounting of filesystems.
Once a DVD or CD is mounted, you can use the files on the disk as they are now part of your file system. While a CD-ROM is mounted, Linux will lock the CD so that it cannot be ejected with the Eject button. Of course, this doesn't stop you from unplugging an external CD or USB drive, pulling a USB stick out of the USB socket, or ejecting a floppy disk. To avoid potential data loss, you should always remove the device safely by first unmounting it.
You can unmount devices from the graphical desktop, or from the command
umount command (note umount
without an 'n') unmounts a device, and the
eject command first unmounts a device and then
tries to eject it, for example by opening a CD drawer. Traditionally,
mounting and unmounting devices required root authority. Nowadays, it
is common to allow user mounting, so that a non-root user is
able to mount and unmount devices. You may have noticed back in Listing 11 the option
uid=1000. The uid of 1000 corresponds to
user ian on this system as shown in Listing 12.
Listing 12. Using the id command for the current user
iian@lyrebird:~> id uid=1000(ian) gid=100(users) groups=33(video),100(users)
If you use the graphical desktop tools to unmount the device, you may only need to worry about ids if you have logged out and then back in as a different user. So let's look at the graphical tools, then the command line ones.
On Ubuntu or Fedora, if you right click on the icon for the removable media you will see a context menu similar to that in Figure 25. You will have an option to either unmount or eject the device. Select the eject option for a CD or DVD and the device will be unmounted, the icon will disappear from your desktop, and your drawer will open. For a USB drive, the option may be unmount rather than eject, and you can safely unplug the device after it is unmounted. If you use an external hard drive with multiple partitions, you need to make sure that all partitions are unmounted before removing or powering off the drive.
Figure 25. SUSE File Manager
In Figure 26 we show our OpenSUSE system with an attached USB drive as well as the DVD we had used earlier. The DVD and one partition of the hard drive are mounted. OpenSUSE distinguishes the mounted from the unmounted by changing the plug icon (when a device is not mounted) to an eject button (when it is mounted). You will also note that the small square at the bottom left of the device icon also changes from having a diagonal line to having an asterisk. Note that for writeable media, such as our hard drive, the available space is also displayed.
Figure 26. OpenSUSE File Manager
To unmount or eject a mounted drive, simply click the eject button (shown above for the 2006R1 partition). If you eject a device such as a CD or DVD where a drawer opens or some other similar physical disconnection takes place, then the device will be removed from your list of plugged devices.
We mentioned the
eject commands earlier. Let's now look at
how to use them and the
lsof that you may
also want to know about. You will find some differences between
systems in this area, so be prepared for things not to be exactly as
we illustrate here.
To begin, we'll look at using the
command to unmount the CD on our Ubuntu system as shown in Listing 13. We first use
grep to filter out the entries from the
mount to show only those entries
that contain 'media', the usual mount point for removable media. We
umount to unmount the device using
its mount point (/media/Ubuntu 10.10 i386). Finally, we
repeat the mount + grep filter to confirm that the device is no longer
mounted. Note that you can use either the mount point or the device
name (/dev/sr0 in this case) as the argument to
Listing 13. Unmounting a CD on Ubuntu from the command line
ian@pinguino:~$ mount | grep media /dev/sr0 on /media/Ubuntu 10.10 i386 type iso9660 (ro,nosuid,nodev,uhelper=udisks, uid=1000,gid=1000,iocharset=utf8,mode=0400,dmode=0500) ian@pinguino:~$ umount /media/Ubuntu\ 10.10\ i386 ian@pinguino:~$ mount | grep media
As you can see, Ubuntu allows you to use the command line to unmount a drive that was automatically mounted. Repeating the above scenario on OpenSUSE 11.3 is likely to result in something like Listing 14.
Listing 14. Unmounting a CD on OpenSUSE from the command line (1)
ian@lyrebird:~> mount | grep media /dev/sr0 on /media/openSUSE-DVD-x86_64.0702..001 type iso9660 (ro,nosuid,nodev,uid=1000, utf8) /dev/sdc6 on /media/2006R1 type ext3 (rw,nosuid,nodev) ian@lyrebird:~> umount /media/openSUSE-DVD-x86_64.0702..001/ umount: /media/openSUSE-DVD-x86_64.0702..001 is not in the fstab (and you are not root) ian@lyrebird:~> umount /dev/sr0 umount: /dev/sr0 is not in the fstab (and you are not root)
Sometimes you will find a disconnect between what you can do
graphically and what you might expect to do at a command line. As
Linux matures, such disconnects become less frequent, but you will
sometimes find them as we have done here. The obvious solution, given
what you have already learned in this tutorial, is to use
run the required command with root privileges. So let's try it using
su as shown in Listing 15.
Listing 15. Unmounting a CD on OpenSUSE from the command line (2)
ian@lyrebird:~> su - Password: lyrebird:~ # mount | grep media /dev/sr0 on /media/openSUSE-DVD-x86_64.0702..001 type iso9660 (ro,nosuid,nodev,uid=1000, utf8) /dev/sdc6 on /media/2006R1 type ext3 (rw,nosuid,nodev) lyrebird:~ # umount /dev/sdc6 lyrebird:~ # umount /media/openSUSE-DVD-x86_64.0702..001/ umount: /media/openSUSE-DVD-x86_64.0702..001: device is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1))
We were able to unmount /dev/sdc6 successfully, but were unable to
unmount /media/openSUSE-DVD-x86_64.0702..001 because Linux claimed it
was busy. We would get a similar error message if we tried to use the
eject command. Remember that we said that
Linux will lock a CD or DVD closed while it is in use. More generally,
you cannot unmount a filesystem if it is in use by some other user. As
suggested in the error message above, you can use the
command to find which user is causing your unmount problem. Typical usage
is illustrated in Listing 16.
Listing 16. Using lsof and fuser (as root)
lyrebird:~ # lsof /media/openSUSE-DVD-x86_64.0702..001/ COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME bash 3824 ian cwd DIR 11,0 2048 2048 /media/openSUSE-DVD-x86_64.0702..001/ boot lyrebird:~ # fuser -um /media/openSUSE-DVD-x86_64.0702..001/ /media/openSUSE-DVD-x86_64.0702..001: 3824c(ian)
lsof command shows the actual file or
directory (/media/openSUSE-DVD-x86_64.0702..001/boot in this case)
that is open as well as the process id (3824) and user id (ian) using
the file or directory. The
shows the user and process id using the
/media/openSUSE-DVD-x86_64.0702..001 filesystem, but not the specific
open file or directory.
Normally you would use this information to cleanly close the open
process or window using the filesystem. On some Linux systems there is
-L option to
umount that allows a lazy unmount
where the filesystem is detached from the filesystem hierarchy
immediately, and all references to the filesystem are cleaned up when
the filesystem is no longer busy. On OpenSUSE 11.3 this option is not
available, so you need to clean up the filesystem references, either
by terminating the offending processes or by ensuring they are no longer
using affected resources, before unmounting.
We mentioned back in Listing 14 that the OpenSUSE system does not
support command line use of
for a device that was mounted from
the desktop, so we attacked the problem by using root authority. There
is another approach using the
command to unmount the device. HAL is a Hardware Abstraction
Layer, which provides a consistent application interface to various
hardware devices. If you use
will still be unable to unmount a busy device. Listing 17 shows how to use
halmount and then switch to
root to forcibly terminate the process that
is keeping the disc busy. Note that killing the process in this way
may cause data loss, so we do not recommend it unless you are sure you
will not lose data. We then switch back to normal user mode and use
halmount to unmount the filesystem that is
no longer busy. Finally we use
open the drawer and eject the DVD.
Listing 17. Using halmount, kill, and eject
ian@lyrebird:~> halmount -u /dev/sr0 /dev/sr0: org.freedesktop.Hal.Device.Volume.Busy: umount: /media/openSUSE-DVD-x86_64.0702 ..001: device is busy. ian@lyrebird:~> lsof /dev/sr0 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME bash 3824 ian cwd DIR 11,0 2048 2048 /media/openSUSE-DVD-x86_64.0702..001/ boot ian@lyrebird:~> su - Password: lyrebird:~ # kill -9 3824 lyrebird:~ # ps -ef | grep 3824 root 5542 5507 0 22:47 pts/2 00:00:00 grep 3824 lyrebird:~ # logout ian@lyrebird:~> halmount -u /dev/sr0 ian@lyrebird:~> eject /dev/sr0
We hope this brief introduction to removable media use on Linux will help you enjoy your removable media files and avoid data loss.