Linux - How to add a new disk to a volume group

 

The disk to add to the VG can be a single disk or a hardware raid of disks that can be seen by the fdisk command

fdisk -l

The truncated output from this command is shown below

Disk /dev/sda: 146.6 GB, 146694733824 bytes
255 heads, 63 sectors/track, 17834 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          17      136521   83  Linux
/dev/sda2              18       17834   143115052+  8e  Linux LVM

Disk /dev/sdb: 293.3 GB, 293389467648 bytes
255 heads, 63 sectors/track, 35669 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn't contain a valid partition table

We can see that the disk /dev/sdb is not in use

We now need to create a partition on this disk using the fdisk command as follows

fdisk /dev/sdb

Command (m for help): p

Disk /dev/sdb: 293.3 GB, 293389467648 bytes
255 heads, 63 sectors/track, 35669 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-35669, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-35669, default 35669):
Using default value 35669

Command (m for help): p

Disk /dev/sdb: 293.3 GB, 293389467648 bytes
255 heads, 63 sectors/track, 35669 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       35669   286511211   83  Linux

The above process creates a linux partition the same size as the disk, however we need to change the type from linux to Linux LVM as this partition will be part of Logical Volume Management (LVM)

Whilst still in the fdisk /dev/sdb utility do the following

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdb: 293.3 GB, 293389467648 bytes
255 heads, 63 sectors/track, 35669 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       35669   286511211   8e  Linux LVM

Command (m for help):

After the partition type has been changed use the w command to write the partition table to the disk.

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

We now need to prepare the disk for use in LVM by using the pvcreate command

pvcreate /dev/sdb1

Physical volume "/dev/sdb1" successfully created

Now in this example we are going to extend an existing volume group to incorporate this new partition. You could however create a new VG and add it to that.

Use the vgdisplay command to see the existing volume groups

vgdisplay

 --- Volume group ---
  VG Name               system
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  7
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                6
  Open LV               6
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               136.48 GB
  PE Size               4.00 MB
  Total PE              34940
  Alloc PE / Size       16896 / 66.00 GB
  Free  PE / Size       18044 / 70.48 GB
  VG UUID               Xcgf7x-W6FJ-OMPa-0j21-XIdt-NLFz-PshfWe

We are going to add /dev/sdb1 into the existing VG called system by using the vgextend command

vgextend system /dev/sdb1

Volume group "system" successfully extended

Use the vgdisplay command again to view the statistics for the VG. Check the Free row to ensure the free disk space in the VG has increased as shown below.

vgdisplay

 --- Volume group ---
  VG Name               system
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  8
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                6
  Open LV               6
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               409.72 GB
  PE Size               4.00 MB
  Total PE              104888
  Alloc PE / Size       16896 / 66.00 GB
  Free  PE / Size       87992 / 343.72 GB
  VG UUID               Xcgf7x-W6FJ-OMPa-0j21-XIdt-NLFz-PshfWe

In this case we want to use the capacity of the new partition to create a new logical volume. This can be achieved by using the lvcreate command

lvcreate -L 293G -n logs system

Logical volume "logs" created

This command creates a 293GB logical volume called logs in the system VG

We now need to create a filesystem on the LV as follows

mkfs.ext3 /dev/system/logs

mke2fs 1.38 (30-Jun-2005)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
38404096 inodes, 76808192 blocks
3840409 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=79691776
2344 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

This creates an ext3 filesystem on the logs LV

Now all we have left to is mount the new filesystem, in this case on an existing directory called logs

mount /dev/system/logs /logs

Lastly don't forget to edit /etc/fstab so that this filesystem is mounted on boot