使用AMAZON上的CentOS-6.5-base-20150305 (ami-0e80db66)的IAM启动了一个EC2实例,配置EBS存储200G。进入系统后,使用df查看存储空间如下:
[ec2-user@s1 ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 14G 2.4G 11G 19% /
tmpfs 7.3G 0 7.3G 0% /dev/shm
/dev/xvda1 477M 73M 379M 17% /boot
很明显,这里只显示了IAM默认加载的大约20G空间,我的EBS有200G,还有180G没有挂载。这是什么情况呢?使用lsblk来看看:
[ec2-user@s1 ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 200G 0 disk
├─xvda1 202:1 0 500M 0 part /boot
├─xvda2 202:2 0 15.5G 0 part
├─VolGroup-lv_root (dm-0) 253:0 0 13.9G 0 lvm /
└─VolGroup-lv_swap (dm-1) 253:1 0 1.6G 0 lvm [SWAP]
可以看出,xvda被分成了2个分区,第一个分区500M挂载了/boot目录,第二个分区是个lvm分区,挂载了/和swap目录。接下来,我要把这180G空间挂载到 / 目录,也就是扩展 VolGroup-lv_root到195G左右。
第一步,这是一个root EBS,所以无法在当前实例执行硬盘分区操作。将这个EBS从当前实例断开,同时挂载到另外一个工具实例(我使用了一个免费的Amazon Linux AMI 2015.09.1 (HVM), SSD Volume Type - ami-60b6c60a实例)。注意:由于此时新实例有2个root EBS,此时不能对工具实例做重启之类的操作,否则无法正确加载root EBS。然后使用parted分区工具对此EBS做分区操作,产生一个新的物理分区xvda3,大小为剩下的180G。操作如下:
[ec2-user@s1~]$ sudo parted /dev/xvda
GNU Parted 2.1
使用 /dev/xvda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) unit s
(parted) print
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvda: 419430400s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system 标志
1 2048s 1026047s 1024000s primary ext4 启动
2 1026048s 33554431s 32528384s primary lvm
(parted) mkpart primary 33554432s 100%
(parted) print
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvda: 419430400s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system 标志
1 2048s 1026047s 1024000s primary ext4 启动
2 1026048s 33554431s 32528384s primary lvm
3 33554432s 419430399s 385875968s primary ext4
(parted) quit
这样就产生了一个新的物理分区,再次使用lsblk查看:
[ec2-user@s1 ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 200G 0 disk
├─xvda1 202:1 0 500M 0 part /boot
├─xvda2 202:2 0 15.5G 0 part
│ ├─VolGroup-lv_root (dm-0) 253:0 0 13.9G 0 lvm /
│ └─VolGroup-lv_swap (dm-1) 253:1 0 1.6G 0 lvm [SWAP]
└─xvda3 202:3 0 184G 0 part
分区操作完成,断开EBS,重新挂到原来的实例上。
第二步,调整LVM分区。 进入EC2实例,查看LVM的分区情况:
[ec2-user@s1 ~]$ sudo vgdisplay
--- Volume group ---
VG Name VolGroup
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 4431gC-stvB-LEil-fhM7-WT2F-y4DG-36Uf6w
[ec2-user@s1 ~]$ sudo lvdisplay
--- Logical volume ---
LV Path /dev/VolGroup/lv_root
LV Name lv_root
VG Name VolGroup
LV UUID UTPDa9-4C6b-S0Yk-npmP-HMOM-ZGs0-TctA1e
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2015-03-05 19:27:09 +0900
LV Status available
# open 1
LV Size 13.91 GiB
Current LE 3561
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Logical volume ---
LV Path /dev/VolGroup/lv_swap
LV Name lv_swap
VG Name VolGroup
LV UUID vlCsmT-W0HL-q3wo-3tWf-jL2A-e2mB-C6zAGl
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2015-03-05 19:27:36 +0900
LV Status available
# open 1
LV Size 1.60 GiB
Current LE 409
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
当前卷组VolGroup有2个LV,把新的分区xvda3转换为物理卷:
pvcreate /dev/xvda3 /dev/xvda3
扩展卷组,将物理卷xvda3添加到已存在的卷组VolGroup中
vgextend VolGroup /dev/xvda3
增大逻辑卷lv_root大小
lvextend -L +180G /dev/VolGroup/lv_root
再查看存储结构:
[ec2-user@s1 ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 200G 0 disk
├─xvda1 202:1 0 500M 0 part /boot
├─xvda2 202:2 0 15.5G 0 part
│ ├─VolGroup-lv_root (dm-0) 253:0 0 197.9G 0 lvm /
│ └─VolGroup-lv_swap (dm-1) 253:1 0 1.6G 0 lvm [SWAP]
└─xvda3 202:3 0 184G 0 part
└─VolGroup-lv_root (dm-0) 253:0 0 197.9G 0 lvm /
根目录已经增加到近198G,成功扩展。