How do I add more disk space to a virtual guest using LVM on Red Hat Enterprise Linux?

Release Found: Red Hat Enterprise Linux 5 running one or more virtual guests

Problem

You want to add disk space to a virtual guest using LVM on a Red Hat Enterprise Linux 5 hypervisor.

Solution

First, stop the guest on the Dom0 system and make a backup of the original guest system:

[root@rhel5-dom0 ~]# lvs|grep rhel5.guest
  rhel5u3.guest        vg0  -wi-a-  4.00G
[root@rhel5-dom0 ~]# lvcreate -L 4G -n rhel5.backup vg0
  Logical volume "rhel5.backup" created
[root@rhel5-dom0 ~]# dd if=/dev/vg0/rhel5.guest of=/dev/vg0/rhel5.backup
8388608+0 records in
8388608+0 records out
4294967296 bytes (4.3 GB) copied, 381.72 seconds, 11.3 MB/s

In this example, 2GB will be added to the logical volume of the guest on the dom0 system:

[root@rhel5-dom0 ~] lvextend -L +2G /dev/vg0/rhel5.guest
  Extending logical volume rhel5.guest to 6.00 GB
  Logical volume rhel5.guest successfully resized

Now, start the guest and wait for it to boot.  After it boots, remove the old partition and create a new one that starts in the same place as the old one and spans to the new end of the disk:

[root@rhel5-guest ~]#  fdisk /dev/xvda

Command (m for help): p <---(type "p")

Disk /dev/xvda: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          13      104391   83  Linux
/dev/xvda2              14         522     4088542+  8e  Linux LVM

Notice that "783 cylinders" is the new end of the disk but the original partition for the physical volume only goes to 522 on /dev/xvda2. The second partition will need be be deleted then recreated from the cylinders 14 to 783.

Command (m for help): d <---(type "d")
Partition number (1-4): 2 <---(type "2")

Command (m for help): n <---(type "n")
Command action
e   extended
p   primary partition (1-4)
p <---(type "p")

Partition number (1-4): 2 <---(type "2")
First cylinder (14-783, default 14): 14 <---(type "14")
Last cylinder or +size or +sizeM or +sizeK (14-783, default 783): 783 <---(type "783")

Command (m for help): t <---(type "t")
Partition number (1-4): 2 <---(type "2")
Hex code (type L to list codes): 8e <---(type "8e")
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): p <---(type "p")

Disk /dev/xvda: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          13      104391   83  Linux
/dev/xvda2              14         783     6185025   8e  Linux LVM

Command (m for help): w <---(type "w")
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

Now the guest system will need to be rebooted so the new partition table is read by the kernel.  After this the pvresize command will set the new size of the physical volume in LVM. 

[root@rhel5-guest ~]# pvresize /dev/xvda2
Physical volume "/dev/xvda2" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

You should now see the free space in the volume group using the vgs command under the field VFree:

[root@rhel5-guest ~]# vgs
  VG   #PV #LV #SN Attr   VSize VFree
  vg0    1   2   0 wz--n- 5.88G 2.00G

Determine what logical volume your root filesystem is located on:

[root@rhel5-guest ~]# lvs
  LV   VG   Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  root vg0  -wi-ao   3.62G                                     
  swap vg0  -wi-ao 256.00M

[root@rhel5-guest ~]# blkid
/dev/mapper/vg0-swap: TYPE="swap"
/dev/mapper/vg0-root: UUID="d9623f07-975e-4628-acd8-f6ec67d7afa4" TYPE="ext3"
/dev/xvda1: LABEL="/boot" UUID="8d2d783c-b782-409c-b157-40f4f95220b3" TYPE="ext3"
/dev/vg0/root: UUID="d9623f07-975e-4628-acd8-f6ec67d7afa4" TYPE="ext3"
/dev/vg0/swap: TYPE="swap"

The next step will be to extend the logical volume in the guest itself:

[root@rhel5-guest ~]# lvextend -L +2G /dev/vg0/root
  Extending logical volume root to 5.62 GB
  Logical volume root successfully resized
[root@rhel5-guest ~]# lvs
  LV   VG   Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  root vg0  -wi-ao   5.62G                                     
  swap vg0  -wi-ao 256.00M

The final step will be resizing the ext3 filesystem on the logical volume that was just extended:

[root@rhel5-guest ~]# resize2fs /dev/vg0/root
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/vg0/root is mounted on /; on-line resizing required
Performing an on-line resize of /dev/vg0/root to 4558848 (4k) blocks.
The filesystem on /dev/vg0/root is now 4558848 blocks long.

You should now be able to confirm the new space with the df command:

[root@rhel5-guest ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg0-root  5.5G  1.9G  3.4G  36% /
/dev/xvda1             99M   13M   81M  14% /boot
tmpfs                 513M     0  513M   0% /dev/shm
原文地址:https://www.cnblogs.com/danghuijian/p/4400261.html