2.8. Using USB Storage
Many USB devices fall into the storage class, including cameras, portable music players, and storage card readers. These devices can easily be used with Fedora.
2.8.1. How Do I Do That?
When you insert a USB storage device while running KDE, the dialog in Figure 2-31 appears with two options: "Open in New Window" and "Do Nothing." Choose one of the options and click OK. If you want to skip this dialog next time you insert a storage device, select the checkbox labeled "Always do this for this type of media."
Figure 2-31. KDE USB Storage action dialog
The action performed when a new USB storage device is detected is configurable in both GNOME and KDE.
18.104.22.168. Safely removing a USB drive
Before unplugging a USB drive, you should unmount it to prevent data loss. In GNOME and KDE, right-click on the drive's desktop icon and select the menu option Unmount Volume or Remove Safely. Wait until the activity lights stop blinking and then unplug the drive.
22.214.171.124. Configuring default actions in GNOME
To configure the action taken when GNOME detects a new USB storage device, select the menu option SystemPreferences"Removable Drives and Media." The window shown in Figure 2-32 will appear.
Figure 2-32. Removable Drives and Media Preferences tool
The first tab, Storage, contains four checkboxes for USB storage devices:
The third tab, Cameras, has a checkbox labeled "Import digital photos when connected." When checked (which is the default), GNOME will look for a directory named dcim on any newly mounted USB media. If found, it will run the specified command (the default is gthumb-import).
126.96.36.199. Configuring default actions in KDE
To configure the behavior of KDE when storage devices are inserted, open the KDE Control Center and select the configuration category PeripheralsStorage Media. The window shown in Figure 2-33 will appear.
Figure 2-33. KDE Removable Media configuration
Select Unmounted Removable Medium in the "Medium types" menu. Two actions will be displayed: "Open in New Window," which mounts the drive and opens a Konqueror browse window, and Do Nothing, which causes a drive icon to be displayed on the desktop, which, when clicked, will mount and browse the drive.
To set one of these actions as the default, click on it, then click "Toggle as Auto Action," and then Apply. The selected action will take place automatically when new media is detected.
2.8.2. How Does It Work?
When a USB storage device is detected by the USB drivers, the hal subsystem takes note and sends a message on the dbus, a messaging system for desktop applications. GNOME or KDE desktop applications listen for messages on the dbus and then perform the action you have configured, such as mounting the drive or displaying the drive contents in a window.
USB devices use a set of data items called descriptors to inform the controlling host of their capabilities. The Class descriptor is used to identify storage devices. These devices, which understand the same commands used to control SCSI disk drives, are given a device name in the form /dev/sd<x> where <x> is a sequential drive letter (sd stands for SCSI disk). Partitions within a USB storage device, if present, are given device names in the form /dev/sd<xp> where <p> is the partition number (1 is the first partition).
$ mount /dev/mapper/main-root on / type ext3 (rw) /dev/proc on /proc type proc (rw) /dev/sys on /sys type sysfs (rw) /dev/devpts on /dev/pts type devpts (rw,gid=5,mode=620) /dev/md0 on /boot type ext3 (rw) /dev/shm on /dev/shm type tmpfs (rw) /dev/mapper/main-home on /home type ext3 (rw) /dev/mapper/main-var on /var type ext3 (rw,acl) /dev/sda on /media/spreadsheet type ext2 (rw,noexec,nosuid,nodev) /dev/sdb on /media/disk type vfat (rw,noexec,nosuid,nodev,shortname=winnt,uid=500)
This particular single USB storage device appears as two separate devices, highlighted in bold in this example: a disk drive, mounted using the filesystem label as the mount point (/media/<fslabel>), and a floppy disk (mounted as /media/disk in the output above). This is a common configuration used on older USB keys; the emulated floppy disk device is intended to store encryption or password software for accessing the main storage device. Removable media is mounted under the /media directory.
A more useful way of looking at the /etc/mtab table is to use df:
# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/main-root 30G 8.9G 20G 32% / /dev/md0 251M 33M 205M 14% /boot /dev/shm 506M 0 506M 0% /dev/shm /dev/mapper/main-home 31G 5.9G 25G 20% /home /dev/mapper/main-var 36G 26G 9.3G 74% /var /dev/sda 120M 1.6M 112M 2% /media/spreadsheet /dev/sdb 1.4M 70K 1.4M 5% /media/disk
/proc/mounts contains the same information as /etc/mtab but is generated directly from the kernel's data structures (and is therefore more reliable).
The kernel uses memory as a buffer, writing data to disk periodically. Unmounting a disk flushes the buffer to disk immediately and updates the disk control structures to indicate that the drive is in a consistent (clean) state. If a drive is removed while mounted, some data (including parts of files) may not be written to the disk, resulting in data corruption.
2.8.3. What About...
188.8.131.52. ...partitioning a flash drive?
# fdisk /dev/sdb
Since fdisk is an interactive tool, it's necessary to enter single-letter commands to specify the changes that should be made to the partition table. First, print the partition table on the screen so you can review it:
Command (m for help): p Disk /dev/sdb: 65 MB, 65536000 bytes 3 heads, 42 sectors/track, 1015 cylinders Units = cylinders of 126 * 512 = 64512 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 1015 63924 83 Linux
This table shows a 64 MB device (64,512 bytes) with one partition.
Delete the old partition:
Command (m for help): d Selected partition 1
Create a new primary partition number 1 that is 30 MB in size:
Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-1015, default 1): ENTER Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-1015, default 1015): +30M
Create a new primary partition number 2, taking up the rest of the drive:
Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (467-1015, default 467): ENTER Using default value 467 Last cylinder or +size or +sizeM or +sizeK (467-1015, default 1015): ENTER Using default value 1015
Print the partition table to check it:
Command (m for help): p Disk /dev/sdb: 65 MB, 65536000 bytes 3 heads, 42 sectors/track, 1015 cylinders Units = cylinders of 126 * 512 = 64512 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 466 29337 83 Linux /dev/sdb2 467 1015 34587 83 Linux
Set the type code for the two partitions:
Command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): L 0 Empty 1e Hidden W95 FAT1 80 Old Minix be Solaris boot 1 FAT12 24 NEC DOS 81 Minix / old Lin bf Solaris 2 XENIX root 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT- ...(snip)... 9 AIX bootable 4f QNX4.x 3rd part 8e Linux LVM df BootIt a OS/2 Boot Manag 50 OnTrack DM 93 Amoeba e1 DOS access b W95 FAT32 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O c W95 FAT32 (LBA) 52 CP/M 9f BSD/OS e4 SpeedStor e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs ...(snip)... 1c Hidden W95 FAT3 75 PC/IX Hex code (type L to list codes): c Changed system type of partition 1 to c (W95 FAT32 (LBA)) Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): c Changed system type of partition 2 to c (W95 FAT32 (LBA))
Write (save) and exit:
Command (m for help): w The partition table has been altered! Calling ioctl( ) to re-read partition table. Syncing disks.
The partition type used, c, indicates that the partition will contain a FAT filesystem. This enables compatibility with Windows and Mac OS X systems and is also necessary for most camera flash-memory cards and digital music players.
# mkfs -t vfat -n spreadsheet -F 32 /dev/sdb1 mkdosfs 2.10 (22 Sep 2003) # mkfs -t vfat -n database -F 32 /dev/sdb2 mkdosfs 2.10 (22 Sep 2003)
The option -F 32 forces the use of 32-bit file allocation tables, which is not strictly necessary for drives under 512 MB in size but is required for larger drives and matches the filesystem type assigned to the partition by the previous fdisk command. The -n labelname option sets the filesystem label, which will be used to determine the mount points for the filesystem.
184.108.40.206. ...using a Linux filesystem such as ext2 on a USB storage device?
# mkfs -t ext3 /dev/sdb2
220.127.116.11. ...accessing USB storage from a nongraphical application?
Automatically mounted storage media are mounted to the directory /media/<label> if the filesystem has a volume label, or to /media/disk<-N> if there is no volume label, where <-N> is a sequentially assigned number (the first disk mounted is simply called /media/disk).
18.104.22.168. ...manually mounting a USB storage device?
# mkdir /mnt/usbdisk1 # mount /dev/sda1 /mnt/usbdisk1
# umount /mnt/usbdisk1
2.8.4. Where Can I Learn More?