您的位置:首页 > 运维架构 > Linux

Extending A Linux Disk With LVM–Extending Root Par

2016-02-23 10:50 721 查看

Extending A Linux Disk With LVM – Extending Root Partition

Posted on December 23, 2012 by Ben · 51 Comments

A quick tutorial of something I did today. We use vmware here a lot and instead of installing an OS each time a VM is requested, we have operating systems installed on VM templates “ready-to-go” and then deploy the template to a VM per request.
As you can guess, the VM templates are “minimal” on resources (including disk space) so when we get a VM request, we often have to extend the root partition per the user requirement.
So here are my exact steps (no cutting anything out) using the LVM (Logical Volume Manager) commands to extend a root disk. OS is Centos 6.3 a.k.a. Redhat 6.3 on a VM.
NOTE: If you are not using a VM but a physical server and adding disks to the chassis or via a SAN, all the commands are the same, you’d just be using different disk devices like /dev/sdb, or /dev/sdc, and the like.




A few terms

Sponsored Links

PV = Physical Volume. When you add a new disk or new partition, you need to designate it as a PV so it can be used by the LVM
VG = Volume Group. This is a group of PVs
LV = Logical Volume. This is an abstraction of disk space carved out from a Volume Group (VG) which the OS can then use just as it would a regular hard drive
PE = Physical Extents. Think of this like “blocks”. When you do a “vgdisplay” you will see the PE size (mine was 4mb), the total number of PE’s in the VG and the free PEs available. With some multiplication you would calculate out the total size of the VG and the total free space available on the VG.

Here are my exact linux commands in a nutshell:

Sponsored Links

# df -h
To see the current disk usage
# fdisk -l
To see the current partitioning
# poweroff
Turn the VM off so we can extend the disk on the VM side. For physical servers you would just be adding a new SAS/SATA disk to the chassis or adding a LUN on a SAN and presenting it which does *not* necessarily require a poweroff
Intermission to add new disk and power on if necessary
# fdisk -l
To see the new disk size on /dev/sda which we just extended, or to see new disks we just presented (/dev/sdb, /dev/sdc, etc.)
# fdisk /dev/sda
To create new partitions for the OS to use. These new partitions will be added to the VG so we can extend the LV that the “/” partition is on. My newly created partition in the example is /dev/sda3. For new disks you would use /dev/sdb, or /dev/sdc. If you are using an extended /dev/sda like in my case, you will need to reboot for the changes to be seen
# fdisk -l
To see the new partitions
# pvdisplay
View current physical volumes a.k.a. pv
# pvcreate /dev/sda3
Allow Linux OS to use the new partition in LVM
# pvdisplay
See the new pv /dev/sda3
# vgdisplay
View the current volume groups
# vgextend vg_cents6364bit /dev/sda3
Add the new PV /dev/sda3 to the existing VG vg_cents6364bit
# vgdisplay
Now you can see the new size of the VG vg_cents6364bit. Note the new amount of free PE’s (physical extents)
# lvdisplay
View the current LV. In my example, /dev/vg_cents6364bit/lv_root which is the root partition
# lvextend -l +2559 /dev/vg_cents6364bit/lv_root
Now make the LV larger. Growing the LV /dev/vg_cents6364bit/lv_root by 2559 PEs
# lvdisplay
Now you can see the larger size of the LV
# resize2fs /dev/vg_cents6364bit/lv_root
Online resize of the actual filesystem now on that LV
# df -h
You can see the new size now using the “df” command

DONE!!

Here are the commands and the actual screen output in detail

Again, specific to my example of growing the single disk in the chassis on the VM side, commands will be similar if you are adding disks via SAN or in the chassis or even adding a VM disk
View current disk with df -h
[root@cents63-64bit ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_cents6364bit-lv_root
7.5G 4.5G 2.7G 64% /
tmpfs 499M 112K 499M 1% /dev/shm
/dev/sda1 485M 49M 411M 11% /boot
[root@cents63-64bit ~]#

View current disk geometry with fdisk -l
[root@cents63-64bit ~]# fdisk -l



Disk /dev/sda: 10.7 GB, 10737418240 bytes
64 heads, 32 sectors/track, 10240 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0001210c

Device Boot Start End Blocks Id System
/dev/sda1 * 2 501 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 502 10240 9972736 8e Linux LVM
Partition 2 does not end on cylinder boundary.

Disk /dev/mapper/vg_cents6364bit-lv_root: 8095 MB, 8095006720 bytes
255 heads, 63 sectors/track, 984 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: 0x00000000

Disk /dev/mapper/vg_cents6364bit-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 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: 0x00000000


[root@cents63-64bit ~]#

Poweroff so we can present/view the new disks or new disk geometry
[root@cents63-64bit ~]# poweroff

On the VM side, this is nothing more than just going into the “Edit Settings” and changing the disk size to something larger. In this case I changed it to 20gig from 10gig.
Power it on now.
Use fdisk -l to see disk geometry
[root@cents63-64bit ~]# fdisk -l



Disk /dev/sda: 21.5 GB, 21474836480 bytes
64 heads, 32 sectors/track, 20480 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0001210c

Device Boot Start End Blocks Id System
/dev/sda1 * 2 501 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 502 10240 9972736 8e Linux LVM
Partition 2 does not end on cylinder boundary.

Disk /dev/mapper/vg_cents6364bit-lv_root: 8095 MB, 8095006720 bytes
255 heads, 63 sectors/track, 984 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: 0x00000000

Disk /dev/mapper/vg_cents6364bit-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 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: 0x00000000


[root@cents63-64bit ~]#

First thing to do is to create a new partition /dev/sda3. If you are adding new disks, you’d create a new /dev/sdb1 or /dev/sdc1, etc.
Create the partition using fdisk command
[root@cents63-64bit ~]# fdisk /dev/sda



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): 3
First cylinder (1-20480, default 1): 10241
Last cylinder, +cylinders or +size{K,M,G} (10241-20480, default 20480):
Using default value 20480

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@cents63-64bit ~]#

Reboot server and do a fdisk -l to see changes
[root@cents63-64bit ~]# fdisk -l



Disk /dev/sda: 21.5 GB, 21474836480 bytes
64 heads, 32 sectors/track, 20480 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0001210c

Device Boot Start End Blocks Id System
/dev/sda1 * 2 501 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 502 10240 9972736 8e Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/sda3 10241 20480 10485760 83 Linux

Disk /dev/mapper/vg_cents6364bit-lv_root: 8095 MB, 8095006720 bytes
255 heads, 63 sectors/track, 984 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: 0x00000000

Disk /dev/mapper/vg_cents6364bit-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 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: 0x00000000


[root@cents63-64bit ~]#

pvdisplay to see current PVs
[root@cents63-64bit ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_cents6364bit
PV Size 9.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 2434
Free PE 0
Allocated PE 2434
PV UUID SfGdvH-Lb0a-Kmrl-Uo9J-66aG-80T8-OsfPqK



[root@cents63-64bit ~]#

pvcreate to add the new partition as a PV for use in the LVM

[root@cents63-64bit ~]# pvcreate /dev/sda3
Writing physical volume data to disk "/dev/sda3"
Physical volume "/dev/sda3" successfully created
[root@cents63-64bit ~]#

Use pvdisplay to see your new PVs in the PV list
[root@cents63-64bit ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_cents6364bit
PV Size 9.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 2434
Free PE 0
Allocated PE 2434
PV UUID SfGdvH-Lb0a-Kmrl-Uo9J-66aG-80T8-OsfPqK



"/dev/sda3" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID HLtl0o-UtMC-kWwp-zcJW-oG2e-Aj5T-Km5nF8


[root@cents63-64bit ~]#

Use vgdisplay to find the VG name you want to add the new PV to

[root@cents63-64bit ~]# vgdisplay
--- Volume group ---
VG Name vg_cents6364bit
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 9.51 GiB
PE Size 4.00 MiB
Total PE 2434
Alloc PE / Size 2434 / 9.51 GiB
Free PE / Size 0 / 0
VG UUID WxnNuc-rHwk-NBVF-10kX-xgoC-u5Ns-DNXhJz
[root@cents63-64bit ~]#

Use vgextend to add the PV to it
[root@cents63-64bit ~]# vgextend vg_cents6364bit /dev/sda3
Volume group "vg_cents6364bit" successfully extended
[root@cents63-64bit ~]#




Use vgdisplay to see the new size of the VG. Note the TOTAL PE and FREE PE have increased

[root@cents63-64bit ~]# vgdisplay
--- Volume group ---
VG Name vg_cents6364bit
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
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 19.50 GiB
PE Size 4.00 MiB
Total PE 4993
Alloc PE / Size 2434 / 9.51 GiB
Free PE / Size 2559 / 10.00 GiB
VG UUID WxnNuc-rHwk-NBVF-10kX-xgoC-u5Ns-DNXhJz
[root@cents63-64bit ~]#

Now find the LV to extend. Use lvdisplay to get an LV list
[root@cents63-64bit ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg_cents6364bit/lv_root
LV Name lv_root
VG Name vg_cents6364bit
LV UUID oLUr3z-9omt-bX4S-APi3-Veov-2rKr-XymbzO
LV Write Access read/write
LV Creation host, time cents63-64bit, 2012-10-26 16:49:32 -0700
LV Status available
# open 1
LV Size 7.54 GiB
Current LE 1930
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0



--- Logical volume ---
LV Path /dev/vg_cents6364bit/lv_swap
LV Name lv_swap
VG Name vg_cents6364bit
LV UUID sbDqbF-RGPn-PdaZ-BBRd-JHTb-qnh9-X2rf03
LV Write Access read/write
LV Creation host, time cents63-64bit, 2012-10-26 16:49:45 -0700
LV Status available
# open 1
LV Size 1.97 GiB
Current LE 504
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
[root@cents63-64bit ~]#

Grow the LV using lvextend. For arguments you need the number of PE’s to grow the LV by and the LV name itself

[root@cents63-64bit ~]# lvextend -l +2559 /dev/vg_cents6364bit/lv_root
Extending logical volume lv_root to 17.54 GiB
Logical volume lv_root successfully resized
[root@cents63-64bit ~]#

lvdisplay command shows the new size
[root@cents63-64bit ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg_cents6364bit/lv_root
LV Name lv_root
VG Name vg_cents6364bit
LV UUID oLUr3z-9omt-bX4S-APi3-Veov-2rKr-XymbzO
LV Write Access read/write
LV Creation host, time cents63-64bit, 2012-10-26 16:49:32 -0700
LV Status available
# open 1
LV Size 17.54 GiB
Current LE 4489
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0



--- Logical volume ---
LV Path /dev/vg_cents6364bit/lv_swap
LV Name lv_swap
VG Name vg_cents6364bit
LV UUID sbDqbF-RGPn-PdaZ-BBRd-JHTb-qnh9-X2rf03
LV Write Access read/write
LV Creation host, time cents63-64bit, 2012-10-26 16:49:45 -0700
LV Status available
# open 1
LV Size 1.97 GiB
Current LE 504
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
[root@cents63-64bit ~]#

Use the resize2fs to grow the filesystem itself.
Some OS may not be able to do it while the filesystem is mounted which means you’d need to boot into a failsafe or rescue mode. With Centos 6.3 it was able to do it on-the-fly while mounted
[root@cents63-64bit ~]# resize2fs /dev/vg_cents6364bit/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_cents6364bit/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/vg_cents6364bit/lv_root to 4596736 (4k) blocks.
The filesystem on /dev/vg_cents6364bit/lv_root is now 4596736 blocks long.

Use df command to see the new filesystem size

[root@cents63-64bit ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_cents6364bit-lv_root
18G 4.5G 12G 28% /
tmpfs 499M 272K 499M 1% /dev/shm
/dev/sda1 485M 49M 411M 11% /boot
[root@cents63-64bit ~]#


DONE!

Good luck and have fun!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: