• Aucun résultat trouvé

To create logical volumes, you need to set up three different parts. The fi rst part is the physical volume (PV). The physical volume is the actual storage device you want to use in your LVM confi guration. This can be a LUN on the SAN, an entire disk, or a partition. If it is a partition, you’ll need to create it as one marked with the 8e partition type. After that, you can use pvcreate to create the physical volume. Using this command is easy: the only mandatory argument specifi es the name of the device you want to use, as in pvcreate /dev/sdb3.

The next step consists of setting up the volume group (VG). The volume group is the col-lection of all the storage devices you want to use in an LVM confi guration. You’ll see the total amount of storage in the volume group while you create the logical volumes in the next step.

You’ll use the vgcreate command to create the volume group. For example, use vgcreate mygroup /dev/sdb3 to set up a volume group that uses /dev/sdb3 as its physical volume.

c05.indd 139

c05.indd 139 1/8/2013 10:44:25 AM1/8/2013 10:44:25 AM

size to stipulate for the logical volume. To specify the size, you can use -L to specify the size in kilo, mega, giga, tera, exa, or petabytes. Alternatively, you can use -l to specify

the size in extents. The extent is the basic building block of the LVM logical volume, and it typically has a size of 4MB. Another very handy way to specify the size of the volume is by using -l 100%FREE, which uses all available extents in the volume group. An example of the command lvcreate is lvcreate -n myvol -L 100M mygroup, which creates a 100MB volume in the group mygroup.

In Figure 5.2, you can see a schematic overview of the way LVM is organized.

F I G U R E 5 . 2 LVM schematic overview Exercise 5.6 shows how to create LVM logical volumes.

E X E R C I S E 5 . 6

Creating LVM Logical Volumes

In this exercise, you’ll learn how to create LVM logical volumes. First you’ll create a parti-tion of partiparti-tion type 8e. Next you’ll use pvcreate to mark this partiparti-tion as an LVM physi-cal volume. After doing that, you can use vgcreate to create the volume group. As the last step of the procedure, you’ll use lvcreate to set up the LVM logical volume. In this exercise, you’ll continue to work on the /dev/sdb device you worked with in previous exercises in this chapter.

1. From a root shell, type fdisk -cul /dev/sdb. This should show the current partitioning of /dev/sdb, as in the example shown in Listing 5.10. You should have available disk space in the extended partition that you can see because the last sector in the extended partition is far beyond the last sector of the logical partition /dev/sdb5.

c05.indd 140

c05.indd 140 1/8/2013 10:44:26 AM1/8/2013 10:44:26 AM

Listing 5.10: Displaying current partitioning

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

32 heads, 62 sectors/track, 1011 cylinders, total 2007040 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: 0x3f075c76

2. Type fdisk -cu /dev/sdb to open the fdisk interface. Now type n to create a new partition, and choose l for a logical partition. Next press Enter to select the default starting sector for this partition, and then type +500M to make this a 500MB partition.

3. Before writing the changes to disk, type t to change the partition type. When asked for the partition number, enter 6. When asked for the partition type, enter 8e. Next type p to print the current partitioning. Then type w to write the changes to disk. If you get an error message, reboot your server to update the kernel with the changes.

In Listing 5.11 below you can see the entire procedure of adding a logical partition with the LVM partition type.

Listing 5.11: Adding a logical partition with the LVM partition type

[root@hnl ~]# fdisk -cu /dev/sdb

Last sector, +sectors or +size{K,M,G} (735232-2007039, default 2007039): +200M

Command (m for help): t Partition number (1-6): 6

c05.indd 141

c05.indd 141 1/8/2013 10:44:26 AM1/8/2013 10:44:26 AM

Hex code (type L to list codes): 8e

Changed system type of partition 6 to 8e (Linux LVM) Command (m for help): p

Disk /dev/sdb: 1027 MB, 1027604480 bytes

32 heads, 62 sectors/track, 1011 cylinders, total 2007040 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: 0x3f075c76

Device Boot Start End Blocks Id System /dev/sdb1 2048 526335 262144 83 Linux /dev/sdb2 526336 2007039 740352 5 Extended /dev/sdb5 528384 733183 102400 83 Linux /dev/sdb6 735232 1144831 204800 8e Linux LVM Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

3. Now that you have created a partition and marked it as partition type 8e, use pvcreate /dev/sdb to convert it into an LVM physical volume. You will now see a message that the physical volume has been created successfully.

4. To create a volume group with the name usbvg and to put the physical volume /dev/

sdb6 in it, use the command vgcreate usbvg /dev/sdb6.

5. Now that you have created a volume group that contains the physical volume on /dev/sdb6, use lvcreate -n usbvol -L 100M usbvg. This creates a logical volume that uses 50 percent of available disk space in the volume group.

6. To confi rm that the logical volume has been created successfully, you can type the lvs command, which summarizes all currently existing logical volumes. Listing 5.12 shows the result of this command.

c05.indd 142

c05.indd 142 1/8/2013 10:44:26 AM1/8/2013 10:44:26 AM

Listing 5.12: Displaying currently existing LVM logical volumes

[root@hnl ~]# lvcreate -n usbvol -L 100M usbvg Logical volume "usbvol" created

[root@hnl ~]# lvs

LV VG Attr LSize Origin Snap% Move Log Copy% Convert usbvol usbvg -wi-a- 100.00m

lv_home vg_hnl -wi-ao 11.00g lv_root vg_hnl -wi-ao 50.00g lv_swap vg_hnl -wi-ao 9.72g

7. Now that you have created the logical volume, you’re ready to put a fi le system on it.

Use mkfs.ext4 /dev/usbvg/usbvol to format the volume with an Ext4 fi le system.

While working with logical volumes, it is important to know which device name to use.

By default, every LVM logical volume has a device name that is structured as /dev/name-of-vg/name-of-lv, like /dev/usbvg/usbvol in the preceding exercise.

An alternative name that exists by default for every LVM volume is in the /dev/mapper directory. There you’ll fi nd every logical volume with a name that is structured as /dev/mapper /vgname_lvname. This means the volume you created in the exercise will also be visible as /dev/mapper/usbvg-subvol. You can use either of these names to address the logical volume.

While managing LVM from the command line gives you many more options and pos-sibilities, you can also use the graphical tool system-confi g-lvm, which offers an easy-to-use graphical interface for LVM management. You will probably miss some features, however, when you use this tool. Figure 5.3 shows the system-confi g-lvm interface.