说明:此操作在centos7下进行,如果是centos6发行版,需要注意格式化LV的文件系统类型(centos7.0开始默认文件系统是xfs,centos6是ext4)、最后一步写入系统的类型
查看当前磁盘[root@fsddxtclgj9vm900419 ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on/dev/mapper/centos-root xfs 44G 3.7G 41G 9% /devtmpfs devtmpfs 7.8G 0 7.8G 0% /devtmpfs tmpfs 7.8G 0 7.8G 0% /dev/shmtmpfs tmpfs 7.8G 777M 7.0G 10% /runtmpfs tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup/dev/vda1 xfs 1014M 179M 836M 18% /boottmpfs tmpfs 1.6G 36K 1.6G 1% /run/user/0tmpfs tmpfs 1.6G 40K 1.6G 1% /run/user/1001
查看块分区
[root@fsddxtclgj9vm900419 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTvda 252:0 0 50G 0 disk ├─vda1 252:1 0 1G 0 part /boot└─vda2 252:2 0 49G 0 part ├─centos-root 253:0 0 44G 0 lvm / └─centos-swap 253:1 0 5G 0 lvm [SWAP]vdb 252:16 0 974G 0 disk
可以看到 /dev/vdb 为新增的磁盘
格式化磁盘[root@fsddxtclgj9vm900419 ~]# parted /dev/vdbGNU Parted 3.1Using /dev/vdbWelcome to GNU Parted! Type 'help' to view a list of commands.(parted) printError: /dev/vdb: unrecognised disk labelModel: Virtio Block Device (virtblk) Disk /dev/vdb: 1046GBSector size (logical/physical): 512B/512BPartition Table: unknownDisk Flags: (parted) mklabel gpt (parted) mkpart primary 0 1045GBWarning: The resulting partition is not properly aligned for best performance.Ignore/Cancel? i (parted) print Model: Virtio Block Device (virtblk)Disk /dev/vdb: 1046GBSector size (logical/physical): 512B/512BPartition Table: gptDisk Flags: Number Start End Size File system Name Flags 1 17.4kB 1045GB 1045GB primary(parted) set 1 lvm on (parted) printModel: Virtio Block Device (virtblk)Disk /dev/vdb: 1046GBSector size (logical/physical): 512B/512BPartition Table: gptDisk Flags: Number Start End Size File system Name Flags 1 17.4kB 1045GB 1045GB primary lvm(parted) quit Information: You may need to update /etc/fstab
以上 (parted) 开头的代表输入的命令,具体功能可以通过 help 查看
重读分区表partprobe /dev/vdb
重新查看块分区
[root@fsddxtclgj9vm900419 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTvda 252:0 0 50G 0 disk ├─vda1 252:1 0 1G 0 part /boot└─vda2 252:2 0 49G 0 part ├─centos-root 253:0 0 44G 0 lvm / └─centos-swap 253:1 0 5G 0 lvm [SWAP]vdb 252:16 0 974G 0 disk └─vdb1 252:17 0 973.2G 0 part
可以看到vdb下多了vdb1,这个用来创建pv
创建PV[root@fsddxtclgj9vm900419 ~]# pvcreate -v /dev/vdb1 Wiping internal VG cache Wiping cache of LVM-capable devices Wiping signatures on new PV /dev/vdb1. Set up physical volume for "/dev/vdb1" with 2041015592 available sectors. Zeroing start of device /dev/vdb1. Writing physical volume data to disk "/dev/vdb1". Physical volume "/dev/vdb1" successfully created.
注意
PV创建好之后,到这一步可以选择扩展或者新建挂载点,扩容的前提条件是扩容挂载点的磁盘格式是LVM格式,这里先演示新建挂载点然后删除挂载点并扩容
新增LVM挂载查看PV[root@fsddxtclgj9vm900419 ~]# pvdisplay --- Physical volume --- PV Name /dev/vda2 VG Name centos PV Size <49.00 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 12543 Free PE 0 Allocated PE 12543 PV UUID CCUeq0-ZnG9-iUY8-dPOj-fVOa-XC10-ooGyle "/dev/vdb1" is a new physical volume of "973.23 GiB" --- NEW Physical volume --- PV Name /dev/vdb1 VG Name PV Size 973.23 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID vUoC7C-MIP2-iwXr-0SWu-c2Wz-yb6o-qFrfo1
注意看 VG Name,前面的是系统安装时选择LVM格式的,后面的还没有创建,所以 VG Name 为空
新建VG[root@fsddxtclgj9vm900419 ~]# vgcreate -s 4M vg01 /dev/vdb1 Volume group "vg01" successfully created
查看VG
[root@fsddxtclgj9vm900419 ~]# vgdisplay --- Volume group --- VG Name vg01 System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 1 Act PV 1 VG Size 973.23 GiB PE Size 4.00 MiB Total PE 249147 Alloc PE / Size 0 / 0 Free PE / Size 249147 / 973.23 GiB VG UUID hb8zoy-kKjU-VXD5-EjCN-8hS2-MOzf-lq3O5j --- Volume group --- VG Name centos 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 <49.00 GiB PE Size 4.00 MiB Total PE 12543 Alloc PE / Size 12543 / <49.00 GiB Free PE / Size 0 / 0 VG UUID kSwsMj-4FKB-zwFq-7MBq-EfOD-rccg-HC59x9
可以看到 vg01 为新建的VG
新建LV[root@fsddxtclgj9vm900419 ~]# lvcreate -l 100%FREE -n lv01 vg01 Logical volume "lv01" created
格式化LV
格式化文件系统类型有xfs,ext4,这里测试使用ext4格式,默认centos7下使用xfs格式,centos6为ext4格式
[root@fsddxtclgj9vm900419 ~]# mkfs.ext4 /dev/vg01/lv01mke2fs 1.42.9 (28-Dec-2013)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks63782912 inodes, 255126528 blocks12756326 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=24033361927786 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 102400000, 214990848Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: done
新建挂载点并挂载
mkdir /appmount /dev/vg01/lv01 /app# 永久写入挂载点# echo '/dev/vg01/lv01 /app xfs defaults 0 0' >> /etc/fstab
查看新增挂载后的块分区
[root@fsddxtclgj9vm900419 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTvda 252:0 0 50G 0 disk ├─vda1 252:1 0 1G 0 part /boot└─vda2 252:2 0 49G 0 part ├─centos-root 253:0 0 44G 0 lvm / └─centos-swap 253:1 0 5G 0 lvm [SWAP]vdb 252:16 0 974G 0 disk └─vdb1 252:17 0 973.2G 0 part └─vg01-lv01 253:2 0 973.2G 0 lvm /app
可以看到新增磁盘以LVM格式挂载在 /app 下
删除LVM挂载点删除挂载点[root@fsddxtclgj9vm900419 ~]# umount -v /dev/vg01/lv01umount: /app (/dev/mapper/vg01-lv01) unmounted
查看块设备
[root@fsddxtclgj9vm900419 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTvda 252:0 0 50G 0 disk ├─vda1 252:1 0 1G 0 part /boot└─vda2 252:2 0 49G 0 part ├─centos-root 253:0 0 44G 0 lvm / └─centos-swap 253:1 0 5G 0 lvm [SWAP]vdb 252:16 0 974G 0 disk └─vdb1 252:17 0 973.2G 0 part └─vg01-lv01 253:2 0 973.2G 0 lvm
可以看到 /app 挂载点删除了,删除 /app 目录
rm -rf /app
删除LV
参数为 LV Path
[root@fsddxtclgj9vm900419 ~]# lvremove /dev/vg01/lv01Do you really want to remove active logical volume vg01/lv01? [y/n]: y Logical volume "lv01" successfully removed
删除VG
参数为 VG Name
[root@fsddxtclgj9vm900419 ~]# vgremove vg01 Volume group "vg01" successfully removed
扩容VG
参数为 VG Name 和 PV Name
[root@fsddxtclgj9vm900419 ~]# vgextend centos /dev/vdb1 Volume group "centos" successfully extended
此时 PV /dev/vdb1 全部扩展到 VG centos下
查看LV[root@fsddxtclgj9vm900419 ~]# lvdisplay --- Logical volume --- LV Path /dev/centos/swap LV Name swap VG Name centos LV UUID PCx0yU-o9JZ-R92O-lI01-f8EU-DKyM-etst0H LV Write Access read/write LV Creation host, time localhost.localdomain, 2018-12-24 16:46:31 +0800 LV Status available # open 2 LV Size 5.00 GiB Current LE 1280 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1 --- Logical volume --- LV Path /dev/centos/root LV Name root VG Name centos LV UUID Idf9IO-AxkA-tS1C-FTgN-FsLT-d3Zk-7o1W5E LV Write Access read/write LV Creation host, time localhost.localdomain, 2018-12-24 16:46:31 +0800 LV Status available # open 1 LV Size <44.00 GiB Current LE 11263 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
通过查看,系统安装时选择的磁盘格式是LVM并且有两个分区,接下来扩展根分区
扩展LV分区参数为 LV Path
[root@fsddxtclgj9vm900419 ~]# lvextend -l +100%FREE /dev/centos/root Size of logical volume centos/root changed from <44.00 GiB (11263 extents) to <1017.23 GiB (260410 extents). Logical volume centos/root successfully resized
如果发现误操作,需要还原,可以通过以下命令缩小LV分区
lvreduce -L 1017.2G /dev/centos/root
加载扩容到系统
此时查看系统可以看到已经扩容,但没有加载到文件系统
[root@fsddxtclgj9vm900419 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTvda 252:0 0 50G 0 disk ├─vda1 252:1 0 1G 0 part /boot└─vda2 252:2 0 49G 0 part ├─centos-root 253:0 0 1017.2G 0 lvm / └─centos-swap 253:1 0 5G 0 lvm [SWAP]vdb 252:16 0 974G 0 disk └─vdb1 252:17 0 973.2G 0 part └─centos-root 253:0 0 1017.2G 0 lvm /[root@fsddxtclgj9vm900419 ~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/mapper/centos-root 44G 3.7G 41G 9% /devtmpfs 电脑 7.8G 0 7.8G 0% /devtmpfs 7.8G 0 7.8G 0% /dev/shmtmpfs 7.8G 777M 7.0G 10% /runtmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup/dev/vda1 1014M 179M 836M 18% /boottmpfs 1.6G 36K 1.6G 1% /run/user/0tmpfs 1.6G 40K 1.6G 1% /run/user/1001
加载扩容到系统
参数为 LV path,被扩容的挂载点一定是LVM格式
[root@fsddxtclgj9vm900419 ~]# xfs_growfs /dev/centos/rootmeta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=2883328 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0data = bsize=4096 blocks=11533312, imaxpct=25 = 电脑 sunit=0 swidth=0 blksnaming =version 2 bsize=4096 ascii-ci=0 ftype=1log =internal bsize=4096 blocks=5631, version=2 = sectsz=512 sunit=0 blks, lazy-count=1realtime =none extsz=4096 blocks=0, rtextents=0data blocks changed from 11533312 to 266659840
说明:若使用ext4文件格式(centos6),是使用resize2fs命令来生效
查看磁盘
[root@fsddxtclgj9vm900419 ~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/mapper/centos-root 1018G 3.7G 1014G 1% /devtmpfs 7.8G 0 7.8G 0% /devtmpfs 7.8G 0 7.8G 0% /dev/shmtmpfs 7.8G 777M 7.0G 10% /runtmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup/dev/vda1 1014M 179M 836M 18% /boottmpfs 1.6G 3电脑6K 1.6G 1% /run/user/0tmpfs 1.6G 40K 1.6G 1% /run/user/1001
电脑