November 18, 2011, Friday, 321

Extend existing LVM root partition without rebooting

From ezUnix

Jump to: navigation, search
                                    pdf_icon.png Download this article as a single PDF document 

Introduction

I sometimes have to add a virtual HD to an existing virtual host running in VMWare and extend the host`s root partition without having to reboot it.
This can be easily done if the root partition on my Linux system is already located on an LVM volume.


Execution

After you assigned a new disk to the virtual machine you have to add it in the guest and expand the LVM volume.
First rescan the SCSI bus to make the kernel detect the new virtual disk:

# echo "- - -" > /sys/class/scsi_host/host0/scan

Find out what the new device is called, should be displayed with 'dmesg'. If it is too noisy, you may get what you need with

# dmesg | grep sd[abcde]

Use fdisk to create a single partition on the new device, make it type 8e (LVM).

Initialize the new partition as a physical volume:

# pvcreate /dev/sdX1

Extend your volume group with the new PV:

# vgextend yourvolumegroup /dev/sdX1

Extend your logical volume with the free space of the VG:

# lvextend -l +100%FREE /dev/yourvolumegroup/yourlogicalvolume

Resize the filesystem online (only ext3 and ext4!):

# resize2fs /dev/mapper/yourvolumegroup-yourlogicalvolume

Verify your new partitions free space with 'df -h'.


Live Example

In this example we will use commands extended to extend root partition with 20Gb.

In our example the / was 16Gb and almost full:

# df -h /
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/localvg-root
                       16G   15G  412M  98% /


First we add a new virtual drive to the VmWare host, reread the SCSI bus and check the dmesg to see if the process was successful:

# dmesg
[1900683.028115] scsi 0:0:2:0: Direct-Access     VMware   Virtual disk     1.0  PQ: 0 ANSI: 2
[1900683.028127]  target0:0:2: Beginning Domain Validation
[1900683.031944]  target0:0:2: Domain Validation skipping write tests
[1900683.031946]  target0:0:2: Ending Domain Validation
[1900683.032163]  target0:0:2: FAST-40 WIDE SCSI 80.0 MB/s ST (25 ns, offset 127)
[1900683.032796] sd 0:0:2:0: [sdc] 41943040 512-byte hardware sectors (21475 MB)
[1900683.033357] sd 0:0:2:0: [sdc] Test WP failed, assume Write Enabled
[1900683.033536] sd 0:0:2:0: [sdc] Cache data unavailable
[1900683.033538] sd 0:0:2:0: [sdc] Assuming drive cache: write through
[1900683.034207] sd 0:0:2:0: [sdc] 41943040 512-byte hardware sectors (21475 MB)
[1900683.034734] sd 0:0:2:0: [sdc] Test WP failed, assume Write Enabled
[1900683.034911] sd 0:0:2:0: [sdc] Cache data unavailable
[1900683.034912] sd 0:0:2:0: [sdc] Assuming drive cache: write through
[1900683.035191]  sdc: unknown partition table
[1900683.070657] sd 0:0:2:0: [sdc] Attached SCSI disk
[1900683.070715] sd 0:0:2:0: Attached scsi generic sg3 type 0

Then we check the LVM info:

# lvdisplay /dev/mapper/localvg-root
 --- Logical volume ---
 LV Name                /dev/localvg/root
 VG Name                localvg
 LV UUID                joih9S-vbJj-04NM-fbld-9e7Q-h9M4-Bnpaoa
 LV Write Access        read/write
 LV Status              available
 # open                 1
 LV Size                15.29 GB
 Current LE             3915
 Segments               2
 Allocation             inherit
 Read ahead sectors     0
 Block device           254:0

Time to partition the newly added device:

# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xe8d635ce.
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 2610.
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)

Let's see what device looks like before partitioning:

Command (m for help): p

Disk /dev/sdc: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xe8d635ce

  Device Boot      Start         End      Blocks   Id  System

As expected, no partitions here. Now let's create new partition:

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

Let's see the newly created partition:

Command (m for help): p

Disk /dev/sdc: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xe8d635ce

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        2610    20964793+  83  Linux

Now we need to change the partition type and set it to Linux LVM:

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/sdc: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xe8d635ce

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        2610    20964793+  8e  Linux LVM

Now write the changes:

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

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

Now add newly created device to the LVM volume:

# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created

# vgextend localvg /dev/sdc1
  Volume group "localvg" successfully extended

# lvextend -l +100%FREE /dev/localvg/root
  Extending logical volume root to 35.36 GB
  Logical volume root successfully resized

And finally resize the / partition:

# resize2fs /dev/mapper/localvg-root
resize2fs 1.40.8 (13-Mar-2008)
Filesystem at /dev/mapper/localvg-root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 3
Performing an on-line resize of /dev/mapper/localvg-root to 9270272 (4k) blocks.
The filesystem on /dev/mapper/localvg-root is now 9270272 blocks long.

Check the size of it:

# df -h /
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/localvg-root
                      36G   15G   20G  43% /


That's all folks.
Marcin


<comments />
Talk:Extend existing LVM root partition without rebooting