Extending a LVM partition in a Hyper-V guest VM running CentOS

Imagine a scenario like the following: you have a Hyper-V host and a CentOS 6.3 guest. The CentOS guest has Microsoft Integration Services for Linux 3.3 installed and running. A data file system on the guest is running out of the free space:

[root@myhost ~]# df -h /data/svn/
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/data-svn   99G   93G  964M  99% /data/svn


Although the partition is a part of LVM, but your entire disk is almost full. In the article I'd like to show how you can get rid of this without getting your services offline.

As first a new HDD must be created on the Hyper-V host side. For data HDDs I prefer to create SCSI HDDs. To get it calm for a while, I'm creating a 128GByte HDD of the type fixed:


After the new HDD is created, it is visible to the guest and can be partitioned immediately:

[root@myhost ~]# tail /varlog/messages
Aug  1 17:49:04 myhost kernel: scsi 1:0:0:1: Direct-Access     Msft     Virtual Disk     1.0  PQ: 0 ANSI: 4
Aug  1 17:49:04 myhost kernel: sd 1:0:0:1: Attached scsi generic sg2 type 0
Aug  1 17:49:04 myhost kernel: sd 1:0:0:1: [sdc] 268435456 512-byte logical blocks: (137 GB/128 GiB)
Aug  1 17:49:04 myhost kernel: sd 1:0:0:1: [sdc] Write Protect is off
Aug  1 17:49:04 myhost kernel: sd 1:0:0:1: [sdc] Write cache: enabled, read cache: enabled, supports DPO and FUA
Aug  1 17:49:04 myhost kernel: sdc: unknown partition table
Aug  1 17:49:04 myhost kernel: sd 1:0:0:1: [sdc] Attached SCSI disk

Create a new partition on the new disk according your needs but set the partition type to 8e (Linux LVM):

[root@myhost ~]# 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 0xa05e1ec8.
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): u
Changing display/entry units to sectors

Command (m for help): c
DOS Compatibility flag is not set

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-268435455, default 2048):ENTER
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-268435455, default 268435455):ENTER
Using default value 268435455

Command (m for help): p

Disk /dev/sdc: 137.4 GB, 137438953472 bytes
255 heads, 63 sectors/track, 16709 cylinders, total 268435456 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 identifier: 0xa05e1ec8

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048   268435455   134216704   83  Linux

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): wq
The partition table has been altered!

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

After the new partition has been created, a new physical volume must be created:

[root@myhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  system lvm2 a--    9.50g 4.25g
  /dev/sdb1  data   lvm2 a--  128.00g 8.00g
[root@myhost ~]# pvcreate /dev/sdc1
  Writing physical volume data to disk "/dev/sdc1"
  Physical volume "/dev/sdc1" successfully created
[root@myhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  system lvm2 a--    9.50g   4.25g
  /dev/sdb1  data   lvm2 a--  128.00g   8.00g
  /dev/sdc1         lvm2 a--  128.00g 128.00g

As you can see at this step the new physical volume is known to the kernel.
Now it's time to extend the volume group, which the concerned partition with low free space belongs to:

[root@myhost ~]# vgextend data /dev/sdc1
  Volume group "data" successfully extended
[root@myhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  data     2   3   0 wz--n- 255.99g 135.99g
  system   1   7   0 wz--n-   9.50g   4.25g

At this point the volume group 'data' has been extended. Now the logical volume, which concerned partition is located on, must be extended:

[root@myhost ~]# lvextend -L +10G /dev/data/svn
  Extending logical volume svn to 110.00 GiB
  Logical volume svn successfully resized

After this has been done the file system must be extended (I have a ext4 file system here, but it should for any other file systems, that support LVM, the same way):

[root@myhost ~]# resize2fs /dev/data/svn
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/data/svn is mounted on /data/svn; on-line resizing required
old desc_blocks = 7, new_desc_blocks = 7
Performing an on-line resize of /dev/data/svn to 28835840 (4k) blocks.
The filesystem on /dev/data/svn is now 28835840 blocks long.

And voila! The file system has some free space again and you can go home ;-)

[root@myhost ~]# df -h /data/svn/
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/data-svn  109G   92G   12G  89% /data/svn

0 Kommentare :: Extending a LVM partition in a Hyper-V guest VM running CentOS

Post a Comment