How to resize an LVM physical volume without a reboot

The essential information -that I lacked- was here: "Avoid Reboot after Partition Change with Fdisk". I've found references to the /sys/block/sda/device/rescan Sysfs entry too, but it seemed not to help. The partprobe command on the other hand worked like a charm.

Now for a detailed example ...
Let's assume you've an LVM volume group named vol01 that has a physical volume named /dev/sda2. You still have free space on the /dev/sda disk and you'd like to increase the size of the sda2 partition and extend the associated physical volume ... thus increasing the free space in your volume group.

Note that this is not for beginners. One small mistake and you can easily lose all your data on the given volume group!

Here're the commands to achieve that pv expansion without a reboot:
# First unmount everything that uses the given volume group
# List all the logical volumes that are in vol01
$ lvdisplay /dev/vol01
  --- Logical volume ---
  LV Name                /dev/vol01/lv01
  VG Name                vol01
  LV UUID                q3eQP3-4V9E-7yvo-LUZR-zbXz-F2Mh-Pn5fDt
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                139.70 GB
  Current LE             35762
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0
   
  --- Logical volume ---
  LV Name                /dev/vol01/lv02
  VG Name                vol01
  LV UUID                b53h7W-VO2U-3Ok5-WvW3-GbDp-Tbvb-6bbdkw
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                29.31 GB
  Current LE             7504
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:1

# Now unmount all these volumes (if they are mounted)
$ umount /dev/vol01/lv01
$ umount /dev/vol01/lv02
# In case you get a "device is busy" error, check out what processes use them (lsof /dev/vol01/lv01) and stop/kill them.
# Now deactive the volume group.
$ vgchange -a n vol01
# Increase the size of the sda2 partition to the desired value.
# You do this by deleting the partition and recreating it with a higher end cylinder.
# I assume that pvresize can handle partition expansions only if the partition start remains the same and the partition end cylinder is moved to a higher value.
$ fdisk /dev/sda

The number of cylinders for this disk is set to 60801.
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: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000e4bad

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1         486     3903763+  83  Linux
/dev/sda2             487       18722   146480670   8e  Linux LVM

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

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (487-60801, default 487):
Using default value 487
Last cylinder or +size or +sizeM or +sizeK (487-60801, default 60801):
Using default value 60801

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

Command (m for help): p

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000e4bad

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1         486     3903763+  83  Linux
/dev/sda2             487       60801   484480237+  8e  Linux LVM

Command (m for help): 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 comes the interesting part: we force the system with partprobe to re-read the partition table.
$ pvdisplay /dev/sda2
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               vol01
  PV Size               139.69 GB / not usable 3.53 MB
  Allocatable           yes (but full)
  PE Size (KByte)       4096
  Total PE              35761
  Free PE               0
  Allocated PE          35761
  PV UUID               S0cDcl-7mr8-2AAb-172u-HClq-J2aQ-DfC2V5
$ partprobe
$ pvresize /dev/sda2
$ pvdisplay /dev/sda2
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               vol01
  PV Size               462.04 GB / not usable 1.04 MB
  Allocatable           yes
  PE Size (KByte)       4096
  Total PE              118281
  Free PE               82520
  Allocated PE          35761
  PV UUID               S0cDcl-7mr8-2AAb-172u-HClq-J2aQ-DfC2V5
# Voala! :-) The physical volume got increased.
# Time to reactivate the volume group.
$ vgchange -a y vol01
# Now check the volume group details.
$ vgdisplay vol01
  --- Volume group ---
  VG Name               vol01
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  18
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               927.79 GB
  PE Size               4.00 MB
  Total PE              237515
  Alloc PE / Size       68866 / 269.01 GB
  Free  PE / Size       168649 / 658.79 GB
  VG UUID               0eY2tg-AOny-XsDL-wfLR-d290-2jxO-TDTNnQ
# You can now use lvextend to increase logical volumes in the volume group.
$ lvextend -L +10G --name lv01 /dev/vol01
# And resize2fs to extend the Ext2/3 filesystem on the logical volume.
$ resize2fs /dev/vol01/lv01
# And finally mount the filesystems.
$ mount /dev/vol01/lv01
$ mount /dev/vol01/lv02
# You're now ready.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

What to do if partprobe also suffers from Device Busy?

# partprobe -s

Error: Error informing the kernel about modifications to partition /dev/sdb1 -- Device or resource busy. This means Linux won't know about any changes you made to /dev/sdb1 until you reboot -- so you shouldn't mount it or use it in any way before rebooting.
Warning: The kernel was unable to re-read the partition table on /dev/sdb (Device or resource busy). This means Linux won't know anything about the modifications you made until you reboot. You should reboot your computer before doing anything with /dev/sdb.

Re: What to do if partprobe also suffers from Device Busy?

Sorry, I've no idea. What kernel version are you using? It's quite possible that partprobe works only with a certain kernel version and above. I had the following, when got partprobe working successfully:
Linux server44 2.6.26-2-xen-amd64 #1 SMP Thu Aug 20 00:36:34 UTC 2009 x86_64 GNU/Linux

As this is still up to date

As this is still up to date -- you can use 'fuser -m' to view processes that might hold a disk in use.

Re: As this is still up to date

Yes, fuser -m is an option too. Although I "trust" lsof more.

fdisk-partprobe

Great and concise explanation !
Worked like a charm for me. Thanks a lot.

Thx for an excellent howto.

Thx for an excellent howto. It was very useful even when using a Live CD to do the work. (I had to reboot using a Live CD since the root file system was on a LV.)

The only info that was missing is that you might have to turn swap off (using swapoff) if you happen to have the swap as a LV. After turning the swap off, inactivate the volume:

lvm lvchange -a n vol01/xyz

At least, I found no way to umount the LV that was used for swap.

I guess the biggest problem was that the swap didn't show up in the df output so I didn't understand why/where the last LV was mounted.

regarding swap-partition ...

cat /proc/swaps
shows the device(file)name(s) used for swap and
swapoff /dev/path/to/device
umounts the specific one (if possible / unused)

Thanks for the feedback!

Thanks for the feedback! Smile

Thank you - very helpful article.

One small suggestion from a grammar nazi though - look up the difference between "lose" and "loose".

Re: Thank you - very helpful article

Thanks for the hint on "lose" vs. "loose". I actually know the difference between the two, it was just a typo.

Syndicate content