Adding new disk to lvm and extending filesystem

Our server had only 20GB of disk space. I had multiple partitions on LVM. We were running out of space on /var and /var/log partitions. After initial dump of existing logs usage was fine but it was going to go up soon again. I was planning to add more space to it and did this adding another disk to lvm and extending /var and /var/log partitions.

Here is how it did look before.

[root@my-server log]# df -h
Filesystem                               Size  Used Avail Use% Mounted on
/dev/mapper/vg_system-lv_root            8.7G  2.4G  6.3G  28% /
devtmpfs                                 3.9G     0  3.9G   0% /dev
tmpfs                                    3.9G     0  3.9G   0% /dev/shm
tmpfs                                    3.9G   57M  3.8G   2% /run
tmpfs                                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/mapper/vg_system-lv_tmp            1014M   33M  982M   4% /tmp
/dev/vda1                                509M  146M  364M  29% /boot
/dev/mapper/vg_system-lv_home            1.1G   33M  1.1G   3% /home
/dev/mapper/vg_system-lv_var             4.4G  481M  3.9G  11% /var
/dev/mapper/vg_system-lv_var_log        1014M  447M  568M  45% /var/log
/dev/mapper/vg_system-lv_var_log_sudoio 1014M   51M  964M   5% /var/log/sudo-io
/dev/mapper/vg_system-lv_var_log_audit   253M   45M  208M  18% /var/log/audit
tmpfs 
                                   783M     0  783M   0% /run/user/56734
[root@my-server log]# lsb
lsblk        lsb_release  
[root@my-server log]# lsblk 
NAME                            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda                             252:0    0   20G  0 disk 
├─vda1                          252:1    0  512M  0 part /boot
└─vda2                          252:2    0 19.5G  0 part 
  ├─vg_system-lv_root           253:0    0  8.7G  0 lvm  /
  ├─vg_system-lv_swap           253:1    0    2G  0 lvm  [SWAP]
  ├─vg_system-lv_var_log_sudoio 253:2    0    1G  0 lvm  /var/log/sudo-io
  ├─vg_system-lv_var_log_audit  253:3    0  256M  0 lvm  /var/log/audit
  ├─vg_system-lv_var_log        253:4    0    1G  0 lvm  /var/log
  ├─vg_system-lv_var            253:5    0  4.3G  0 lvm  /var
  ├─vg_system-lv_tmp            253:6    0    1G  0 lvm  /var/tmp
  └─vg_system-lv_home           253:7    0  1.1G  0 lvm  /home
vdb                             252:16   0   30G  0 disk

Add new physical disk, As you can see my new added 30GB is now vdb.

Using pvscan you can see the current physical volumes.

[root@my-server log]# pvscan 
  PV /dev/vda2   VG vg_system   lvm2 [19.50 GiB / 160.00 MiB free]
  Total: 1 [19.50 GiB] / in use: 1 [19.50 GiB] / in no VG: 0 [0   ]

Now I can add the disk to my server as a partition and make type 8e lvm.

[root@my-server log]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
 
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
Building a new DOS disklabel with disk identifier 0x824e6158.
 
Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
 
Command (m for help): p
 
Disk /dev/vdb: 32.2 GB, 32212254720 bytes, 62914560 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 label type: dos
Disk identifier: 0x824e6158
 
   Device Boot      Start         End      Blocks   Id  System
 
Command (m for help): l
 
 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
 
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-62914559, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559): 
Using default value 62914559
Partition 1 of type Linux and of size 30 GiB is set
 
Command (m for help): p
 
Disk /dev/vdb: 32.2 GB, 32212254720 bytes, 62914560 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 label type: dos
Disk identifier: 0x824e6158
 
   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    62914559    31456256   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): p
 
Disk /dev/vdb: 32.2 GB, 32212254720 bytes, 62914560 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 label type: dos
Disk identifier: 0x9c33419b
 
   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    62914559    31456256   8e  Linux LVM
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
 
[root@my-server log]# lsblk 
NAME                            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda                             252:0    0   20G  0 disk 
├─vda1                          252:1    0  512M  0 part /boot
└─vda2                          252:2    0 19.5G  0 part 
  ├─vg_system-lv_root           253:0    0  8.7G  0 lvm  /
  ├─vg_system-lv_swap           253:1    0    2G  0 lvm  [SWAP]
  ├─vg_system-lv_var_log_sudoio 253:2    0    1G  0 lvm  /var/log/sudo-io
  ├─vg_system-lv_var_log_audit  253:3    0  256M  0 lvm  /var/log/audit
  ├─vg_system-lv_var_log        253:4    0    1G  0 lvm  /var/log
  ├─vg_system-lv_var            253:5    0  4.3G  0 lvm  /var
  ├─vg_system-lv_tmp            253:6    0    1G  0 lvm  /var/tmp
  └─vg_system-lv_home           253:7    0  1.1G  0 lvm  /home
vdb                             252:16   0   30G  0 disk 
└─vdb1                          252:17   0   30G  0 part

To create a new physical LVM volume on our disk we use pvcreate.

[root@my-server log]# pvcreate /dev/vdb1
  Physical volume "/dev/vdb1" successfully created
[root@my-server log]# lsblk 
NAME                            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda                             252:0    0   20G  0 disk 
├─vda1                          252:1    0  512M  0 part /boot
└─vda2                          252:2    0 19.5G  0 part 
  ├─vg_system-lv_root           253:0    0  8.7G  0 lvm  /
  ├─vg_system-lv_swap           253:1    0    2G  0 lvm  [SWAP]
  ├─vg_system-lv_var_log_sudoio 253:2    0    1G  0 lvm  /var/log/sudo-io
  ├─vg_system-lv_var_log_audit  253:3    0  256M  0 lvm  /var/log/audit
  ├─vg_system-lv_var_log        253:4    0    1G  0 lvm  /var/log
  ├─vg_system-lv_var            253:5    0  4.3G  0 lvm  /var
  ├─vg_system-lv_tmp            253:6    0    1G  0 lvm  /var/tmp
  └─vg_system-lv_home           253:7    0  1.1G  0 lvm  /home
vdb                             252:16   0   30G  0 disk 
└─vdb1                          252:17   0   30G  0 part

Now we can add our new physical volume to the volume group: vg_system.

[root@my-server log]# vgextend vg_system /dev/vdb1
  Volume group "vg_system" successfully extended

See the new physical volume assigned to our volume group vg_system

[root@my-server log]# pvscan 
  PV /dev/vda2   VG vg_system   lvm2 [19.50 GiB / 160.00 MiB free]
  PV /dev/vdb1   VG vg_system   lvm2 [30.00 GiB / 30.00 GiB free]
  Total: 2 [49.49 GiB] / in use: 2 [49.49 GiB] / in no VG: 0 [0   ]

We can now increase the size of the logical volume on our /var and /var/log partitions. (check for other flags)

[root@my-server log]# lvextend -L +20G /dev/vg_system/lv_var
  Size of logical volume vg_system/lv_var changed from 4.33 GiB (1109 extents) to 24.33 GiB (6229 extents).
  Logical volume lv_var successfully resized.
 
[root@my-server log]# lvextend -L +10G /dev/vg_system/lv_var_log
  Size of logical volume vg_system/lv_var_log changed from 1.00 GiB (256 extents) to 11.00 GiB (2816 extents).
  Logical volume lv_var_log successfully resized.

There was some space left and so I decided to give that to /var/log/audit

[root@my-server log]# pvscan 
  PV /dev/vda2   VG vg_system   lvm2 [19.50 GiB / 0    free]
  PV /dev/vdb1   VG vg_system   lvm2 [30.00 GiB / 156.00 MiB free]
  Total: 2 [49.49 GiB] / in use: 2 [49.49 GiB] / in no VG: 0 [0   ]
 
[root@my-server log]# lvextend -L +156M /dev/vg_system/lv_var_log_audit 
  Size of logical volume vg_system/lv_var_log_audit changed from 256.00 MiB (64 extents) to 412.00 MiB (103 extents).
  Logical volume lv_var_log_audit successfully resized.
 
[root@my-server log]# pvscan 
  PV /dev/vda2   VG vg_system   lvm2 [19.50 GiB / 0    free]
  PV /dev/vdb1   VG vg_system   lvm2 [30.00 GiB / 0    free]
  Total: 2 [49.49 GiB] / in use: 2 [49.49 GiB] / in no VG: 0 [0   ]

Now the coolest part since I have a xfs filesystem I can extend (grow) it without rebooting.

[root@my-server /]# xfs_growfs /var
meta-data=/dev/mapper/vg_system-lv_var isize=256    agcount=4, agsize=283904 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=1135616, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 1135616 to 6378496
 
[root@my-server /]# xfs_growfs /var/log
meta-data=/dev/mapper/vg_system-lv_var_log isize=256    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 262144 to 2883584

Final look on my drive is

[root@my-server /]# df -h
Filesystem                               Size  Used Avail Use% Mounted on
/dev/mapper/vg_system-lv_root            8.7G  2.4G  6.3G  28% /
devtmpfs                                 3.9G     0  3.9G   0% /dev
tmpfs                                    3.9G     0  3.9G   0% /dev/shm
tmpfs                                    3.9G   57M  3.8G   2% /run
tmpfs                                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/mapper/vg_system-lv_tmp            1014M   33M  982M   4% /tmp
/dev/vda1                                509M  146M  364M  29% /boot
/dev/mapper/vg_system-lv_home            1.1G   33M  1.1G   3% /home
/dev/mapper/vg_system-lv_var              25G  488M   24G   2% /var
/dev/mapper/vg_system-lv_var_log          11G  449M   11G   4% /var/log
/dev/mapper/vg_system-lv_var_log_sudoio 1014M   51M  964M   5% /var/log/sudo-io
/dev/mapper/vg_system-lv_var_log_audit   409M   45M  364M  11% /var/log/audit
tmpfs                                    783M     0  783M   0% /run/user/56734
tmpfs                                    783M     0  783M   0% /run/user/3732