This document outlines how to add disk space to a live Linux VM.
I wrote this document against Debian Squeeze (testing), but it should be applicable to Ubuntu and other Linuxes. I'll also try to explain the general principles used.
I was running Linux 3.1, but I don't know of anything here that won't work against the 2.6 series.
I'm assuming that you've installed Linux with LVM enabled, and that the installer set up a single LVM volume group and a single LVM logical volume for your filesystem (there may be an additional logical volume for swap)
LVM stands for "Logical Volume Management" and provides a way to virtualize a single "logical" filesystem across multiple physcial disks or partitions.
LVM has three basic elements: physical volumes, volume groups, and logical volumes.
A physcial volume is a disk partition that LVM has been told about. Physical volumes
are manipulated and introspected upon with the tools in /sbin/pv*
.
A volume group is a named colleciton of physical volumes. Volume groups are manipulated
and introspected upon the the tools in /sbin/vg*
.
A volume group then provides storage to one or more logical volumes. A logical volume
is the equivalent of a non-LVM partition - the logical volume may be formatted and
mounted into the file system like any other disk. Logical volumes are managed with the
tools in /sbin/lv*
.
First, you'll want to know what you have. These tools will help:
less /etc/fstab
- lets you know which volumes are attached to particular mount points, so you know what you want to grow./sbin/lvdisplay
- lists each LVM logical volume, along with which volume group it is on and what its current size is./sbin/vgdisplay
- lists each volume group, with its name and size./sbin/pvdisplay
- lists each LVM physcial volume, along with its size, and which volume group it is assigned to.fdisk -l
- lists each device, and each partition on the device.
We will:
- Add a new virtual disk to our VM
- Add a partition to the new disk
- Initialize the new partition as an LVM physical volume
- Add the physical volume to our volume group
- Expand the logical volume into the new space
- Resize the filesystem on the logical volume
First, add the new disk with VMWare tools however you need to. You should be able to do this without bringing down the VM.
Next we need to tell the linux kernel to notice the new disk.
In the directory /sys/class/scsi_host
there should be one or more hostN
folders, where
N
is a number 0 or greater. These represent the SCSI host adapters on your machine.
The command echo "- - -" > /sys/class/scsi_host/host0/scan
will tell the kernel to re-scan
SCSI host 0. I did this for each host, since I didn't know which one the new disk went on to.
Using fdisk -l
, you should be able to find the new disk. In my case, the output
included at the end the text "Disk /dev/sdb doesn't contain a valid partition table", which tells
me that my newly added disk is /dev/sdb
.
Run fdisk
for your new drive, in my case that is fdisk /dev/sdb
. Select 'n' to create a new
partition, preferably primary partion 1. If you blow through the defaults, the partion will take
up the entire disk which is good.
Enter 't' to change the type of the parition, and set the type to '8e', for Linux LVM.
If you want to review your work, use the 'p' command.
Now, use the 'w' command to save your changes and exit.
Run /sbin/partprobe
(from the Debian package parted
) to have the kernel re-scan your
new partition table. To verify this run ls /dev/sd*
to see if you can find your
new partition.
Here, the examples assume that your new parition is /dev/sdb1
.
First, run /sbin/pvcreate /dev/sdb1
to create the new LVM physical
volume.
Next add the new PV to your volume group with /sbin/vgextend $volume-group-name /dev/sdb1
, where
$volume-group-name
is the name of the volume group your logical volume resides on.
If you run /sbin/vgdisplay
, your volume group should have a larger size now than it
did.
To expand your logical volume by the size of your new disk you can use the
command /sbin/lvextend $logical-volume-name /dev/sdb1
(you can also expand by a fixed
amount, in the case you want to share the new physical volume with multiple
logical volumes). (You can find your logical volume name either in /etc/fstab
or with
the command /sbin/lvdisplay
.)
Your logical volume should now show up as larger in the /sbin/lvdisplay
listing.
Assuming that you're using ext3 or ext4, executing /sbin/resize2fs $logical-volume-name
will grow your file system, and make the new space usable by your applications. You can
check your new size using the command df -h
.
I don't know of any other way to extend a partition with no down-time.
It should be possible to take down the VM, grow the existing disk, and
then use something like parted
or the graphical gparted
to grow one
of the existing partitions. This probably only works for unmounted partitions,
and partitions with room "after" them, that is, this will likely only work
for the last on-disk partition.
The non-LVM approch will require some planning and forsight, whereas the LVM approach requires extra steps and an addtional layer of indirection on your filesystem, but growing can be done in a much-more ad-hoc manner.
<style type="text/css"> code {background-color: lightgray} </style>