VMware下的Linux扩展磁盘空间

  • 业务场景
  • 业务场景
  • 操作流程
  • 一、VMware 加内存
  • 二 、linux操作
  • 设置开启自动挂载


业务场景

业务场景

本文以 VMware® Workstation 15 Pro -15.5.6 build-16341506+ CentOS Linux release 7.5.1804 (Core)为测试机,如需增加新硬盘请参考 链接: VMware下的Linux扩展磁盘空间(扩展原有硬盘空间。

操作流程

一、VMware 加内存

如下图所示,此时可以看到虚拟机硬盘目前为25G,点击编辑虚拟机设置,进入虚拟机设置界面。

centos7虚拟机扩容后无法启动 虚拟机磁盘扩容 linux_centos7虚拟机扩容后无法启动


分别按下图步骤点击硬盘扩展→扩展内存→扩展确定

centos7虚拟机扩容后无法启动 虚拟机磁盘扩容 linux_centos_02


硬盘扩展成功后,须要在客户机(即Linux虚拟机)上进行分区硬盘和扩展文件系统。

二 、linux操作

启动Linux虚拟机。以root用户登录,由于后面的操作须要root用户的权限。
打开终端窗体,输入df -h,查看文件系统。例如以下图所看到的,能够看到在我的虚拟机中/的大小为17G。可用容量为8.8G,/dev/sda3为之前挂载的硬盘。

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  8.3G  8.8G  49% /
devtmpfs                 1.4G     0  1.4G   0% /dev
tmpfs                    1.4G     0  1.4G   0% /dev/shm
tmpfs                    1.4G   11M  1.4G   1% /run
tmpfs                    1.4G     0  1.4G   0% /sys/fs/cgroup
/dev/sda1               1014M  171M  844M  17% /boot
tmpfs                    283M  8.0K  283M   1% /run/user/42
tmpfs                    283M     0  283M   0% /run/user/0
/dev/sda3                4.8G   20M  4.6G   1% /mount_001
[root@localhost ~]# pwd
/root

运行fdisk –l命令。查看硬盘分区情况,能够发现硬盘空间确实已经扩展了到了30G(我也不知道他为什么显示的是32.2G,暂时还没有去研究,欢迎补充知识点)。

[root@localhost ~]# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000c591b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040    52428799     5242880   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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

然后下面进行格式化增加内存分区的步骤,执行fdisk /dev/sda,按下图步骤操作。

[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
## p :显示磁盘当前的分区表
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000c591b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040    52428799     5242880   8e  Linux LVM

Command (m for help): n
##输入n创建分区
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): p
## 询问我们是要创建主分区还是扩展分区,主分区最多只能创建4个,如果创建了扩展分区那么扩展分区需要占用一个主分区。

这里我们创建分区,输入p
Selected partition 4
First sector (52428800-62914559, default 52428800): 4
Value out of range.
First sector (52428800-62914559, default 52428800): 52428800
Last sector, +sectors or +size{K,M,G} (52428800-62914559, default 62914559): 62914559
Partition 4 of type Linux and of size 5 GiB is set

Command (m for help): t
Partition number (1-4, default 4): 4
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
## 分区选择默认4
Command (m for help): w
The partition table has been altered!
## w 保存
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.

在此执行fdisk -l查看,此时 /dev/sda4 硬盘已经存在

[root@localhost ~]# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000c591b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040    52428799     5242880   8e  Linux LVM
/dev/sda4        52428800    62914559     5242880   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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

执行mkfs -t ext3 /dev/sda4格式化逻辑分区

[root@localhost ~]# mkfs -t ext3 /dev/sda4
mke2fs 1.42.9 (28-Dec-2013)
Could not stat /dev/sda4 --- No such file or directory

The device apparently does not exist; did you specify it correctly?

分区正常,格式化错误。
原因是分区信息尚未到分区表里,
执行命令partprobe 再次执行 mkfs -t ext3 /dev/sda4格式化逻辑分区

[root@localhost ~]# partprobe 
[root@localhost ~]# mkfs -t ext3 /dev/sda4
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
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 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

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@localhost ~]#

创建挂载目录mkdir /mount_002 执行mount /dev/sda4 /mount_002挂载
在此执行df -h检查硬盘

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  8.3G  8.8G  49% /
devtmpfs                 1.4G     0  1.4G   0% /dev
tmpfs                    1.4G     0  1.4G   0% /dev/shm
tmpfs                    1.4G   11M  1.4G   1% /run
tmpfs                    1.4G     0  1.4G   0% /sys/fs/cgroup
/dev/sda1               1014M  171M  844M  17% /boot
tmpfs                    283M   12K  283M   1% /run/user/42
tmpfs                    283M     0  283M   0% /run/user/0
/dev/sda3                4.8G   20M  4.6G   1% /mount_001
/dev/sda4                4.8G   11M  4.6G   1% /mount_002

此时dev/sda4已经挂载导了mount_002

设置开启自动挂载

重启linux,发现挂载的磁盘不在了,检查发现硬盘还在,原因如下
/etc/fstab文件负责配置Linux开机时自动挂载的分区,某些时候当Linux系统下划分了新的分区后,需要将这些分区设置为开机自动挂载,否则,linux是无法使用新建的分区的。磁盘分区都必须挂载到目录树中的某个具体的目录上才能进行读写操作,而fstab正是负责这一配置。需要在根目录上挂载上一个磁盘分区。

执行vi /etc/fstab,将/dev/sda4 /mount_002 ext4 defaults 0 0写入文件。

#
# /etc/fstab
# Created by anaconda on Tue Apr  2 10:41:12 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=6cba1788-aac2-4fd2-b2f5-598a3628bf5e /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sda3 /mount_001 ext4 defaults 0 0
/dev/sda4 /mount_002 ext4 defaults 0 0

此时再重启,硬盘会自动挂载。