虚拟机磁盘空间不够,需要从外部添加新的磁盘时,操作如下:
1、在VMware是虚拟机设置中,添加硬盘。
重启虚拟机。查看磁盘分区情况,看到新添加的磁盘/dev/sdb。
使用fdisk命令对/dev/sdb进行分区。
[root@localhost ~]# fdisk -l
Disk /dev/sdb: 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
Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000b0983
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 10487807 4194304 82 Linux swap / Solaris
/dev/sda3 10487808 83886079 36699136 83 Linux
[root@localhost ~]# 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 0xa9b5f3c7.
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): 1
First sector (2048-4194303, default 2048):
Using default value 2048
Last sector, +sectors or +size{
K,M,G} (2048-4194303, default 4194303):
Using default value 4194303
Partition 1 of type Linux and of size 2 GiB is set
Command (m for help): p
Disk /dev/sdb: 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
Disk label type: dos
Disk identifier: 0xa9b5f3c7
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4194303 2096128 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
分区完成之后,要进行格式化的操作,格式化命令为mkfs。
[root@localhost ~]# mkfs.xfs /dev/sdb
sdb sdb1
[root@localhost ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131008 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=524032, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
在linux系统中,一切皆是文件,磁盘分区也不例外,如果要使用某个磁盘分区,能够在分区上存储和读取数据,就需要挂载该磁盘分区。磁盘分区相当于是一个文件,我们无法在一个文件内存储文件或文件夹,但是我们可以在文件夹内进行文件或文件夹的存储操作。使用挂载的方式,就是将磁盘分区文件和一个文件夹关联,通过该文件夹在磁盘上存储文件,这个文件夹就是挂载点。
磁盘挂载命令是mount。但mount命令实现的是一次性磁盘挂载,重启系统后,需要再次执行挂载命令。挂载点必须是一个已经存在的文件夹。
[root@localhost ~]# mkdir /mnt/test
[root@localhost ~]# mount /dev/sdb1 /mnt/test/
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 xfs 35G 9.6G 26G 28% /
devtmpfs devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs tmpfs 3.9G 144K 3.9G 1% /dev/shm
tmpfs tmpfs 3.9G 9.0M 3.9G 1% /run
tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sr0 iso9660 3.6G 3.6G 0 100% /var/ftp/dvd
/dev/sda1 xfs 1014M 170M 845M 17% /boot
tmpfs tmpfs 781M 8.0K 781M 1% /run/user/0
/dev/sdb1 xfs 2.0G 33M 2.0G 2% /mnt/test
如果需要新添加的磁盘每次开机都自动挂载,需要编辑/etc/fstab文件,/etc/fstab文件下保存开机自动挂载的设备。
[root@localhost ~]# vi /etc/fstab
[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Feb 24 15:34:07 2021
#
# 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
#
UUID=e260d75c-d83a-45fe-b084-a5016087f284 / xfs defaults 0 0
UUID=bbce0f47-6db0-4d4a-a37f-025ed12a45ca /boot xfs defaults 0 0
UUID=c299518f-7ce3-430b-872d-7d30e5ec3b98 swap swap defaults 0 0
/dev/sr0 /var/ftp/dvd iso9660 defaults 0 0
/dev/sdb1 /mnt/test xfs defaults 0 0
[root@localhost ~]# mount -a
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 xfs 35G 9.6G 26G 28% /
devtmpfs devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs tmpfs 3.9G 144K 3.9G 1% /dev/shm
tmpfs tmpfs 3.9G 9.0M 3.9G 1% /run
tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sr0 iso9660 3.6G 3.6G 0 100% /var/ftp/dvd
/dev/sda1 xfs 1014M 170M 845M 17% /boot
tmpfs tmpfs 781M 8.0K 781M 1% /run/user/0
/dev/sdb1 xfs 2.0G 33M 2.0G 2% /mnt/test
磁盘添加完成,虚拟机可以正常使用/mnt/test新增的区域了。