fdisk + Logical Volume Manager

Extending an existing logical volume

Expanding a VMware Workstation VM Disk and increasing the size of the LVM /var partition. This was performed on my Debian installation which had separate /home, /tmp and /var partitions.

Power off the VM and in settings expand disk (from 20G to 40G), when done power on and follow these instructions

We can confirm that Linux has seen the new disk size with

# fdisk -l
    Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: dos
    Disk identifier: 0x949f9917
Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 499711 497664 243M 83 Linux /dev/sda2 501758 41940991 41439234 19.8G 5 Extended /dev/sda5 501760 41940991 41439232 19.8G 8e Linux LVM

so we have 40G but only 20G allocated, we will therefore create a new partition on /dev/sda, we can do this as there are at present only two primary partions (sda1 & sda2) and one logical partition (sda5 Linux LVM on sda2 Extended), so issue the following command

# fdisk /dev/sda
    Welcome to fdisk (util-linux 2.25.2).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.

    Command (m for help): n
    Partition type
    p   primary (1 primary, 1 extended, 2 free)
    l   logical (numbered from 5)
    Select (default p): p
    Partition number (3,4, default 3): 3
    First sector (499712-83886079, default 499712): 41940992
    Last sector, +sectors or +size{K,M,G,T,P} (41940992-83886079, default 83886079):

    Created a new partition 3 of type 'Linux' and of size 20 GiB.

    Command (m for help): t
    Partition number (1-3,5, default 5): 3
    Hex code (type L to list all codes): 8e

    Changed type of partition 'Linux' to 'Linux LVM'.

    Command (m for help): w
    The partition table has been altered.
    Calling ioctl() to re-read partition table.
    Re-reading the partition table failed.: 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).

41940992 is first sector not the default 499712 - /dev/sda3 MUST start after the end of /dev/sda2 which is at sector 41940991.

To run partprobe on Debian you must first install it

# aptitude install partprobe -y

then run

# partprobe -s

Now we can move on to extending /dev/var with LVM

# pvcreate /dev/sda3
    Physical volume "/dev/sda3" successfully created

Let's extend the volume created in the Debian 8 installation called "debian"

# vgextend debian /dev/sda3
    Volume group "debian" successfully extended

We need to check the path for the /var partition we wish to extend

# lvscan
    ACTIVE            '/dev/debian/root' [7.18 GiB] inherit
    ACTIVE            '/dev/debian/var' [2.79 GiB] inherit
    ACTIVE            '/dev/debian/swap_1' [1.29 GiB] inherit
    ACTIVE            '/dev/debian/tmp' [380.00 MiB] inherit
    ACTIVE            '/dev/debian/home' [8.12 GiB] inherit

Issue the following command, this will allocate all free extents to /dev/debian/var

# lvextend -l +100%FREE /dev/debian/var
    Size of logical volume debian/var changed from 2.79 GiB (715 extents) to 22.79 GiB (5835 extents).
    Logical volume var successfully resized.

Finally we must resize the file system, to find the file system used input

# df -h -T
    Filesystem              Type      Size  Used Avail Use% Mounted on
    /dev/dm-0               ext4      7.0G  751M  5.9G  12% /
    udev                    devtmpfs   10M     0   10M   0% /dev
    tmpfs                   tmpfs     400M  5.6M  394M   2% /run
    tmpfs                   tmpfs     999M     0  999M   0% /dev/shm
    tmpfs                   tmpfs     5.0M     0  5.0M   0% /run/lock
    tmpfs                   tmpfs     999M     0  999M   0% /sys/fs/cgroup
    /dev/mapper/debian-home ext4      7.9G   19M  7.5G   1% /home
    /dev/sda1               ext2      236M   33M  191M  15% /boot
    /dev/mapper/debian-var  ext4       23G  205M   22G   1% /var
    /dev/mapper/debian-tmp  ext4      360M  2.1M  335M   1% /tmp

If it is ext* (Debian) use the following command

# resize2fs /dev/debian/var
    resize2fs 1.42.12 (29-Aug-2014)
    Filesystem at /dev/debian/var is mounted on /var; on-line resizing required
    old_desc_blocks = 1, new_desc_blocks = 2
    The filesystem on /dev/debian/var is now 5975040 (4k) blocks long.

If it is xfs (Centos 7/RHEL) use

# xfs_growfs /dev/centos/var