VMware虚拟机中Linux系统磁盘空间扩容实战

简介: VMware虚拟机中Linux系统磁盘空间扩容实战

 平常在VMware上创建Linux系统虚拟机的时候,往往当时不会给太多的磁盘空间,在后期的使用过程中经常会遇到磁盘空间不足的情况,需要对Linux系统扩展磁盘空间。


扩展磁盘步骤:


1.在系统关机的情况下操作磁盘扩展,并且这个磁盘上没有快照文件,如有需要删除才可以。

1.png2.png

在VMware虚拟机管理界面操作成功后,还没有结束,还需要到操作系统内部进行操作。


2.登录该Linux系统,执行fdisk命令查看磁盘扩展情况

[root@test2 ~]# fdisk -l
磁盘 /dev/sda:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0003efbe
   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
磁盘 /dev/mapper/centos-root:18.2 GB, 18249416704 字节,35643392 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
[root@test2 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   50G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sr0              11:0    1  906M  0 rom

3.从主磁盘空间中创建新的分区

[root@test2 ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):n   //输入
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p   //输入
分区号 (3,4,默认 3):3    //输入
起始 扇区 (41943040-104857599,默认为 41943040):   //回车
将使用默认值 41943040
Last 扇区, +扇区 or +size{K,M,G} (41943040-104857599,默认为 104857599):   //回车
将使用默认值 104857599
分区 3 已设置为 Linux 类型,大小设为 30 GiB
命令(输入 m 获取帮助):p   //输入
磁盘 /dev/sda:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0003efbe
   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040   104857599    31457280   83  Linux
命令(输入 m 获取帮助):w   //输入
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
[root@test2 ~]# fdisk -l     //查看分区创建情况
磁盘 /dev/sda:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0003efbe
   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040   104857599    31457280   83  Linux
磁盘 /dev/mapper/centos-root:18.2 GB, 18249416704 字节,35643392 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

4.重启Linux系统并格式化分区文件系统

[root@test2 ~]# reboot
[root@test2 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   50G  0 disk
├─sda1            8:1    0    1G  0 part /boot
├─sda2            8:2    0   19G  0 part
│ ├─centos-root 253:0    0   17G  0 lvm  /
│ └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
└─sda3            8:3    0   30G  0 part
sr0              11:0    1  906M  0 rom
[root@test2 ~]# mkfs.ext3 /dev/sda3         //将/dev/sda3文件系统格式化为ext3
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1966080 inodes, 7864320 blocks
393216 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=4294967296
240 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
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

5.将分区转换为物理卷,此步骤可省略:当将分区直接添加到卷组时,LVM会自动将分区转换为物理卷组。

[root@test2 ~]# lvm
lvm> pvcreate /dev/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> pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos              //***卷组名称***
  PV Size               <19.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4863
  Free PE               0
  Allocated PE          4863
  PV UUID               xLgDul-dNOf-qOhP-8s0j-Xg27-OUUm-d3BY3N
  "/dev/sda3" is a new physical volume of "30.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name
  PV Size               30.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               beWBdd-QioN-Hgwc-Jtgl-2BOV-AZCM-6K6OPN
lvm> vgextend centos /dev/sda3      //将物理卷添加到现有的卷组
  Volume group "centos" successfully extended
[root@test2 ~]# pvscan      //查看所有物理卷及所属分组
  PV /dev/sda2   VG centos          lvm2 [<19.00 GiB / 0    free]
  PV /dev/sda3   VG centos          lvm2 [<30.00 GiB / <30.00 GiB free]
  Total: 2 [48.99 GiB] / in use: 2 [48.99 GiB] / in no VG: 0 [0   ]
[root@test2 ~]# lvscan      //查看所有逻辑卷
  ACTIVE            '/dev/centos/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/centos/root' [<17.00 GiB] inherit

6.查看分区情况扩展逻辑卷大小

[root@test2 ~]# fdisk -l
磁盘 /dev/sda:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0003efbe
   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040   104857599    31457280   83  Linux
磁盘 /dev/mapper/centos-root:18.2 GB, 18249416704 字节,35643392 个扇区     //可以看到我们要扩展的磁盘名称
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
[root@test2 ~]# lvm     //进入LVM管理器
lvm> lvextend -L +29.99G /dev/mapper/centos-root        //扩展逻辑卷大小(在fdisk -l中查看要扩展过去的磁盘)
  Rounding size to boundary between physical extents: 29.99 GiB.
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <46.99 GiB (12029 extents).
  Logical volume centos/root successfully resized.
[root@test2 ~]# lvscan
  ACTIVE            '/dev/centos/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/centos/root' [<46.99 GiB] inherit
[root@test2 ~]# pvscan
  PV /dev/sda2   VG centos          lvm2 [<19.00 GiB / 0    free]
  PV /dev/sda3   VG centos          lvm2 [<30.00 GiB / 4.00 MiB free]
  Total: 2 [48.99 GiB] / in use: 2 [48.99 GiB] / in no VG: 0 [0   ]

注意:上面虽然显示物理卷/dev/sdb3有30G空闲,但是这里不能全部使用,29.99G是可以的。


7.激活即重新读取磁盘信息

[root@test2 ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4455424, 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 4455424 to 12317696
[root@test2 ~]# df -h           //查看激活状态
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   47G   17G   30G   37% /
devtmpfs                 592M     0  592M    0% /dev
tmpfs                    604M     0  604M    0% /dev/shm
tmpfs                    604M  9.2M  595M    2% /run
tmpfs                    604M     0  604M    0% /sys/fs/cgroup
/dev/sda1               1014M  129M  886M   13% /boot
tmpfs                    121M     0  121M    0% /run/user/0

 可以看到现在我们的磁盘已经从最开始的19G扩展到了47G。


相关文章
|
25天前
|
Ubuntu Windows
【Ubuntu/Arm】Ubuntu 系统如何链接有线网络(非虚拟机)?
【Ubuntu/Arm】Ubuntu 系统如何链接有线网络(非虚拟机)?
|
25天前
|
Ubuntu Linux 网络安全
【ubuntu】MobaXtem远程登录ubuntu系统(或虚拟机)
【ubuntu】MobaXtem远程登录ubuntu系统(或虚拟机)
|
1月前
|
算法 虚拟化 C++
VMware虚拟机无法自适应和拖拽复制粘贴和共享目录问题
VMware虚拟机无法自适应和拖拽复制粘贴和共享目录问题
96 0
|
4天前
|
程序员 Linux KVM
【qemu虚拟化】将img镜像文件转换为VMware虚拟机
QEMU是一个开源的硬件虚拟化器,能在多种平台如x86、ARM、PowerPC上运行,支持虚拟化不同体系结构的操作系统。它具有硬件仿真、虚拟化支持、磁盘和网络仿真、快照回滚及可扩展性等特点。要使用QEMU,首先从[官网](https://www.qemu.org/download/)下载,然后通过命令行运行进行转换。转换后的vMDK文件可在VMware中导入,创建新的虚拟机进行使用。
44 1
【qemu虚拟化】将img镜像文件转换为VMware虚拟机
|
17天前
|
Linux 虚拟化
linux 虚拟机扩容--直接扩容到根目录
linux 虚拟机扩容--直接扩容到根目录
27 1
|
26天前
【嵌入式】虚拟机未能将管道连接到虚拟机: 系统找不到指定的文件
【嵌入式】虚拟机未能将管道连接到虚拟机: 系统找不到指定的文件
|
1月前
|
关系型数据库 MySQL 数据库
虚拟机Linux-Centos系统网络配置常用命令+Docker 的常用命令
虚拟机Linux-Centos系统网络配置常用命令+Docker 的常用命令
46 0
|
1月前
|
Linux 虚拟化 数据安全/隐私保护
使用VMware安装linux虚拟机
使用VMware安装linux虚拟机
使用VMware安装linux虚拟机