I often have to increase the size of a virtual disk on a virtual machine. But I always seem to forget how to do it. I guess I have done it over a 100 times and I cannot remember exactly how I did it. So this blog entry is to help people on how to do this and as a reminder to myself.
This example is done on a virtual machine with CentOS 6, but it can be
done on every Linux. And in the fdisk
examples I have left out some of
the not to interesting lines.
Oke, here we go:
First, shut down your virtual machine and increase the disk size. Then
start your virtual machine and go to the console. Now you have a virtual
machine with a new disk size, but the current partition table needs to
be adjusted to the new disk size. I know it’s possible with parted
,
but I always seem to end up on systems where it’s not available. So I
just use fdisk
.
# fdisk /dev/sda
Now give the p
command, which prints the partition table and make a
note of the start cylinder of the Linux LVM
partition. This is the
partition we are going to increase.
Please be very careful This trick only works if the partition you want to resize is at the end of the disk and contains a logical volume type system.
Command (m for help): p Disk /dev/sda: 17.2 GB, 17179869184 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux /dev/sda2 65 2089 16264192 8e Linux LVM
Delete the LVM partition (we will recreate this later)
Command (m for help): d Partition number (1-5): 2
and create a new partition with the original starting point. The starting point should be the same, because all the partitions meta data is at the start of the partition.
Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2
And fill in the start cylinder of the LVM partition we deleted above
First cylinder (1-25600, default 1): 65 Last cylinder, +cylinders or +size{K,M,G} (65-25600, default 25600): Using default value 25600
and change the type to Linux LVM
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: 26.8 GB, 26843545600 bytes Device Boot Start End Blocks Id System /dev/sda1 * 2 64 262144 83 Linux /dev/sda2 65 2089 24567892 8e Linux LVM
If you agree with the new layout, write it to disk with the w
command
and quit with q
. If it’s not the disk disk with the root
volume on
it, it could be possible that you can skip the next reboot
. Just a
partprobe
could do the trick.
# reboot
First it’s needed to resize physical volume.
# pvresize /dev/sda2
Make sure you know how much free space you now have
# vgdisplay
Make a note of the LV Name of the logical volume you want to resize
# lvdisplay
Resize the logical volume. I use gigabytes as an example here.
# lvresize -L +[Size]GB [LV Name]
Resize the file system on the logical volume.
# resize2fs [LV Name]