Servers and Server Apps

Add a drive to an existing Linux LVM

10556

Had a problem where I ran out of disk space, and needed to increase it on a Linux server.  Now, I’m not a Linux engineer, but I have used it for some time.  As far as I know, there are 2 ways to extend a drive that is built using LVM (Logical Volume Manager).

  1. Add an additional drive, add it to LVM, and expand the data to use the new drive. (physical and VMware)
  2. Expand the drive in VMware, then expand the LVM (VMware only)

I’m going to discuss option 1, since it can be used for both physical and virtual.

First off, let’s see what we have to work with, shall we?  Let’s do a “df -h” to see our current disk usage:

 

[root@www2 ~]# df -h
 
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00 15G 14G 0 100% /
/dev/sda1 99M 13M 82M 14% /boot
tmpfs    1.7G 0 1.7G 0% /dev/shm
 
Now, do a scan of our volume group:
 
[root@www2 ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "VolGroup00" using metadata type lvm2
 
Let’s see what our LVM Disks are:

 
[root@www2 ~]# lvmdiskscan
 
 
  /dev/ramdisk             [       16.00 MB]
  /dev/root                [       15.00 GB]
  /dev/ram                 [       16.00 MB]
  /dev/sda1                [      101.94 MB]
  /dev/VolGroup00/LogVol01 [      768.00 MB]
  /dev/ram2                [       16.00 MB]
  /dev/sda2                [        7.90 GB] LVM physical volume
  /dev/ram3                [       16.00 MB]
  /dev/ram4                [       16.00 MB]
  /dev/ram5                [       16.00 MB]
  /dev/ram6                [       16.00 MB]
  /dev/ram7                [       16.00 MB]
  /dev/ram8                [       16.00 MB]
  /dev/ram9                [       16.00 MB]
  /dev/ram10               [       16.00 MB]
  /dev/ram11               [       16.00 MB]
  /dev/ram12               [       16.00 MB]
  /dev/ram13               [       16.00 MB]
  /dev/ram14               [       16.00 MB]
  /dev/ram15               [       16.00 MB]
  /dev/sdb                 [        8.00 GB] LVM physical volume
  3 disks
  16 partitions
  1 LVM physical volume whole disk
  1 LVM physical volume
[root@www2 ~]#
 
And finally, our hard drives:
 
[root@www2 ~]# fdisk -l

 
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        1044     8281507+  8e  Linux LVM
 
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
Disk /dev/sdb doesn't contain a valid partition table
[root@www2 ~]#

 
At this time, I insert the hard drive.  This is done by adding another drive to the physical machine, or adding a drive using VMware.  Either way, add it, and reboot.
 
Now, when you do a fdisk -l, you will see a listing of drives, and my new drive (which is /dev/sdc)
 
[root@www2 ~]# fdisk -l

 
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        1044     8281507+  8e  Linux LVM
 
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
Disk /dev/sdb doesn't contain a valid partition table
 
Disk /dev/sdc: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
Disk /dev/sdc doesn't contain a valid partition table
 
Now, let’s create a partition
 
[root@www2 ~]# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
 
 
The number of cylinders for this disk is set to 3916.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
>p
Partition number (1-4): 1
First cylinder (1-3916, default 1): 1
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-3916, default 3916): 3916
Using default value 3916
 
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): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
[root@www2 ~]#

 
Let’s look at our disks, now that we have added a new drive and partitioned it.  Did our LVM Disks change?
 
[root@www2 ~]# lvmdiskscan
  /dev/ramdisk             [       16.00 MB]
  /dev/root                [       15.00 GB]
  /dev/ram                 [       16.00 MB]
  /dev/sda1                [      101.94 MB]
  /dev/VolGroup00/LogVol01 [      768.00 MB]
  /dev/ram2                [       16.00 MB]
  /dev/sda2                [        7.90 GB] LVM physical volume
  /dev/ram3                [       16.00 MB]
  /dev/ram4                [       16.00 MB]
  /dev/ram5                [       16.00 MB]
  /dev/ram6                [       16.00 MB]
  /dev/ram7                [       16.00 MB]
  /dev/ram8                [       16.00 MB]
  /dev/ram9                [       16.00 MB]
  /dev/ram10               [       16.00 MB]
  /dev/ram11               [       16.00 MB]
  /dev/ram12               [       16.00 MB]
  /dev/ram13               [       16.00 MB]
  /dev/ram14               [       16.00 MB]
  /dev/ram15               [       16.00 MB]
  /dev/sdb                 [        8.00 GB] LVM physical volume
  /dev/sdc1                [       30.00 GB]
  3 disks
  17 partitions
  1 LVM physical volume whole disk
  1 LVM physical volume
 
Now, let’s create a physical volume for the new partition of the new drive
 
[root@www2 ~]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created
 
Remind me again which Volume Groups we have?  We need to extend it to cover the new drive
 
[root@www2 ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "VolGroup00" using metadata type lvm2
 
Oh yeah, that’s right.  VolGroup00.  Lets extend the volume to include the new partitioned drive.
 
[root@www2 ~]# vgextend VolGroup00 /dev/sdc1
  Volume group "VolGroup00" successfully extended
 
Okay.  The Volume Group has been extended.  Now, we need to extend the Logical Volume.  Where is this LogVol?  I need to know what to expand.
 
[root@www2 ~]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                O9VkzE-BOec-nvse-7yPD-ebuR-2BTK-2DYqWk
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                15.00 GB
  Current LE             1440
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

 
 
Here, I am adding 30GB ( -L+30G) to the logical volume.  I know this, because I added a 30GB drive.  There are other ways to extend it, but I’m just using size.
 
[root@www2 ~]# lvextend -L+30G /dev/VolGroup00/LogVol00
  Extending logical volume LogVol00 to 45.00 GB
  Logical volume LogVol00 successfully resized
 
Now that the Logical Volume is resized, we need to expand the current file system.
 
[root@www2 ~]# resize2fs /dev/VolGroup00/LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 11796480 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 11796480 blocks long.
 
Done!!  

Share This Page : Share on TwitterShare on FacebookShare on GooglePlusShare on PinterestShare on Linkedin

About the author / 

Aaron Paxson

Aaron is a network engineer and global systems manager for a sewing machine company. His primary passion is in network systems and network management systems, though, his attention is usually distracted by other shiny new technologies. He loves programming in Java and Python to help out in his activities. His personal hobbies, when NOT doing tech, include Aquaria, WoodWorking, and Wine Making.

Related Posts

1Cloud Road

 
Find me on TwitterFind me on GooglePlusFind me on PinterestFind me on YouTubeFind me on LinkedinOpen my RSS Feed