Sunday, December 1, 2013

Resize Linux Partition under Virtualized Environment

1. Current environment:

Linux: Oracle Linux Version 6.4

[root@gc12c ~]# pvscan
PV /dev/sda2 VG vg_gc12c lvm2 [15.51 GiB / 0 free]
PV /dev/sdb1 VG vg_gc12c lvm2 [16.00 GiB / 0 free]
Total: 2 [31.50 GiB] / in use: 2 [31.50 GiB] / in no VG: 0 [0 ]

[root@gc12c ~]# vgdisplay
--- Volume group ---
VG Name vg_gc12c
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 31.50 GiB
PE Size 4.00 MiB
Total PE 8065
Alloc PE / Size 8065 / 31.50 GiB
Free PE / Size 0 / 0
VG UUID 0cFw0g-f4om-v3iD-8JTF-1CZu-flY5-K5kfZv




2. Poweroff the machine, remove any snapshots, and extend the virtual disk



In this case, /dev/sdb extended from 16GB to 32GB.



After this, boot the virtual machine.



3. Poweron the machine, create new partition for the “extended” size



[root@gc12c ~]# fdisk /dev/sdb

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): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (2089-4177, default 2089):
Using default value 2089
Last cylinder, +cylinders or +size{K,M,G} (2089-4177, default 4177):
Using default value 4177

Command (m for help): p

Disk /dev/sdb: 34.4 GB, 34359738368 bytes
255 heads, 63 sectors/track, 4177 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: 0x7d9b4852

Device Boot Start End Blocks Id System
/dev/sdb1 1 2089 16776192 8e Linux LVM
/dev/sdb2 2089 4177 16774536+ 83 Linux


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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

[root@gc12c ~]# ls -l /dev/sd
sda sda1 sda2 sdb sdb1




4. Perform reboot, ensure new partition detected by OS




[root@gc12c ~]# reboot

[root@gc12c ~]# ls -l /dev/sdb*
brw-rw----. 1 root disk 8, 16 Dec 1 20:13 /dev/sdb
brw-rw----. 1 root disk 8, 17 Dec 1 20:13 /dev/sdb1
brw-rw----. 1 root disk 8, 18 Dec 1 20:13 /dev/sdb2






5. Create PV disk



[root@gc12c ~]# pvdisplay /dev/sdb2
"/dev/sdb2" is a new physical volume of "16.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 16.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID nDzFDP-bF2d-MOs6-wS0Q-fWPT-h0Tt-FMmOrg




6. Extend the VG to include this PV



root@gc12c ~]# vgextend vg_gc12c /dev/sdb2
Volume group "vg_gc12c" successfully extended
[root@gc12c ~]# vgdisplay
--- Volume group ---
VG Name vg_gc12c
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 7
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 3
Act PV 3
VG Size 47.50 GiB
PE Size 4.00 MiB
Total PE 12160
Alloc PE / Size 8065 / 31.50 GiB
Free PE / Size 4095 / 16.00 GiB
VG UUID 0cFw0g-f4om-v3iD-8JTF-1CZu-flY5-K5kfZv


7. Extend the size of LV



[root@gc12c ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg_gc12c/lv_root
LV Name lv_root
VG Name vg_gc12c
LV UUID 0R4XM3-YTlO-nVs0-ZXWU-VG1t-6NBG-FhxjBK
LV Write Access read/write
LV Creation host, time gc12c.dbaglobe.com, 2013-12-01 14:20:04 +0800
LV Status available
# open 1
LV Size 27.63 GiB
Current LE 7073
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0

--- Logical volume ---
LV Path /dev/vg_gc12c/lv_swap
LV Name lv_swap
VG Name vg_gc12c
LV UUID 5ReO9i-BS9M-yfXl-jSp6-MObn-eZ9p-R3GuEQ
LV Write Access read/write
LV Creation host, time gc12c.dbaglobe.com, 2013-12-01 14:20:09 +0800
LV Status available
# open 2
LV Size 3.88 GiB
Current LE 992
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:1

[root@gc12c ~]# lvextend -L 8G /dev/vg_gc12c/lv_swap
Extending logical volume lv_swap to 8.00 GiB


[root@gc12c ~]# lvextend -L +11G /dev/vg_gc12c/lv_root
Extending logical volume lv_root to 38.63 GiB
Logical volume lv_root successfully resized

[root@gc12c ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg_gc12c/lv_root
LV Name lv_root
VG Name vg_gc12c
LV UUID 0R4XM3-YTlO-nVs0-ZXWU-VG1t-6NBG-FhxjBK
LV Write Access read/write
LV Creation host, time gc12c.dbaglobe.com, 2013-12-01 14:20:04 +0800
LV Status available
# open 1
LV Size 38.63 GiB
Current LE 9889
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0

--- Logical volume ---
LV Path /dev/vg_gc12c/lv_swap
LV Name lv_swap
VG Name vg_gc12c
LV UUID 5ReO9i-BS9M-yfXl-jSp6-MObn-eZ9p-R3GuEQ
LV Write Access read/write
LV Creation host, time gc12c.dbaglobe.com, 2013-12-01 14:20:09 +0800
LV Status available
# open 2
LV Size 8.00 GiB
Current LE 2048
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:1



8. Resize the root file system



[root@gc12c ~]# resize2fs /dev/vg_gc12c/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_gc12c/lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/vg_gc12c/lv_root to 10126336 (4k) blocks.
The filesystem on /dev/vg_gc12c/lv_root is now 10126336 blocks long.

[root@gc12c ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_gc12c-lv_root
39G 5.3G 31G 15% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
/dev/sda1 485M 55M 405M 12% /boot




9. Resize the swap size



[root@gc12c ~]# swapoff /dev/vg_gc12c/lv_swap
[root@gc12c ~]# mkswap /dev/vg_gc12c/lv_swap
mkswap: /dev/vg_gc12c/lv_swap: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 8388604 KiB
no label, UUID=63f4007b-3f76-403c-9cf0-8ce2dfcc0a7a
[root@gc12c ~]# swapon /dev/vg_gc12c/lv_swap
[root@gc12c ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 8388604 0 -1