Monday, January 4, 2016

How to add a new hard drive with LVM


Let's assume you want to add a new disk (physical or virtual) in your Linux
system, so you will surely have to shutdown your system, add the desired hard
drive and and then boot the system.
After that you are 6 step away from adding your new hard drive.

1. Listing hard disk drives

Here for instance I have added a new 2GiB hard drive, naturally now the second (new)
hard drive on my system is named /dev/sdb, so let's show all disk present in the system :

# fdisk -l
Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 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 Disk /dev/sda: 8 GiB, 8589934592 bytes, 16777216 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: 0x0003e22d Device     Boot Start      End  Sectors Size Id Type /dev/sda1  *     2048 16777215 16775168   8G 8e Linux LVM Disk /dev/mapper/system-swap: 2 GiB, 2147483648 bytes, 4194304 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 Disk /dev/mapper/system-root: 6 GiB, 6434062336 bytes, 12566528 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

2. Creating a new partition

So now we will create a new partition that will take the entire hard drive and
labeled it as a LVM partition with fdisk:
# fdisk /dev/sdb Welcome to fdisk (util-linux 2.25). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table. Created a new DOS disklabel with disk identifier 0xa86da927. Command (m for help): p Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 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: 0xa86da927 Command (m for help): n Partition type
   p   primary (0 primary, 0 extended, 4 free)    e   extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): First sector (2048-4194303, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-4194303, default 4194303): Created a new partition 1 of type 'Linux' and of size 2 GiB. Command (m for help): p Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 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: 0xa86da927 Device     Boot Start     End Sectors Size Id Type /dev/sdb1        2048 4194303 4192256   2G 83 Linux Command (m for help): t Selected partition 1 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. Syncing disks.

3. Format your new partition
Format your new partition with the desired File System, here I will use ext4:
# mkfs.ext4 /dev/sdb1

4. Listing the LVM layout
So now let's add our new partition to our existing LVM layout: But first let's show the existing LVM layout :
# pvs   PV         VG     Fmt  Attr PSize PFree   /dev/sda1  system lvm2 a--  8.00g 4.00m
# vgs   VG     #PV #LV #SN Attr   VSize VFree   system   1   2   0 wz--n- 8.00g 4.00m
# lvs   LV   VG     Attr      LSize Pool Origin Data%  Move Log Cpy%Sync Convert   root system -wi-ao--- 5.99g   swap system -wi-ao--- 2.00g

5. Adding the new partition to LVM

Initialize the partition for use by LVM :
# pvcreate /dev/sdb1   Physical volume "/dev/sdb1" successfully created

Add the new partition to the existing "system" Volume Group:
# vgextend system /dev/sdb1   Volume group "system" successfully extended

for adding all free space to Logical Volume "root"
# lvextend -l +100%FREE /dev/system/root   Extending logical volume root to 7.99 GiB   Logical volume root successfully resized

or for extending the logical volume to a specific amount (replace x by the desired amount of GiB)
# lvextend -L xG /dev/system/root

for adding one more GiB to the logical volume
lvextend -L+1G /dev/system/root

6. Resizing the Filesystem
For ext Filesystems:
# resize2fs /dev/system/root

For btrfs File System: Please note that btrfs has the built-in ability to add new hard drive to a mounted filesystem. Thus for performance optimization you could use btrfs without the LVM layer. So adding a btrfs filesystem to LVM is still doable as below.
# btrfs filesystem show Label: none  uuid: 71e5c18b-ed4b-475a-9669-b2ee31769351     Total devices 1 FS bytes used 4.67GiB     devid    1 size 5.99GiB used 5.99GiB path /dev/mapper/system-root   Btrfs v3.18.2+20150430

Where 1 is the devid number, you can specify an amount but "max" also possible and this will use all remaining free space on the device.
# btrfs filesystem resize devid:amount /mount-point

# btrfs filesystem resize 1:max /   Resize '/' of '1:max'

# btrfs filesystem show   Label: none  uuid: 71e5c18b-ed4b-475a-9669-b2ee31769351       Total devices 1 FS bytes used 4.67GiB       devid    1 size 7.99GiB used 5.99GiB path /dev/mapper/system-root   Btrfs v3.18.2+20150430