Online resizing of an LVM partition.
The concept and objective
I built a Bacula server a while back with a 500GB HDD, which I use to backup a number of servers including a mail server. I had it setup so it would recycle the volumes every 180 days (roughly six months), thinking that each volume would well and truly be ready for recycle by that time.
The trouble was, the mail server’s popularity, not to mention the users desire to keep every Powerpoint presentation touting the latest emotional platitudes (you know the ones) meant that recycle time came around much faster than I thought.
I didn’t want to rebuild the server, so the plan was to clone the 500GB drive onto a 1TB drive, and resize the partitions accordingly.
The plan was to follow these general steps:
- Create a new partition on the unallocated free space.
- Expand the physical volume to include the new partition.
- Expand the logical volume into the inflated PV.
- Resize the file system into the expanded LV.
Simple right? Well, yes… in a sort of round about way. You have to remember the context I operate in – I use free stuff as much as possible!! Most free tools out there allow you to do most things, but don’t make it too easy for you because they want you to buy the posh version (of course!)
So, that said there are probably paid applications which will do it all for you – this HOWTO is based on some of the FREE tools I had at hand.
The ultimate power in the universe… well, in a boot CD maybe
If you don’t have it already, grab a copy of the “Ultimate Boot CD” or UBCD as it’s often referred to. It’s a great ISO packed with tons of tools that can either be burnt onto CD or imaged to USB.
I had mixed results with some of the tools so I’ll give you a rundown own what worked/didn’t work for me:
- Parted Magic has some great utilities. However, I don’t know if it was me but Clonezilla (which can resize partitions automatically and proportionally) took the better part of three days and was still going when I decided to kill it.
- Parted magic also has GParted , a GUI of parted for resizing the partitions – but this didn’t support LVM volume resizing.
- One of the “Hard Disk Cloning” tools under the UBCD menu is HDClone. A great cloning tool, but it doesn’t have the ability to resize the partitions proportionally, and is transfer speed limited (because it’s the free edition).
- I ended up using EaseUs Disk Copy (v2.3.1) – it cloned the disks with a fantastic 3.08GB/s and only took around an hour and a half (1.5 hours).
Once the disks had been cloned, I removed the original drive and booted off the clone drive as normal.
Come out, come out free space wherever you are?
I’ll add “the” disclaimer at this point: This was the way I resized my volume – there may be other ways, better ways. Most importantly, make sure you have a backup of your data as working with the partitions and volumes can result in loss of data!!
OK, are we good to go on?
I used ‘parted‘ to create a new partition because fdisk didn’t give me any options to display or use the unallocated free space.
sudo parted
(parted) print free
Model: ATA ST31000524AS (scsi)
Disk /dev/sda: 1000GB <-- !!Take note of this disk id, we'll need it later !!
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
32.3kB 1049kB 1016kB Free Space
1 1049kB 256MB 255MB primary ext2 boot
256MB 257MB 1048kB Free Space
2 257MB 500GB 500GB extended
5 257MB 500GB 500GB logical lvm
500GB 500GB 1049kB Free Space
500GB 1000GB 500GB Free Space <-- Note the 'Start' and 'End' value.
1000GB 1000GB 745kB Free Space
(parted) print free
Model: ATA ST31000524AS (scsi)
Disk /dev/sda: 1000GB <– !!Take note of this disk id, we’ll need it later !!
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
32.3kB 1049kB 1016kB Free Space
1 1049kB 256MB 255MB primary ext2 boot
256MB 257MB 1048kB Free Space
2 257MB 500GB 500GB extended
5 257MB 500GB 500GB logical lvm
500GB 500GB 1049kB Free Space
500GB 1000GB 500GB Free Space <– Note the ‘Start’ and ‘End’ value.
1000GB 1000GB 745kB Free Space
Look at the entries listing the free space and note down the ‘Start’ and ‘End’ value for the corresponding block of free space you want to use. In this case it’s ‘500GB’ and ‘1000GB’ respectively. It’s also worth noting down the disk id as we’ll need this in a later step – in this case it’s /dev/sda.
Time to create our new partition…
(parted) mkpart
Partition type? primary/logical? p
File system type? [ext2]? ext3
Start? 500GB
End? 1000GB
…and check to confirm it’s been created, then quit parted
(parted) print
Model: ATA ST31000524AS (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 256MB 255MB primary ext2 boot
2 257MB 500GB 500GB extended
5 257MB 500GB 500GB logical lvm
3 500GB 1000GB 500GB primary <– Your new partiton! Note the number ‘3’
(parted) quit
Information: You may need to update /etc/fstab.
Now some wichery as we use fdisk to change the new partition’s system type.
sudo fdisk /dev/sda
Command (m for help): t
Partition number (1-5): 3 <– Use the number obtained above using parted
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)
Now check to make sure changes have taken…
Command (m for help): p
Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0002a3e6
Device Boot Start End Blocks Id System
/dev/sda1 * 1 32 248832 83 Linux
/dev/sda2 32 60802 488136705 5 Extended
/dev/sda3 60802 121602 488374272 8e Linux LVM <– Here is the changed partition!
/dev/sda5 32 60802 488135680 8e Linux LVM
… and write the changes.
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 or after you run partprobe(8) or kpartx(8)
Syncing disks.
Reboot the system for the changes to take effect.
sudo shutdown -r now
A volume by any other name.
Once the system has restarted, log on and create a new physical volume, on the new partition…
sudo pvcreate /dev/sda3
Physical volume “/dev/sda3” successfully created
Confirm the new volume has been created…
sudo pvdisplay
— Physical volume —
PV Name /dev/sda5
VG Name vgname
PV Size 465.52 GiB / not usable 2.81 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 119173
Free PE 12
Allocated PE 119161
PV UUID vdZpKf-KkHG-mEtw-2vsh-khTT-rRr2-6qhbuL
“/dev/sda3” is a new physical volume of “465.75 GiB”
— NEW Physical volume —
PV Name /dev/sda3
VG Name
PV Size 465.75 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID UfF1rK-Lx8e-G8Vg-A321-uJQO-5I5O-gqS4XZ
Now we extend the volume group to include the new physical volume.
sudo vgextend vgname /dev/sda3
Volume group “vgname” successfully extended
We can confirm the logical volume name…
sudo lvdisplay
— Logical volume —
LV Name /dev/vgname/root <– this is what we’ll use in the next step
VG Name vgname
LV UUID ebGsgt-CNyw-MsMw-cMM1-ORiD-uZyI-jQqCU4
LV Write Access read/write
LV Status available
# open 1
LV Size 459.82 GiB <– this will increase after we extend the LV
Current LE 117715
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 251:0
… and extend the logical volume to include the physical volume (which is now in our volume group).
sudo lvextend /dev/vgname/root /dev/sda3
Extending logical volume root to 925.57 GiB
Logical volume root successfully resized
A quick check now reveals…
sudo lvdisplay
— Logical volume —
LV Name /dev/vgname/root <– this is what we’ll use in the next step
VG Name vgname
LV UUID ebGsgt-CNyw-MsMw-cMM1-ORiD-uZyI-jQqCU4
LV Write Access read/write
LV Status available
# open 1
LV Size 925.57 GiB <– HOORAY!!
Current LE 236946
Segments 2
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 251:0
Finally, we use resize2fs to resize the file system to include the new space created in the logical volume.
sudo resize2fs /dev/vgname/root
resize2fs 1.41.11 (14-Mar-2010)
Filesystem at /dev/vgname/root is mounted on /; on-line resizing required
old desc_blocks = 29, new_desc_blocks = 58
Performing an on-line resize of /dev/vgname/root to 242632704 (4k) blocks.
This last step took almost an hour to complete. The rough maths would be 60 minutes divided by the 29 block increase equals ~2 minutes per block. I’m guessing that this would change depending on the specifications of your hardware, disks, interfaces etc…