Add a Disk to a Linux System Using LVM

Let’s imagine that you had added a new physical disk to your Linux box and you need to get it setup to use inside your Linux operating system.  This can be done pretty quickly.  You can add the filesystem directly onto the physical disk, or you can set it up in a logical volume inside your Linux operating system.  Logical volumes give some benefits, some of which I will write about in future articles, so I will show you how to add it using logical volumes.  So, the directions below explain how to add a disk to a Linux system using lvm.

Use fdisk to Create a Disk Partitions

I want to use the entire disk if possible so I used fdisk below to specify the new disk which is /dev/sdb in my case.  You will want to verify what the new disk device is on your machine.  Typically, you can look for /dev/sd* and see all of the disks listed there.  Typically if you added a disk, it will be the last one alphabetically in the list, but be certain it is the new disk, because you could actually destroy everything you have on a disk otherwise.

[root@drleads-db dev]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x0d5385bf.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won’t be recoverable.Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).Command (m for help): pDisk /dev/sdb: 1649.3 GB, 1649267441664 bytes
255 heads, 63 sectors/track, 200512 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0d5385bfDevice Boot      Start         End      Blocks   Id  SystemCommand (m for help): n
Command action
e   extended
p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-200512, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-200512, default 200512):
Using default value 200512

Command (m for help): p

Disk /dev/sdb: 1649.3 GB, 1649267441664 bytes
255 heads, 63 sectors/track, 200512 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0d5385bf

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      200512  1610612608+  83  Linux

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

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

Make Physical Volume Available for Use in LVM Volume Groups

# pvcreate /dev/sdb1
Physical volume “/dev/sdb1” successfully created

Create a Volume Group

In my case, the volume group will be leadsdb.

# vgcreate leadsdb /dev/sdb1
Volume group “leadsdb” successfully created

Display the Volume Group Information

I want to see how much space is available in my Volume Group for assigning to logical volumes so I run the vgdisplay command as shown below on my new volume group (leadsdb).

# vgdisplay leadsdb
— Volume group —
VG Name               leadsdb
System ID
Format                lvm2
Metadata Areas        1
Metadata Sequence No  1
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                0
Open LV               0
Max PV                0
Cur PV                1
Act PV                1
VG Size               1.50 TiB
PE Size               4.00 MiB
Total PE              393215
Alloc PE / Size       0 / 0
Free  PE / Size       393215 / 1.50 TiB
VG UUID               oRcMfY-oALB-f6Up-Io6v-Yy2N-YdUn-MEB06d

Create the logical volume

Now I need to create a logical volume named data inside my volume group named leadsdb.  I set the size to 1530 GB which is nearly all of the space in the leadsdb volume group.

# lvcreate –size 1530G –name data leadsdb
Logical volume “data” created

If ever I want to delete my logical volume, I show how to do that below.  Be CAREFUL!  It will delete it and all of the data on it!  Only do this if you really want to!!

# lvremove leadsdb
Do you really want to remove active logical volume data? [y/n]: yLogical volume “data” successfully removed

Look at the Disk device

When we create logical volumes, it creates device files for them.  My “data” logical volume is show below.

# ls -l /dev/leadsdb/datalrwxrwxrwx. 1 root root 7 Sep 20 15:47 /dev/leadsdb/data -> ../dm-2

Create the File System

We now need to create a file system on the logical volume that we just created.  In my case, I want to make it an ext4 file system.

# mkfs -t ext4 /dev/leadsdb/data
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
100270080 inodes, 401080320 blocks
20054016 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
12240 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Create the Mount Point

The mount point is what helps us access the data that we will store in the filesystem on the new disk.  We can access the data on the new disk by changing to the directory.  “cd /data”

# mkdir /data

 Edit the /etc/fstab File

Edit the /etc/fstab file specifying the device path, the mount point, the fstype, options, dump or not (0,1), and fsck pass (set at 2 if you want it checked, 0 for no check).  The /etc/fstab file tells the Linux operating system how to mount all of the filesystems when the machine boots.

/dev/mapper/vg_drleadsdb-lv_root /                       ext4    defaults        1 1
UUID=1e5d0952-b0c2-421e-a882-594e39277675 /boot                   ext4    defaults        1 2
/dev/mapper/vg_drleadsdb-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/leadsdb/data       /data                   ext4    defaults        0 2

Mount the File System

# mount /data

Verify That the File System is Mounted

# df -h /data
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/leadsdb-data
1.5T  197M  1.4T   1% /data

Looks good.  Our file system is mounted and our new disk is all setup to be used on the Linux machine.

 

 

 

 

The following two tabs change content below.
Jeff has 20 years of professional IT experience, having done nearly everything in his roles of IT consultant, Systems Integrator, Systems Engineer, CNOC Engineer, Systems Administrator, Network Systems Administrator, and IT Director. If there is one thing he knows for sure, it is that there is always a simple answer to every IT problem and that downtime begins with complexity. Seasoned IT professional by day, Jeff hopes to help other IT professionals by blogging about his experiences at night on his blog: http://uptimemadeeasy.com. You can find Jeff on or LinkedIn at: LinkedIn or Twitter at: Twitter

Latest posts by Jeff Staten (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *