VMware虚拟机扩展Linux根目录磁盘空间(CentOS)

1、Centos 关机,选择编辑虚拟机设置,硬盘,在实用工具那里选择“扩展”
在这里插入图片描述
在这里插入图片描述
点击扩展后,VMware会提示磁盘已成功扩展。您必须从客户机操作系统内部对磁盘重新进行分区和扩展文件系统。也就是说,这里扩展的磁盘空间,在操作系统里面还不可用,还没生效,需要在操作系统里面进行设置后才能使用。
在这里插入图片描述


2、启动客户机操作系统Centos,查看磁盘情况
输入指令

[root@dw ~]# fdisk -l

Disk /dev/sda: 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: 0x000b0b8a

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    83886079    40893440   8e  Linux LVM

Disk /dev/mapper/centos-root: 37.7 GB, 37706792960 bytes, 73646080 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: 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

其中,可以看到现在的分区情况如下

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    83886079    40893440   8e  Linux LVM

3、创建分区 sda3
输入指令

[root@dw ~]# 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    # 输入 n 表示创建新分区
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 3    # 输入 3 表示sda3,因为前面已经有两个了
First sector (83886080-209715199, default 83886080):    # 回车,按默认值
Using default value 83886080
Last sector, +sectors or +size{
    
    K,M,G} (83886080-209715199, default 209715199):    # 回车,按默认值
Using default value 209715199
Partition 3 of type Linux and of size 60 GiB is set

Command (m for help): w    # 输入 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@dw ~]# fdisk -l

Disk /dev/sda: 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: 0x000b0b8a

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    83886079    40893440   8e  Linux LVM
/dev/sda3        83886080   209715199    62914560   83  Linux

Disk /dev/mapper/centos-root: 37.7 GB, 37706792960 bytes, 73646080 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: 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

其中,分区已变为3个了,但这时还没挂载,还不能用

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    83886079    40893440   8e  Linux LVM
/dev/sda3        83886080   209715199    62914560   83  Linux

4、重启操作系统

[root@dw ~]# reboot

5、格式化新分区为ext3

输入指令

[root@dw ~]# mkfs -t 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
3932160 inodes, 15728640 blocks
786432 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
480 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

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

6、将物理硬盘分区初始化为物理卷,以便被LVM使用,输入指令

[root@dw ~]# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- <35.12g
  swap centos -wi-ao----  <3.88g
[root@dw ~]# 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.

7、向卷组中添加物理卷来增加卷组的容量

查看lvm组名,输入指令 df -h

[root@dw ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
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-root   36G   18G   18G  49% /
/dev/sda1               1014M  149M  866M  15% /boot
tmpfs                    378M     0  378M   0% /run/user/0

其中,centos-root 就是 根 所在的组名

输入指令,增加卷组的容量

[root@dw ~]# vgextend centos /dev/sda3
  Volume group "centos" successfully extended

8、查看可扩展的空间大小

输入指令,显示LNM卷组的元数据信息

[root@dw ~]# 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               98.99 GiB
  PE Size               4.00 MiB
  Total PE              25342
  Alloc PE / Size       9982 / 38.99 GiB
  Free  PE / Size       15360 / 60.00 GiB
  VG UUID               zJfuos-4OrR-b2OS-g589-oj2P-Jhqe-laP5QC

其中 Free PE / Size 就是可供分配的自由空间,最多有 60.00G,在扩展时输入小于该值


9、扩充磁盘空间

[root@dw ~]# lvextend -L+58G /dev/mapper/centos-root /dev/sda3

提示扩充成功

  Size of logical volume centos/root changed from <35.12 GiB (8990 extents) to <93.12 GiB (23838 extents).
  Logical volume centos/root successfully resized.

10、使用e2fsck指令检查文件系统错误

[root@dw ~]# e2fsck -a /dev/mapper/centos-root
/dev/mapper/centos-root is mounted.
e2fsck: Cannot continue, aborting.

11、xfs格式的文件扩充需要使用 xfs_growfs指令来增大或者收缩未加载的“ext2/ext3”文件系统的大小

[root@dw ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        94G   18G   76G  19% /
/dev/sda1               xfs      1014M  149M  866M  15% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
[root@dw ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=2301440 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=9205760, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=4495, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 9205760 to 24410112

12、查看扩展空间后的结果

[root@dw ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
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-root   94G   18G   76G  19% /
/dev/sda1               1014M  149M  866M  15% /boot
tmpfs                    378M     0  378M   0% /run/user/0

可以看出,在根目录下磁盘空间已经扩容了,大功告成!

猜你喜欢

转载自blog.csdn.net/weixin_48482704/article/details/115075411