• Aucun résultat trouvé

One of the advantages of working with LVM is that you can resize volumes if you’re out of disk space. That goes both ways: you can extend a volume that has become too small, and you can shrink a volume if you need to offer some of the disk space somewhere else.

When resizing logical volumes, you always have to resize the fi le system that is on it as well. If you are extending a logical volume, you will fi rst extend the volume itself, and then you can extend the fi le system that is on it. When you reduce a logical volume, you fi rst need to reduce the fi le system before you can reduce the size of the logical volume. To resize any Ext fi le system (Ext2, Ext3, or Ext4), you can use resize2fs.

Sometimes you’ll need to extend the volume group before you can extend a logical volume.

This occurs when you have allocated all available disk space in the volume group previously.

To extend a volume group, you have to add new physical volumes to it.

c05.indd 143

c05.indd 143 1/8/2013 10:44:27 AM1/8/2013 10:44:27 AM

Download from Wow! eBook <www.wowebook.com>

Extending a logical volume if there are still unallocated extents in the volume group.

Extending a logical volume if there are no longer any unallocated extents in the volume group. When this occurs, you’ll need to extend the volume group first.

Shrinking a logical volume.

F I G U R E 5 . 3 The system-config-lvm tool allows you to manage LVM from a graphical interface.

In the following three exercises (Exercises 5.7 through 5.9), you’ll learn how to perform these procedures.

E X E R C I S E 5 . 7

Extending a Logical Volume

In this exercise, you’ll extend the logical volume you created in Exercise 5.6. At this point, there still is unallocated space available in the volume group, so you just have to grow the logical volume. After that, you need to extend the Ext fi le system as well.

1. Type vgs to get an overview of the current volume groups. If you’ve succeeded in the preceding exercises, you’ll have a VG with the name usbvg that still has 96MB of unassigned disk space. Listing 5.13 shows the result of this.

c05.indd 144

c05.indd 144 1/8/2013 10:44:27 AM1/8/2013 10:44:27 AM

Listing 5.13: Checking available disk space in volume groups

2. Use lvextend -l +100%FREE /dev/usbvg/usbvol. This command adds 100 percent of all free extents to the usbvol logical volume and tells you that it now has a size of 196MB.

3. Type resize2fs /dev/usbvg/usbvol. This extends the fi le system on the logical vol-ume to the current size of the logical volvol-ume.

In the previous exercise, you learned how to extend a logical volume that is in a VG that still has unallocated extents. Unfortunately, it won’t be always that easy. In many cases, the volume group will no longer have unallocated extents, which means you fi rst need to extend it by adding a physical volume to it. The next procedure shows how to do this.

E X E R C I S E 5 . 8

Extending a Volume Group

If you want to extend a logical volume and you don’t have unallocated extents in the vol-ume group, you fi rst need to create a physical volvol-ume and add that to the volvol-ume group.

This exercise describes how to do this.

1. Use the vgs command to confi rm that VFree indicates that no unallocated disk space is available.

2. Use the procedure that you learned earlier to create a logical partition called /dev/

sdb7 that has a size of 100MB. Remember to set the partition type to 8e. Write the changes to disk, and when fdisk indicates that rereading the partition table has failed, reboot your server.

3. Use vgextend usbvg /dev/sdb7 to extend the volume group with the physical vol-ume you just created. To confi rm that you were successful, type vgs, which now shows that there are 96MB of available disk space within the VG. Listing 5.14 shows the results of performing these steps.

Listing 5.14: Extending a volume group

[root@hnl ~]# vgextend usbvg /dev/sdb7 No physical volume label read from /dev/sdb7

c05.indd 145

c05.indd 145 1/8/2013 10:44:28 AM1/8/2013 10:44:28 AM

Writing physical volume data to disk "/dev/sdb7"

Physical volume "/dev/sdb7" successfully created Volume group "usbvg" successfully extended [root@hnl ~]# vgs

VG #PV #LV #SN Attr VSize VFree usbvg 2 1 0 wz--n- 292.00m 96.00m vg_hnl 1 3 0 wz--n- 232.39g 161.68g

In the preceding exercise, you extended a volume group. At this point, you can grow any of the logical volumes in the volume group. You learned how to do that in Exercise 5.8, and therefore that procedure won’t be repeated here.

E X E R C I S E 5 . 9

Reducing a Logical Volume

If you need to reduce a logical volume, you fi rst have to reduce the fi le system that is on it. You can do that only on an unmounted fi le system that has been checked previously.

This exercise describes the procedure that you have to apply in this situation.

1. Before shrinking an LVM logical volume, you fi rst must reduce the size of the fi le sys-tem. Before reducing the size of the fi le system, you must unmount the fi le system and check its integrity. To do so, use umount /dev/usbvg/usbvol and use e2fsck -f /dev/usbvg/usbvol to check its integrity.

2. Once the check is completed, use resize2fs /dev/usbvg/usbvol 100M to shrink the fi le system on the volume to 100MB.

3. Use lvreduce -L 100M /dev/usbvg/usbvol to reduce the size of the volume to 100MB as well. Once completed, you can now safely mount the reduced volume.