Fork me on GitHub

vmware的centos扩容

完全参考vmware centos扩容

前提:vmware上安装的centos开始只给了30G容量,现在要扩容到40G

  1. 在vmware上扩展分配的磁盘空间

  2. 启动虚拟机,使用root用户执行以下操作

  3. 执行 df -h

[root@localhost docker]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.5M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   27G   17G  9.6G  65% /          # 需要扩容的根分区(/)
/dev/sda1               1014M  150M  865M  15% /boot
tmpfs                    182M     0  182M   0% /run/user/1000

可以看到根分区27G,使用了17G,可使用9.6G,需要扩容的就是这个分区

  1. 执行 fdisk -l
[root@localhost docker]# fdisk -l
# 磁盘(/dev/sda)总空间 /dev/sda 42.9G
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: 0x000b2961

# 总磁盘又分了三个区 /dev/sda1, /dev/sda2, /dev/sda3
# 新添加一个分区 /dev/sda4
   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    62914559    10485760   8e  Linux LVM

# 根分区,现在空间是28.4G,扩展的分区就是这个
Disk /dev/mapper/centos-root: 28.4 GB, 28449964032 bytes, 55566336 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
  1. 执行 fdisk /dev/sda,运行分区程序
[root@localhost docker]# 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): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n  # 创建分区
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): p  # 选择 primary
# 分配扇区,一直按enter选择默认即可
Selected partition 4
First sector (62914560-83886079, default 62914560): 
Using default value 62914560
Last sector, +sectors or +size{K,M,G} (62914560-83886079, default 83886079): 
Using default value 83886079
Partition 4 of type Linux and of size 10 GiB is set

# print 显示分区表
Command (m for help): print

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: 0x000b2961

   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    62914559    10485760   8e  Linux LVM
/dev/sda4        62914560    83886079    10485760   83  Linux      # 新增的/dev/sda4显示出来了,但注意最后一列的 System是 Linux,Id是83

# 修改 system Id
Command (m for help): t
Partition number (1-4, default 4): 
Hex code (type L to list all codes): 8e
# Changed type of partition 'Linux' to 'Linux LVM'
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): print

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: 0x000b2961

   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    62914559    10485760   8e  Linux LVM
/dev/sda4        62914560    83886079    10485760   8e  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 docker]# partprobe   # 通知系统分区表的变化:

参考

如果不是扩容,而是添加新磁盘,执行以下步骤
6. 格式化磁盘

mkfs.ext4 /dev/sdb1
  1. 挂载目录
mount /dev/sdb1 /data
  1. 永久挂载
# 获取磁盘id
$ blkid /dev/sdb1
/dev/sdb1: UUID="ef578891-bed3-4495-bde9-2f19eac2d91d" TYPE="ext4"
vim /etc/fstab

# 打开后,在最后一行加入以下代码:
/dev/sdb1 /data ext4 defaults 0 1
# ef578891-bed3-4495-bde9-2f19eac2d91d /data ext4 defaults 0 1

如果是扩容,而是不是添加新磁盘,执行以下步骤
6. 按顺序执行一下步骤

[root@localhost docker]# pvcreate /dev/sda4  # 将物理硬盘分区初始化为物理卷,以便LVM使用:
  Physical volume "/dev/sda4" successfully created.
[root@localhost docker]# 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               iVGBdN-Ex37-Sr63-RDyp-YOht-nmlx-jHBKoR
   
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               centos
  PV Size               10.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              2559
  Free PE               127
  Allocated PE          2432
  PV UUID               PNayEO-HG4z-ency-1tqP-rSS8-OOJk-fKyswF
  
  # 新增物理卷/dev/sda4, 但没有VG name
  "/dev/sda4" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda4
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               JeWnV9-FqjU-7HPf-d1WH-ULzZ-513C-DBKXZr

# 将初始化过的分区加入到虚拟卷组centos:  
# 如果 /dev/sda4的 system I不是8e,则这一步执行失败 
[root@localhost docker]# vgextend centos /dev/sda4  # 将/dev/sda4添加到卷组centos
  Volume group "centos" successfully extended
[root@localhost docker]# 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               iVGBdN-Ex37-Sr63-RDyp-YOht-nmlx-jHBKoR
   
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               centos
  PV Size               10.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              2559
  Free PE               127
  Allocated PE          2432
  PV UUID               PNayEO-HG4z-ency-1tqP-rSS8-OOJk-fKyswF
  
  # 已添加到卷组 
  --- Physical volume ---
  PV Name               /dev/sda4
  VG Name               centos
  PV Size               10.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              2559
  Free PE               2559
  Allocated PE          0
  PV UUID               JeWnV9-FqjU-7HPf-d1WH-ULzZ-513C-DBKXZr

  1. 扩容
[root@localhost docker]# lvresize -L +10G /dev/mapper/centos-root  # 扩展逻辑卷 lv resize
  Size of logical volume centos/root changed from <26.50 GiB (6783 extents) to <36.50 GiB (9343 extents).
  Logical volume centos/root successfully resized.
[root@localhost docker]# xfs_growfs /dev/mapper/centos-root    # 就理解为格式化操作
meta-data=/dev/mapper/centos-root isize=512    agcount=7, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=6945792, 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 6945792 to 9567232
  1. df -h
[root@localhost docker]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.5M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   37G   17G   20G  47% /                 # 可以看到,已经扩容了
/dev/sda1               1014M  150M  865M  15% /boot
tmpfs                    182M     0  182M   0% /run/user/1000
tmpfs                    182M     0  182M   0% /run/user/0
posted @ 2021-09-04 17:32  Catalinas  阅读(461)  评论(0编辑  收藏  举报