VMware下CentOS7扩容/root/目录磁盘容量
具体步骤
我们给CentOS7虚拟机的/root/扩展20G空间
//---------------------------------------------------------------//
1、关闭虚机,然后在vmware给CentOS7扩展20G,然后启动CentOS7查看分区信息。
查看磁盘信息,可以看到系统下只有一块硬盘sda;
[root@lwh ~]# df -h 查看磁盘使用情况
Filesystem Size Used Avail Use% Mounted on
devtmpfs 470M 0 470M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 8.1M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 20G 20G 722M 97% / !!!!!!!!!!!!!!
/dev/sda1 1014M 178M 837M 18% /boot
vmhgfs-fuse 100G 41G 60G 41% /mnt/hgfs
tmpfs 98M 0 98M 0% /run/user/1000
tmpfs 98M 0 98M 0% /run/user/0
[root@lwh ~]#
[root@lwh ~]# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000be59c
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 48252927 23076864 8e Linux LVM
...
//---------------------------------------------------------------//
2、使用fdisk分区工具给sda磁盘分区
输入:fdisk /dev/sda
输入命令:n #添加新分区。
输入命令:p #创建主分区。
输入:回车 #选择默认大小,这样不浪费空间
输入:回车 #选择默认的start cylinder。
输入:w #保持修改
输入:reboot #重启linux,必须reboot
[root@lwh ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3):
First sector (48252928-125829119, default 48252928):
Using default value 48252928
Last sector, +sectors or +size{K,M,G} (48252928-125829119, default 125829119):
Using default value 125829119
Partition 3 of type Linux and of size 37 GiB is set
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@lwh ~]# reboot
//---------------------------------------------------------------//
3、fdisk -l,查看分区信息,查看是否有刚才挂载的盘如/dev/sda3
sda3即为新增加的分区,
看sda3最后一列,sda3的System为Linux(Id为83),需要修改为Linux LVM(Id为8e)
[root@lwh ~]# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000be59c
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 48252927 23076864 8e Linux LVM
/dev/sda3 48252928 125829119 38788096 83 Linux !!!!!!!
Disk /dev/mapper/centos-root: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@lwh ~]#
//---------------------------------------------------------------//
4、修改sda3为LVM卷文件系统
fdisk /dev/sda
t
3
8e
w
[root@lwh ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
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@lwh ~]#
//---------------------------------------------------------------//
5、再次查看分区信息,sda3已经改为Linux LVM了;
[root@lwh ~]# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000be59c
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 48252927 23076864 8e Linux LVM
/dev/sda3 48252928 125829119 38788096 8e Linux LVM
Disk /dev/mapper/centos-root: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
//---------------------------------------------------------------//
6、重启系统;
[root@lwh ~]# reboot
//---------------------------------------------------------------//
7、将sda3分区创建为文件系统
[root@lwh ~]# mkfs -t ext4 /dev/sda3
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2424832 inodes, 9697024 blocks
484851 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2157969408
296 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
//---------------------------------------------------------------//
8、查看当前的物理卷
[root@lwh ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <22.01 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5633
Free PE 1
Allocated PE 5632
PV UUID mWYEqE-VxM9-RsPN-jAiF-SeOh-9X8R-u64ate
[root@lwh ~]#
//---------------------------------------------------------------//
9、将物理硬盘分区初始化为物理卷,以便被LVM使用;(创建物理卷)
只有System Id修改为8e之后,才能使用pvcreat指令将分区初始化为物理卷;
[root@lwh ~]# pvcreate /dev/sda3
WARNING: ext4 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/sda3.
Physical volume "/dev/sda3" successfully created.
[root@lwh ~]#
//---------------------------------------------------------------///10再次查10、看当前物理卷
[root@lwh ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <22.01 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5633
Free PE 1
Allocated PE 5632
PV UUID mWYEqE-VxM9-RsPN-jAiF-SeOh-9X8R-u64ate
"/dev/sda3" is a new physical volume of "36.99 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 36.99 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID D4laZv-xvEX-vXxq-0yQr-iQ0n-N29U-nYJDXV
[root@lwh ~]#
//---------------------------------------------------------------//
11、动态扩展VG
通过vgdisplay查看当前需要扩充的lvm组名,本机为centos;
[root@lwh ~]# vgdisplay
--- 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 22.00 GiB
PE Size 4.00 MiB
Total PE 5633
Alloc PE / Size 5632 / 22.00 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID j7osYQ-3Stt-rrQ2-YZlL-0gUL-xXts-4Z4q7C
[root@lwh ~]#
vgextend通过向卷组中添加物理卷来增加卷组的容量;(使用新增物理卷扩展vg)
[root@lwh ~]# vgextend centos /dev/sda3
Volume group "centos" successfully extended
[root@lwh ~]#
再次查看VG信息,发现倒数第2行的Free PE下36G(20G(新扩展)+16G(原本剩余))的可扩展容量;
[root@lwh ~]# vgdisplay
--- Volume group ---
VG Name centos
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 58.99 GiB
PE Size 4.00 MiB
Total PE 15102
Alloc PE / Size 5632 / 22.00 GiB
Free PE / Size 9470 / 36.99 GiB
VG UUID j7osYQ-3Stt-rrQ2-YZlL-0gUL-xXts-4Z4q7C
[root@lwh ~]#
//---------------------------------------------------------------//
12、LV扩展
通过lvdisplay查看根目录的LV Path为/dev/centos/root;
[root@lwh ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID Q031g7-xUS6-Ycfs-CEat-zGSv-Wk7p-n0cDQ7
LV Write Access read/write
LV Creation host, time localhost, 2019-11-27 16:01:59 +0800
LV Status available
# open 2
LV Size 2.00 GiB
Current LE 512
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 cTa5Lq-NgCU-jKnp-3MRY-NP2o-2G1E-l8Wuh3
LV Write Access read/write
LV Creation host, time localhost, 2019-11-27 16:01:59 +0800
LV Status available
# open 1
LV Size 20.00 GiB
Current LE 5120
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@lwh ~]#
LV扩展
扩展空间要小于VG 的Free PE的大小,即小于20GB,这里使用19.8G;
[root@lwh ~]# lvextend -L +19.8G /dev/centos/root /dev/sda3
Rounding size to boundary between physical extents: 19.80 GiB.
Size of logical volume centos/root changed from 20.00 GiB (5120 extents) to 39.80 GiB (10189 extents).
Logical volume centos/root successfully resized.
[root@lwh ~]#
//---------------------------------------------------------------//
13、查看分区文件系统类型
CentOS7默认使用xfs,CentOS6默认使用ext4;
[root@lwh ~]#df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
devtmpfs devtmpfs 481092 0 481092 0% /dev
tmpfs tmpfs 497880 0 497880 0% /dev/shm
tmpfs tmpfs 497880 8216 489664 2% /run
tmpfs tmpfs 497880 0 497880 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 20961280 20219952 741328 97% /
/dev/sda1 xfs 1038336 182196 856140 18% /boot
vmhgfs-fuse fuse.vmhgfs-fuse 104296444 42109716 62186728 41% /mnt/hgfs
tmpfs tmpfs 99576 0 99576 0% /run/user/1000
tmpfs tmpfs 99576 0 99576 0% /run/user/0
[root@lwh ~]#
//---------------------------------------------------------------//
14、同步文件系统
文件系统类型是xfs,只能使用xfs_growfs命令同步文件系统,不能使用resize2fs、e2fsck、dumpe2fs;
resize2fs命令,用于扩大或者缩小未挂载的ext2,ext3或者是ext4文件系统;
[root@lwh ~]# xfs_growfs /dev/centos/root #(扩展centor-root容量)
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=5242880, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 5242880 to 10433536
[root@lwh ~]#
//---------------------------------------------------------------//
15、查看磁盘信息,发现根目录已经扩展了20G,由20G变为40G;
[root@lwh ~]# df -lh
Filesystem Size Used Avail Use% Mounted on
devtmpfs 470M 0 470M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 8.1M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 40G 20G 21G 49% /
/dev/sda1 1014M 178M 837M 18% /boot
vmhgfs-fuse 100G 41G 60G 41% /mnt/hgfs
tmpfs 98M 0 98M 0% /run/user/1000
tmpfs 98M 0 98M 0% /run/user/0
[root@lwh ~]#
[root@lwh ~]#
[root@lwh ~]# df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
devtmpfs devtmpfs 481092 0 481092 0% /dev
tmpfs tmpfs 497880 0 497880 0% /dev/shm
tmpfs tmpfs 497880 8216 489664 2% /run
tmpfs tmpfs 497880 0 497880 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 41723904 20220236 21503668 49% /
/dev/sda1 xfs 1038336 182196 856140 18% /boot
vmhgfs-fuse fuse.vmhgfs-fuse 104296444 42111952 62184492 41% /mnt/hgfs
tmpfs tmpfs 99576 0 99576 0% /run/user/1000
tmpfs tmpfs 99576 0 99576 0% /run/user/0
//---------------------------------------------------------------//
12、到15、再走一遍
LV扩展
扩展空间要小于VG 的Free PE的大小,即小于(36.99G - 20GB),这里再次使用了16G;
[root@lwh ~]# lvextend -L +16G /dev/centos/root /dev/sda3
Size of logical volume centos/root changed from 39.80 GiB (10189 extents) to 55.80 GiB (14285 extents).
Logical volume centos/root successfully resized.
同步文件系统
[root@lwh ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512 agcount=8, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=10433536, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 10433536 to 14627840
查看磁盘信息,发现根目录已经扩展了16G,由40G变为56G;
[root@lwh ~]# df -lh
Filesystem Size Used Avail Use% Mounted on
devtmpfs 470M 0 470M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 8.1M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 56G 20G 37G 35% /
/dev/sda1 1014M 178M 837M 18% /boot
vmhgfs-fuse 100G 41G 60G 41% /mnt/hgfs
tmpfs 98M 0 98M 0% /run/user/1000
tmpfs 98M 0 98M 0% /run/user/0
[root@lwh ~]#
[root@lwh ~]# vgdisplay
--- Volume group ---
VG Name centos
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 58.99 GiB
PE Size 4.00 MiB
Total PE 15102
Alloc PE / Size 14797 / 57.80 GiB
Free PE / Size 305 / 1.19 GiB
VG UUID j7osYQ-3Stt-rrQ2-YZlL-0gUL-xXts-4Z4q7C
//---------------------------------------------------------------//
参考博客
https://blog.csdn.net/hellboy0621/article/details/84345672