硬盘扩容的事情每隔几个月就会遇到,可是流程命令总是记不牢靠,而扩容又是需要小心谨慎操作的,于是每做一次就要查一次命令,今天刚好又要扩容了,不如自己记一份吧:
这是一台centos纯Web节点,16G硬盘,马上要跑一个中间件,需要扩容到80G。
1、先看看磁盘空间占用情况
df -h
[root@149 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_base-lv_root
12G 4.7G 6.1G 44% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
/dev/sda1 477M 127M 325M 29% /boot
16G空间,已用4.7G,可用6.1G,swap3.9G。
2、查看块设备信息
lsblk
[root@149 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
sda 8:0 0 16G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 15.5G 0 part
├─vg_base-lv_root (dm-0) 253:0 0 11.6G 0 lvm /
└─vg_base-lv_swap (dm-1) 253:1 0 4G 0 lvm [SWAP]
这里更清楚些,盘/dev/sda 16G
3、为sda分区,提示No free sectors available就说明没有识别出新增加的空间,要reboot一下,再
lsblk
[root@149 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
sda 8:0 0 80G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 15.5G 0 part
├─vg_base-lv_root (dm-0) 253:0 0 11.6G 0 lvm /
└─vg_base-lv_swap (dm-1) 253:1 0 4G 0 lvm [SWAP]
/dev/sda已经识别为80G了。
fdisk /dev/sda
[root@149 ~]# 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 (2089-10443, default 2089):
Using default value 2089
Last cylinder, +cylinders or +size{K,M,G} (2089-10443, default 10443):
Using default value 10443
Command (m for help): p
Disk /dev/sda: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 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: 0x000c1400
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2089 16264192 8e Linux LVM
/dev/sda3 2089 10443 67106181+ 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: 设备或资源忙.
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.
这里先输n,表示新建分区,p表示主分区,3表示第3个分区(上图已经有了sda1、sda3,再次创建应该敲3),w存盘
提示设备忙,重新查询分区并没建立成功,没关系,直接重启。重启之后:
lsbkl
[root@149 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
sda 8:0 0 80G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 15.5G 0 part
│ ├─vg_base-lv_root (dm-0) 253:0 0 11.6G 0 lvm /
│ └─vg_base-lv_swap (dm-1) 253:1 0 4G 0 lvm [SWAP]
└─sda3 8:3 0 64G 0 part
看到新建的分区sda3,大小64G。
4、创建物理卷
先查一下,pvdisplay
[root@149 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_base
PV Size 15.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 3970
Free PE 0
Allocated PE 3970
PV UUID aIhV3j-Nv0x-8ddK-2Tb3-M1JK-nwUG-M78yi8
只有一个/dev/sda2,再创建一个,pvcreate /dev/sda3
[root@149 ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
再查一下,pvdisplay
[root@149 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_base
PV Size 15.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 3970
Free PE 0
Allocated PE 3970
PV UUID aIhV3j-Nv0x-8ddK-2Tb3-M1JK-nwUG-M78yi8
"/dev/sda3" is a new physical volume of "64.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 64.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID K9OJwZ-bwU2-D1DA-AX0A-FfH3-7xdM-6UWzss
5、将物理卷加入卷组
先查一下卷组,vgdisplay
[root@149 ~]# vgdisplay
--- Volume group ---
VG Name vg_base
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 15.51 GiB
PE Size 4.00 MiB
Total PE 3970
Alloc PE / Size 3970 / 15.51 GiB
Free PE / Size 0 / 0
VG UUID s4ngmz-Nrez-l3Nj-n5ul-R0Lx-zCr3-WbXVU1
将卷加入,vgextend vg_base /dev/sda3 。注意“vg_base”取自上图中的“VG Name”,每个节点有可能不同。
[root@149 ~]# vgextend vg_base /dev/sda3
Volume group "vg_base" successfully extended
再查一下卷组,vgdisplay
[root@149 ~]# vgdisplay
--- Volume group ---
VG Name vg_base
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
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 79.50 GiB
PE Size 4.00 MiB
Total PE 20353
Alloc PE / Size 3970 / 15.51 GiB
Free PE / Size 16383 / 64.00 GiB
VG UUID s4ngmz-Nrez-l3Nj-n5ul-R0Lx-zCr3-WbXVU1
这时可以看到变化,卷组的Free Size 由0增加到了64G。
6、将新增的空闲空间扩展到逻辑卷。
先查看一下逻辑卷,lvdisplay
[root@149 ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg_base/lv_root
LV Name lv_root
VG Name vg_base
LV UUID c1Aw9t-N1E4-yx9x-LM3Y-knx2-VF9i-iencD0
LV Write Access read/write
LV Creation host, time base.xnjd.cn, 2014-03-29 02:29:10 +0800
LV Status available
# open 1
LV Size 11.57 GiB
Current LE 2962
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Logical volume ---
LV Path /dev/vg_base/lv_swap
LV Name lv_swap
VG Name vg_base
LV UUID mtycRL-PSCg-pOYG-Lsf3-GWDX-jtNQ-QZVqKV
LV Write Access read/write
LV Creation host, time base.xnjd.cn, 2014-03-29 02:29:12 +0800
LV Status available
# open 1
LV Size 3.94 GiB
Current LE 1008
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
记住lv_root的Path,这里要用,下一步也要用。然后将新增的空闲空间扩展到逻辑卷,lvextend -l +100%FREE /dev/vg_base/lv_root
[root@149 ~]# lvextend -l +100%FREE /dev/vg_base/lv_root
Size of logical volume vg_base/lv_root changed from 11.57 GiB (2962 extents) to 75.57 GiB (19345 extents).
Logical volume lv_root successfully resized.
7、改变文件系统大小。
resize2fs /dev/vg_base/lv_root
[root@149 ~]# resize2fs /dev/vg_base/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_base/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 5
Performing an on-line resize of /dev/vg_base/lv_root to 19809280 (4k) blocks.
The filesystem on /dev/vg_base/lv_root is now 19809280 blocks long.
完成了。将第一步的命令再运行一次,做个对比。
df -h
[root@149 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_base-lv_root
75G 4.7G 66G 7% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
/dev/sda1 477M 133M 319M 30% /boot
80G空间,已用4.7G,可用66G,swap3.9G。可用空间由6.1G变为了66G。