Move /var to a new partition with LVM

Apparently docker needs A LOT of space under /var to handle a devicemapper and stuff for the containers obviously, my root partition was too small.

Well, fortunately, the process to extend an partition of a is really smooth and not painful at all.

First shut down the VM and add a new HD under the existing controller (or create a new one, whatever).

Boot your VM in single mode (or rescue mode or maintenance, all different names to say “init 1”) and check with dmesg the new device name; in my case, it is/dev/sdb.

Now you need to create a primary partition with fdisk, remember that the type must be LVM, some steps below:

# fdisk /dev/sdb

n is the option to create a new partition, then p for selecting primary, then a bunch of useless question for this case, like the partition number, first and last cylinder, just use the default options.

This will create a Linux primary partition, you will need to use the command t in order to change the partition type to 8e (Linux LVM).

Then w will write everything to the disk and exit from fdisk.

# fdisk -l /dev/sdb

This will return something like

Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 20971519 20969472 10G 8e Linux LVM

Ok, now it’s time to add the newly formatted device to LVM, this is a very easy and smooth process.

Add the new device to the LVM physical volume:

# pvcreate /dev/sdb1

You will need to know your VG name, you can use vgdisplay to check it:

--- Volume group ---
VG Name virtualdeb64-vg
[... bla bla bla... a lot of usefull lines here]

Ok, so in my case is virtualdeb64-vg, you have chosen yours during the system installation.

Extend the Volume group:

# vgextend virtualdeb64-vg /dev/sdb1

In order to create a new logical volume you will need to run vgdisplay again and pick the Free PE number, in my case it was something similar to this:

VG Size 29.75 GiB
PE Size 4.00 MiB
Total PE 7617
Alloc PE / Size 5058/ 19.75 GiB
Free PE / Size 2559 / 10 Gib

Create a new logical volume:

# lvcreate -n var -l2559 /dev/virtualdeb64-vg/var

Create a filesystem (in my case ext4)

# mkfs -t ext4 /dev/virtualdeb64-vg/var

Done! Now let’s mount it, copy /var and replace it.

# mkdir /mnt/var_new
# mount /dev/virtualdeb64-vg/var /mnt/var_new
# rsync -avHPSAX /var/ /mnt/var_new/

Rsync is a better option than cp -R, because it preserves links and attributes also, it prints a nice status update for each file.

Check if everything is fine with diff

# diff -r /var /mnt/var_new/

If the diff returns something you are screwed. So have fun check what is missing and (mostly) why. May the force be with you.
Now update the fstab (I am not sure if there is a line priority in the fstab, I have put this line right after / to be sure)


/dev/mapper/virtualdeb64--vg-var /var ext4 defaults 0 0

Then rename the current /var, create a new empty folder and remount

# mv /var /var_old
# mkdir /var
# mount /var

After that your df -h | grep /dev/ should be something similar to this:

# df -h | grep /dev/
/dev/dm-0 8.9G 6.4G 2.1G 76% /
/dev/mapper/virtualdeb64--vg-var 9.8G 1.1G 8.2G 12% /var
/dev/sda1 236M 34M 190M 15% /boot
/dev/mapper/virtualdeb64--vg-home 9.8G 2.8G 6.6G 30% /home

Have fun!


Sources:

RootUsers – How to Increase the size of a Linux LVM by adding a new disk

Debian wiki – LVM

Leave a Reply

CommentLuv badge