This article serves as a cheat guide for managing a Linux virtual machine. Most of this applies to physical Linux machines as well. Look for future updates to cover different hypervisors, distributions, etc. This is all pretty basic stuff that experienced Linux users are probably already aware of. But a cheat guide like this can come in handy.
Increase Disk Space
Your VM may run out of disk space on one or more partitions. One nice thing about virtual machines is that you can expand the virtual drive(s) or add new ones at any time (assuming you have enough physical disk space of course). Make a full backup of your virtual machine in case something goes wrong!
Note: using the current version of VirtualBox, I discovered that if you do this process on a VM with multiple snapshots, you may encounter a
BLKCACHE_IOERR crash. I know I’ve done this under VBox in the past and I always use snapshots, so this could be only an issue with growing the XFS file system, since that’s where the error occurs. Although you can force the process to continue, it seems to corrupt the file system and could cause data loss. So if this happens to you, revert to your last working snapshot then either clone the machine or delete all snapshots before doing this. On the forums it has been suggested to enable
Host I/O Cache for the SATA controller that hosts the virtual disks in order to avoid this crash, but this only caused more problem for me under CentOS/Fedora and possibly other distributions.
Expand Virtual Disk
Before you can extend a partition, you will probably need to increase the size of your virtual disk (alternately, you could skip this section by adding a second virtual disk to the VM). The method for doing so varies for each hypervisor. For example, on VirtualBox, you can increase the size of a VDI for a powered-off VM via the built-in Virtual Media Manager (just select the disk and use the slider).
Boot to Live CD
Mount the GParted Live CD ISO and boot to it. You could alternately use another Live CD like Knoppix or Ubuntu. For LVM, we won’t be using GParted except for extending the PV (LVM physical volume). What matters most is that your Live CD has support for your file system(s) and LVM (if using it).
Technically, if using LVM, you may be able to safely do this entire process without booting to a Live CD, but I prefer doing it that way so I know no processes are running that may interfere with the operation. But if this is a production machine and you can’t have even five minutes of downtime, you could skip booting to the Live CD.
Add Virtual Disk to Volume Group
If you chose to add a virtual disk rather than extending the existing one, and you are using LVM, you will need to partition the new disk and add it to LVM. Close GParted after booting to the CD image, and open the terminal. Assuming this new disk is the second disk (
sdb), do the following (change to
sdc, etc. as appropriate).
sudo fdisk /dev/sdb n p [Enter] [Enter] [Enter] t [Enter] 8e p w
Now restart, booting to the Live CD image again. Open the terminal again and check that the partition was successfully created with:
sudo fdisk -l /dev/sdb
Create the LVM physical volume and check that it was successful:
sudo pvcreate /dev/sdb1 sudo pvs
Extend the existing volume group to the new disk, checking that each command was successful as you go (replace
vg-name with the VG name):
sudo vgs sudo vgextend vg-name /dev/sdb1 sudo vgs sudo pvscan
Note that this only extends the volume group. It does not extend the volumes or file systems. See the next section for how to do that.
Extend Partion or Physical Volume
Use the GParted utility to move/expand partitions as needed. If using LVM, you will only be able to extend the LVM PV (physical volume) itself and not the volumes inside it. You may need to right-click on partitions and select "Deactivate" before you can expand them.
If using LVM, note the path to the LVM partition (
/dev/sda2 in the above screenshot). Reboot the system normally (not booting to the Live CD). If not using LVM, that should be it; you’re completely done.
Now, if using LVM ensure the LVM physical volume is using all available space (changing the partition name if necessary):
sudo pvresize /dev/sda2
Get a list of all the volumes:
If you have a lot of volumes, you may need to pipe
more to the above command to scroll through the output:
sudo lvdisplay | more
Note the LV Path and LV Size of the volume you wish to extend. There are several ways to do this. You can extend by number of extents (use
sudo vgdisplay to see number of free extents), by megabytes, or by percentage of free space. I prefer the latter, since I already know how much free space I have. For example, let’s say you want to assign half of the free space to each of two volumes. You can do so as follows. (The exact path may vary by distribution. Example:
lv-name as appropriate.
sudo lvextend -r -l +50%FREE /dev/vg-name/lv-name1 sudo lvextend -r -l +100%FREE /dev/vg-name/lv-name2
The first command extends a volume to take up half of the free space and the second extends another volume to take up the remaining free space.
You don’t have to use all available space. You could always reserve some for extending other volumes later as necessary.
Let’s make sure the sizes of the volumes and volume groups are what we expect them to be with:
sudo pvs sudo vgs sudo lvs
Reduce Volume/Partition Size
In some cases, you may wish to reduce the size of a volume or partition. This is not possible with XFS, but is easily done with EXT file systems, whether you are using LVM or not. First make a snapshot or backup in case something goes wrong!
You should make sure you really want to do this by checking how much of the partition’s capacity you are using:
If using LVM, you should check the size of your logical volumes (assuming you’ve set things up properly, this should match the size produced by
Lastly, do the following to find the correct path to the partition you wish to reduce.
sudo lvdisplay | more
Download and boot to the GParted Live CD ISO. Unlike with extending a partition, even if using LVM, it is highly recommended you do this from a Live CD! If not using LVM, you can simply use the Gparted graphical utility to reduce the size of your partitions. If using LVM, open the terminal instead and check the filesystem (changing the device path to match yours as displayed by
sudo e2fsck -ff /dev/vg-name/lv-name
Afterwards you can reduce the volume and file system size with a single command (replacing
xx with the size you want in gigabytes):
sudo lvreduce --resizefs -L xxG /dev/vg-name/lv-name
Shrink Virtual Disk
If you are using a dynamically-expanding virtual disk, there may be times you wish to shrink it. What happens is that as you remove files (or move them between partitions), Linux doesn’t wipe the space occupied by the removed files (actually no operating system does by default). So while you may only have 20 GB of data in your virtual machine, the virtual disk can grow to be as large as the maximum you set when you first configured the machine. This really doesn’t hurt anything, and in fact pre-allocated virtual disks give a slight performance improvement over dynamically-expanding ones. But maybe you are short on physical hard drive space.
Note on snapshots: if you have snapshots, this will only alter the current virtual disk and not any parent snapshots, so it may not free up as much physical disk space as you expect.
First, clean up files from your package manager. This process varies by distribution. Here are some common ones. These commands should all be safe assuming you haven’t done something like installing a kernel outside of an official repository. From a terminal, do:
sudo yum clean all sudo yum autoremove
sudo dnf clean all sudo yum autoremove
sudo apt-get clean sudo apt-get autoremove --purge
sudo zypper clean -a
Unfortunately, Zypper currently does not offer an easy method of automatically removing unneeded dependencies. You can list them with
zypper packages --unneeded and then type a long command to remove them, but this shouldn’t be needed if you used the
--clean-deps switch every time you removed a package. You can also use the terminal version of Yast to view orphaned packages and remove them one by one if you really want to. But I’m not sure I totally trust Zypper to remove orphaned packages as much as I do Yum, DNF, and Apt. If you really need those couple extra megabytes of space, you probably need to buy a new hard drive.
sudo pacman -Scc sudo pacman -Rns $(pacman -Qtdq)
The first command will clean the cache and the second will remove orphaned packages (if they exist; so you may see an error if you have none).
I am not recommending deleting things like log files or anything in the tmp directory. While it is sometimes safe to do so, you really need to be careful and know what you are doing. If you are really that hard up on freeing disk space, you should research all of your installed applications to see how doing so might affect them first. You could truncate log files that have gotten too large, but hopefully the applications writing to these logs automatically limit the file size.
Now let’s zero-fill all that free space to overwrite space used by deleted files. You can check your mount points with
df -h, but ignore special mounts such as
tmpfs. For example, let’s say you have a partition mounted to
/opt and another on
/usr/local, in addition to the root partition. In this case, you can zero-fill each partition as follows (I am using su in order to ensure we can run commands in sequence to avoid possible crashes):
sudo su dd if=/dev/zero of=/zerofill bs=25M; rm -fv /zerofill dd if=/dev/zero of=/opt/zerofill bs=25M; rm -fv /opt/zerofill dd if=/dev/zero of=/usr/local/zerofill bs=25M; rm -fv /usr/local/zerofill exit
bs=25M option will make this process much faster, though you could end up with a few extra megabytes of space you could have freed up. You could increase this number to make the process faster and reduce the risk of crashes (at the cost of a bit of RAM). Or you could decrease it if you are really trying to free up every possible kilobyte of space. If you omit this option, dd will use its default block size (usually 512 bytes), which can make this process take a long time on large drives.
Once all of this is done, shut down the VM. Time to compact the virtual disk itself. This process varies by hypervisor and host platform. For example, on a Windows host running VirtualBox, you would open a command prompt (possibly as Administrator) and do the following to list all virtual disks.
"C:\Program Files\Oracle\VirtualBox\VBoxManage" list hdds
Find the UUID of the disk you want to compact and run the following (replacing uuid with the correct UUID):
"C:\Program Files\Oracle\VirtualBox\VBoxManage" modifyhd --compact uuid