theducks.org

16 Nov, 2009

Expanding LVM Partitions in VMware, on the fly

Posted by: alex in: Gibbering

In my previous post on the subject, I expanded Filesystems on LVM without rebooting, by adding more partitions. As pointed out by Zanchey, there is a much cleaner way to do this, by just resizing existing partitions being used by LVM.

# fdisk /dev/sda

The number of cylinders for this disk is set to 9790.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sda: 80.5 GB, 80530636800 bytes
255 heads, 63 sectors/track, 9790 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 14 104391 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 14 4178 33450007+ 8e Linux LVM
/dev/sda3 4178 8354 33549073+ 8e Linux LVM
/dev/sda4 8355 9137 6289447+ 8e Linux LVM

Command (m for help): q

###
### Extend the VMDK in vCenter
###

# fdisk /dev/sda

The number of cylinders for this disk is set to 9790.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sda: 80.5 GB, 80530636800 bytes
255 heads, 63 sectors/track, 9790 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 14 104391 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 14 4178 33450007+ 8e Linux LVM
/dev/sda3 4178 8354 33549073+ 8e Linux LVM
/dev/sda4 8355 9137 6289447+ 8e Linux LVM

Command (m for help): q

###
### Whuuuuuut? I can't see the extra space!
###

# echo '1' > /sys/class/scsi_disk/0\:0\:0\:0/device/rescan

###
### Rescan device geometry
###

# fdisk /dev/sda

The number of cylinders for this disk is set to 11096.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sda: 91.2 GB, 91268055040 bytes
255 heads, 63 sectors/track, 11096 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 14 104391 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 14 4178 33450007+ 8e Linux LVM
/dev/sda3 4178 8354 33549073+ 8e Linux LVM
/dev/sda4 8355 9137 6289447+ 8e Linux LVM

###
### Ok, that's much better.
###

Command (m for help): d
Partition number (1-4): 4

###
### Delete partition 4
###

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

###
### Now recreate it, so it starts in the same place, and ends at the end of the disk
###

First cylinder (8355-11096, default 8355):
Using default value 8355
Last cylinder or +size or +sizeM or +sizeK (8355-11096, default 11096):
Using default value 11096

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

###
### Remember to change the partition type of Linux LVM. Or you'll be in trouble.
###

Command (m for help): p

Disk /dev/sda: 91.2 GB, 91268055040 bytes
255 heads, 63 sectors/track, 11096 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 14 104391 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 14 4178 33450007+ 8e Linux LVM
/dev/sda3 4178 8354 33549073+ 8e Linux LVM
/dev/sda4 8355 11096 22025115 8e Linux LVM

Command (m for help): w
The partition table has been altered!

###
### Write to disk and away we go
###

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.
# partprobe -s
/dev/sda: msdos partitions 1 2 3 4

###
### Reprobe partition table
###

# pvresize /dev/sda4
Physical volume "/dev/sda4" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

###
### Resize the "filesystem" of the LVM partition we've just extended in fdisk,
### Think of this kind of like resize2fs for LVM, but we'll still have to do that on the logical disks later
###

# vgdisplay
--- Volume group ---
VG Name VGroot
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 8
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 2
Max PV 0
Cur PV 3
Act PV 3
VG Size 84.84 GB
PE Size 32.00 MB
Total PE 2715
Alloc PE / Size 1660 / 51.88 GB
Free PE / Size 1055 / 32.97 GB
VG UUID tSuI0o-Z3mu-mldY-n4pS-qfSZ-g5UP-tXPZlR

###
### Confirm we can now see the extra space in the LVM group
###

# lvdisplay
--- Logical volume ---
LV Name /dev/VGroot/LVroot
VG Name VGroot
LV UUID HYQp9x-yA5H-RLJw-f033-pmEY-Lohg-loRhXa
LV Write Access read/write
LV Status available
# open 1
LV Size 7.88 GB
Current LE 252
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

--- Logical volume ---
LV Name /dev/VGroot/LVswap
VG Name VGroot
LV UUID ZXCxs3-nZd4-jR5S-mkCf-zTJP-yVgp-dpq7Mg
LV Write Access read/write
LV Status available
# open 1
LV Size 4.00 GB
Current LE 128
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1

--- Logical volume ---
LV Name /dev/VGroot/LVut01
VG Name VGroot
LV UUID VUv17I-QbdC-pkyr-bE25-f4MC-gLXq-2QKGr0
LV Write Access read/write
LV Status available
# open 1
LV Size 40.00 GB
Current LE 1280
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2

###
### Confirm how big our logical volumes are within our LVM pool
###

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VGroot-LVroot
7.7G 3.3G 4.0G 45% /
/dev/sda1 99M 24M 71M 26% /boot
tmpfs 1014M 0 1014M 0% /dev/shm
/dev/mapper/VGroot-LVut01
40G 177M 38G 1% /ut01

###
### Confirm amount of space seen on filesystems within the LVM Logical Volumes
###

# lvextend -L+5G /dev/VGroot/LVut01
Extending logical volume LVut01 to 45.00 GB
Logical volume LVut01 successfully resized

###
### Added 5Gb onto our Logical Volume, LVut01
###

# lvdisplay
--- Logical volume ---
LV Name /dev/VGroot/LVroot
VG Name VGroot
LV UUID HYQp9x-yA5H-RLJw-f033-pmEY-Lohg-loRhXa
LV Write Access read/write
LV Status available
# open 1
LV Size 7.88 GB
Current LE 252
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

--- Logical volume ---
LV Name /dev/VGroot/LVswap
VG Name VGroot
LV UUID ZXCxs3-nZd4-jR5S-mkCf-zTJP-yVgp-dpq7Mg
LV Write Access read/write
LV Status available
# open 1
LV Size 4.00 GB
Current LE 128
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1

--- Logical volume ---
LV Name /dev/VGroot/LVut01
VG Name VGroot
LV UUID VUv17I-QbdC-pkyr-bE25-f4MC-gLXq-2QKGr0
LV Write Access read/write
LV Status available
# open 1
LV Size 45.00 GB
Current LE 1440
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VGroot-LVroot
7.7G 3.3G 4.0G 45% /
/dev/sda1 99M 24M 71M 26% /boot
tmpfs 1014M 0 1014M 0% /dev/shm
/dev/mapper/VGroot-LVut01
40G 177M 38G 1% /ut01

###
### So the space is added to the Logical Volume, but the filesystem in the logical volume is not extended
###

# resize2fs /dev/VGroot/LVut01
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VGroot/LVut01 is mounted on /ut01; on-line resizing required
Performing an on-line resize of /dev/VGroot/LVut01 to 11796480 (4k) blocks.
The filesystem on /dev/VGroot/LVut01 is now 11796480 blocks long.

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VGroot-LVroot
7.7G 3.3G 4.0G 45% /
/dev/sda1 99M 24M 71M 26% /boot
tmpfs 1014M 0 1014M 0% /dev/shm
/dev/mapper/VGroot-LVut01
45G 181M 42G 1% /ut01

###
### TADA! All done.
###

This will also work if your root filesystems (“/”) is on LVM, since most of the important stuff is loaded into memory.

Comments

  1. […] 2009-11-16: Use these instructions instead – much cleaner way of doing […]

  2. dionrowney says:

    I tried this but had problems with lvm recognizing the free space after the new partition was created.

    I had to reboot instead of running ‘partprobe -s’ as I was getting this:
    partprobe -s

    Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy). As a result, it may not reflect all of your changes until after reboot.

    Luckily system was okay to reboot, but it would be nice to be able to do this on a running machine. After that the procedure was good.

    I am guessing it was because it was the vg with the root filesystem on it?

  3. alex says:

    Yeah, it looks likes that’s the case.