两个扩容VMware Linux7虚拟机的空间姿势(亲测CentOS7.5和RHEL7.7)
source link: https://www.itpwd.com/426.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
最近测试环境的一台虚拟机CentOS 7.5根目录快满,昨晚做了扩容操作,顺便把过程记录下来。
根目录本来还剩不到3G快满了,删掉一些备份后,根目录还有19G,计划加多100G到根目录上,先关闭这个虚拟机
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 50G 32G 19G 63% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-home 47G 40M 47G 1% /home
/dev/sda1 1014M 170M 845M 17% /boot
tmpfs 379M 8.0K 379M 1% /run/user/42
tmpfs 379M 0 379M 0% /run/user/0
关机之后,如果之前已创建快照需要删除,再进入虚拟机的属性-硬盘
扩容根目录的姿势有两个
姿势1:扩展原有硬盘的容量,点击扩展,由原来的100G改成200G(本文姿势)
姿势2:加入新硬盘,添加个新的100G硬盘
开机,命令看到sda已经由原来的100G变成了200G(差不多就好了,以下输出是214.7G);如果是姿势2会看到有/dev/sdb
[root@localhost ~]# fdisk -l
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000282bb
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 209715199 103808000 8e Linux LVM
…
接下来对扩容在sda上的100G使用fdisk命令进行分区,留意#号后面的文字说明
[root@localhost ~]# 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): p #查看已分区数量(以下输出看到已有sda1和sda2两个分区)
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000282bb
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 209715199 103808000 8e Linux LVM
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): 3 #回车,因为1和2的分区号已用,所以输入3
First sector (209715200-419430399, default 209715200): #默认开始扇区,回车
Using default value 209715200
Last sector, +sectors or +size{K,M,G} (209715200-419430399, default 419430399): #默认结束扇区,回车
Using default value 419430399
Partition 3 of type Linux and of size 100 GiB is set
Command (m for help): t #修改分区类型
Partition number (1-3, default 3): 3 #分区号为3,也就是刚刚扩容的分区
Hex code (type L to list all codes): 8e #修改为LVM,代号就是8e,如果不清楚这里可以输入L可以看全部的代号
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@localhost ~]# partprobe
查看创建好的sda3
[root@localhost ~]# fdisk -l
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000282bb
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 209715199 103808000 8e Linux LVM
/dev/sda3 209715200 419430399 104857600 8e Linux LVM
…
格式化sda3分区
[root@localhost ~]# mkfs.ext3 /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
6553600 inodes, 26214400 blocks
1310720 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
800 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, 11239424, 20480000, 23887872
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]#
把上述创建好的LVM(sda3分区)添加进已有的LVM组,使用lvm命令扩容卷
[root@localhost ~]# lvm
lvm> pvcreate /dev/sda3 #初始化sda3分区
WARNING: ext3 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: y
Wiping ext3 signature on /dev/sda3.
Physical volume "/dev/sda3" successfully created.
lvm> vgdisplay #查看虚拟卷组的名字为centos
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size <99.00 GiB
PE Size 4.00 MiB
Total PE 25343
Alloc PE / Size 25342 / 98.99 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID 7Gxc3r-hIBZ-ImPd-R3Uk-k7sn-grlz-RLUSo5
lvm> vgextend centos /dev/sda3 #将初始化后的sda3分区加入虚拟卷组centos
Volume group "centos" successfully extended
lvm> vgdisplay #查看Free PE号为25600
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 2
Act PV 2
VG Size 198.99 GiB
PE Size 4.00 MiB
Total PE 50942
Alloc PE / Size 25342 / 98.99 GiB
Free PE / Size 25600 / 100.00 GiB
VG UUID 7Gxc3r-hIBZ-ImPd-R3Uk-k7sn-grlz-RLUSo5
lvm> lvextend -l+25600 /dev/mapper/centos-root #扩展已有卷的容量
Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 150.00 GiB (38400 extents).
Logical volume centos/root successfully resized.
lvm> exit
Exiting.
最后扩容文件系统
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3276800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=13107200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=6400, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 13107200 to 39321600
再看下根目录,已扩容成功
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 150G 32G 119G 21% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-home 47G 40M 47G 1% /home
/dev/sda1 1014M 170M 845M 17% /boot
tmpfs 379M 12K 379M 1% /run/user/42
tmpfs 379M 0 379M 0% /run/user/0
[root@localhost ~]#
注意:上文主要是介绍姿势1扩容原有硬盘,如果是姿势2新加硬盘可参考下列亲测RHEL7.7的姿势不再啰嗦,主要留意新加的硬盘号为sdb,新的分区号默认为sdb1即可。
亲测ESXi和Workstations,理论上Fusion也通用。
Fyi RHEL7.7的home目录由150G扩容到250G
[email protected]'s password:
Last login: Fri Apr 10 14:09:07 2020 from 192.168.2.86
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
[root@rhelhost ~]#
[root@rhelhost ~]#
[root@rhelhost ~]#
[root@rhelhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 12G 0 12G 0% /dev
tmpfs 12G 0 12G 0% /dev/shm
tmpfs 12G 9.7M 12G 1% /run
tmpfs 12G 0 12G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 5.7G 45G 12% /
/dev/sda1 1014M 169M 846M 17% /boot
/dev/mapper/rhel-home 146G 42G 104G 29% /home
tmpfs 2.4G 4.0K 2.4G 1% /run/user/0
tmpfs 2.4G 8.0K 2.4G 1% /run/user/42
[root@rhelhost ~]#
[root@rhelhost ~]#
[root@rhelhost ~]# fdisk -l
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x0005d2d3
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 419430399 208665600 8e Linux LVM
Disk /dev/mapper/rhel-root: 53.7 GB, 53687091200 bytes, 104857600 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/rhel-swap: 4160 MB, 4160749568 bytes, 8126464 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/rhel-home: 155.8 GB, 155818393600 bytes, 304332800 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@rhelhost ~]#
[root@rhelhost ~]# fdisk -l
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x0005d2d3
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 419430399 208665600 8e Linux LVM
Disk /dev/mapper/rhel-root: 53.7 GB, 53687091200 bytes, 104857600 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/rhel-swap: 4160 MB, 4160749568 bytes, 8126464 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/rhel-home: 155.8 GB, 155818393600 bytes, 304332800 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@rhelhost ~]# fdisk /dev/sdb
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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xcff24543.
Command (m for help): p
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0xcff24543
Device Boot Start End Blocks Id System
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set
Command (m for help): t
Selected partition 1
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.
Syncing disks.
[root@rhelhost ~]# partprobe
[root@rhelhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 12G 0 12G 0% /dev
tmpfs 12G 0 12G 0% /dev/shm
tmpfs 12G 11M 12G 1% /run
tmpfs 12G 0 12G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 5.7G 45G 12% /
/dev/sda1 1014M 169M 846M 17% /boot
/dev/mapper/rhel-home 146G 42G 104G 29% /home
tmpfs 2.4G 24K 2.4G 1% /run/user/0
tmpfs 2.4G 8.0K 2.4G 1% /run/user/42
[root@rhelhost ~]#
[root@rhelhost ~]#
[root@rhelhost ~]# fdisk -l
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0xcff24543
Device Boot Start End Blocks Id System
/dev/sdb1 2048 209715199 104856576 8e Linux LVM
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x0005d2d3
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 419430399 208665600 8e Linux LVM
Disk /dev/mapper/rhel-root: 53.7 GB, 53687091200 bytes, 104857600 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/rhel-swap: 4160 MB, 4160749568 bytes, 8126464 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/rhel-home: 155.8 GB, 155818393600 bytes, 304332800 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@rhelhost ~]#
[root@rhelhost ~]# mkfs.ext3 /dev/sdb1
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
6553600 inodes, 26214144 blocks
1310707 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
800 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, 11239424, 20480000, 23887872
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information:
done
[root@rhelhost ~]#
[root@rhelhost ~]# lvm
lvm> pvcreate /dev/sdb1
WARNING: ext3 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
Wiping ext3 signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created.
lvm> vgdisplay
--- Volume group ---
VG Name rhel
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size <199.00 GiB
PE Size 4.00 MiB
Total PE 50943
Alloc PE / Size 50942 / 198.99 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID kdRgg5-6PAG-2CGa-NmFL-hs5f-JlmK-pAUtbm
lvm> vgextend rhel /dev/sdb1
Volume group "rhel" successfully extended
lvm> vgdisplay
--- Volume group ---
VG Name rhel
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 2
Act PV 2
VG Size 298.99 GiB
PE Size 4.00 MiB
Total PE 76542
Alloc PE / Size 50942 / 198.99 GiB
Free PE / Size 25600 / 100.00 GiB
VG UUID kdRgg5-6PAG-2CGa-NmFL-hs5f-JlmK-pAUtbm
lvm> lvextend -l+25600 /dev/mapper/rhel-home
Size of logical volume rhel/home changed from <145.12 GiB (37150 extents) to <245.12 GiB (62750 extents).
Logical volume rhel/home successfully resized.
lvm> exit
Exiting.
[root@rhelhost ~]# xfs_growfs /dev/mapper/rhel-home
meta-data=/dev/mapper/rhel-home isize=512 agcount=4, agsize=9510400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=38041600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=18575, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 38041600 to 64256000
[root@rhelhost ~]#
[root@rhelhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 12G 0 12G 0% /dev
tmpfs 12G 0 12G 0% /dev/shm
tmpfs 12G 11M 12G 1% /run
tmpfs 12G 0 12G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 5.7G 45G 12% /
/dev/sda1 1014M 169M 846M 17% /boot
/dev/mapper/rhel-home 246G 42G 204G 17% /home
tmpfs 2.4G 28K 2.4G 1% /run/user/0
tmpfs 2.4G 12K 2.4G 1% /run/user/42
[root@rhelhost ~]#
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK