• Aucun résultat trouvé

As an administrator, you’ll occasionally need to make storage devices like USB fl ash drives, hard drives, or network shares available. To do this, you need to connect the device to a directory in the root fi le system. This process is known as mounting the device.

If you’re working from the graphical desktop, you’ll notice that devices are mounted automatically. That is, if you take a USB fl ash drive that is formatted with a supported fi le system like Ext4 or FAT, the graphical interface will create a subdirectory in the folder /media and make the contents of the USB drive accessible in that subdirectory. The prob-lem, however, is that this works only from a graphical environment. If you’re behind a server that was started in text mode, you’ll need to mount your devices manually.

c03.indd 83

c03.indd 83 1/8/2013 10:43:12 AM1/8/2013 10:43:12 AM

primary hard drive in your server is known as /dev/sda. However, if your server is con-nected to a SAN, you might have many additional sd devices. lsscsi is a convenient com-mand you can use to fi nd out the current confi guration for your server, but it isn’t installed by default. To install it, use yum install lsscsi.

If the yum install command fails, you first need to set up a repository.

You’ll learn how to do that in Chapter 4, “Managing Software.”

The commands blkid and dmesg are alternative ways to fi nd out the names of storage devices. blkid provides an overview of all block devices currently connected to your com-puter. The last few lines of dmesg show the names of devices that were recently connected to your computer. In Listing 3.1, you can see how dmesg shows that the USB drive that was con-nected to this computer is now known as sdb. So, /dev/sdb is the name of the device in this case. Just stick in the key, and run dmesg; it will show you the device name that is assigned.

Listing 3.1: dmesg shows the name of recently connected block devices

usb 2-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3 usb 2-1.2: Product: Flash Disk

usb 2-1.2: Manufacturer: Usb 2

usb 2-1.2: SerialNumber: 00005655851111ED usb 2-1.2: configuration #1 chosen from 1 choice Initializing USB Mass Storage driver...

scsi6 : SCSI emulation for USB Mass Storage devices usbcore: registered new interface driver usb-storage USB Mass Storage support registered.

usb-storage: device found at 3

usb-storage: waiting for device to settle before scanning usb-storage: device scan complete

scsi 6:0:0:0: Direct-Access Usb 2.0 Flash Disk 2.10 PQ: 0 ANSI: 2 sd 6:0:0:0: Attached scsi generic sg2 type 0

sd 6:0:0:0: [sdb] 4072448 512-byte logical blocks: (2.08 GB/1.94 GiB) sd 6:0:0:0: [sdb] Write Protect is off

sd 6:0:0:0: [sdb] Mode Sense: 0b 00 00 08

sd 6:0:0:0: [sdb] Assuming drive cache: write through sd 6:0:0:0: [sdb] Assuming drive cache: write through sdb:

sd 6:0:0:0: [sdb] Assuming drive cache: write through sd 6:0:0:0: [sdb] Attached SCSI removable disk

SELinux: initialized (dev sdb, type vfat), uses genfs_contexts [root@hnl ~]#

c03.indd 84

c03.indd 84 1/8/2013 10:43:12 AM1/8/2013 10:43:12 AM

there are any partitions on the device. The fdisk -cul command will help you with that.

Assuming that your USB drive is known to your server by the name /dev/sdb, you have to use fdisk -cul dev/sdb to see the current partitioning of the USB drive. Listing 3.2 shows what this looks like.

Listing 3.2: Use fdisk -cul to show partition information

[root@hnl ~]# fdisk -cul /dev/sdb Disk /dev/sdb: 4127 MB, 4127195136 bytes

94 heads, 60 sectors/track, 1429 cylinders, total 8060928 sectors Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x84556ad2

Device Boot Start End Blocks Id System /dev/sdb1 2048 8060927 4029440 83 Linux [root@hnl ~]#

In Listing 3.2, you can see that there is one partition only on /dev/sdb, and it is called /dev/sdb1. Now that you know the name of the partition, you can mount it on a direc-tory. If you want to mount the partition just once, the directory /mnt is an excellent one to host the temporary mount. If you think you’re going to use the mount more than once, you might want to use mkdir to create a dedicated directory for your device. To mount the device /dev/sdb1 on the directory /mnt, you would use the following command:

mount /dev/sdb1 /mnt

At this point, if you use cd to go into the /mnt directory, you’ll see there the contents of the USB drive. You can now treat it as an integrated part of the local fi le system. Also, you can check that it is actually mounted using the mount command (see Listing 3.3). The device you’ve just mounted will be shown last in the list.

Listing 3.3: Use the mount command to display all current mounts

[root@hnl ~]# mount

[root@hnl ~]#

Once you’ve stopped working with the device you’ve just mounted, you need to dis-mount it. To do this, use the umount command. This works only if there are no fi les on the mounted device currently in use. It also means you cannot be in the directory you used as a mount point. After verifying this, use umount followed either by the name of the device that you want to unmount or by the name of the directory you used as a mount point.

For instance, to unmount a device that currently is mounted on /mnt, use umount /mnt. Exercise 3.5 shows how to mount a USB fl ash drive.

E X E R C I S E 3 . 5

Mounting a USB Flash Drive

In this exercise, you’ll learn how to mount a USB fl ash drive. After mounting it success-fully on the /mnt directory, you’ll then dismount it. You’ll also see what happens if there are fi les currently in use while dismounting the device.

1. Open a terminal, and make sure you have root privileges.

2. Insert a USB fl ash drive in the USB port of your computer.

3. Use dmesg to fi nd the device name of the USB fl ash drive. (I’ll assume it is /dev/sdb for the remainder of this exercise.)

4. Use fdisk -cul /dev/sdb to fi nd current partitions on the USB fl ash drive. I’ll assume you’ll fi nd one partition with the name of /dev/sdb1.

5. Use mount /dev/sdb1 /mnt to mount the USB fl ash drive on the /mnt directory.

6. Use cd /mnt to go into the /mnt directory.

7. Type ls to verify that you see the contents of the USB fl ash drive.

8. Now use umount /dev/sdb1 to try to dismount the USB fl ash drive. This won’t work because you still are in the /mnt directory. You’ll see the “device is busy” error mes-sage.

9. Use cd without any arguments. This takes your current shell out of the /mnt directory and back to your home directory.

10. At this point, you’ll be able to dismount the USB fl ash drive successfully using umount /dev/sdb1.

c03.indd 86

c03.indd 86 1/8/2013 10:43:13 AM1/8/2013 10:43:13 AM

Understanding device naming

On a server, normally no graphical desktop is available. That means devices won’t be mounted automatically, and you need to do this by hand. If on a server many storage devices are used, which often is the case in a datacenter environment, your USB key doesn’t automatically become /dev/sdb, but it can be /dev/sdcz. (Once all the letters of the alphabet are used up to /dev/sdz, the next device created is /dev/sdaa.) To fi nd out the name of the device you’ve just attached, dmesg is very useful. In general, it reports on many hardware-related changes that have occurred on your server.