Linux虚拟机扩展磁盘

1.虚拟机关机,Vmware中扩展磁盘

image-20210726093124228

2.虚拟机开机,查看磁盘大小

[root@hadoop6 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 7.8G     0  7.8G    0% /dev
tmpfs                    7.8G     0  7.8G    0% /dev/shm
tmpfs                    7.8G  891M  6.9G   12% /run
tmpfs                    7.8G     0  7.8G    0% /sys/fs/cgroup
/dev/mapper/centos-root   17G   17G   20K  100% /
/dev/sda1               1014M  150M  865M   15% /boot
cm_processes             7.8G     0  7.8G    0% /run/cloudera-scm-agent/process
tmpfs                    1.6G     0  1.6G    0% /run/user/0
[root@hadoop6 ~]# fdisk -l

磁盘 /dev/sda:536.9 GB, 536870912000 字节,1048576000 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000ee5d4

   设备 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 字节

这里的扩展只是增加了操作系统的磁盘空间,并没有与系统内部的文件目录挂载,所以,磁盘占有量还是不会变化,下一步就是要把扩展的容量挂载到文件目录上去。

3.对新硬盘分区并修改系统类型

[root@hadoop6 ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。


命令(输入 m 获取帮助):m
命令操作
   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)

依次输入:

  • n --新建分区
  • p --默认创建主分区
  • 3 --默认分区号
  • 回车 --默认大小不浪费空间
  • 回车 --默认大小不浪费空间
命令(输入 m 获取帮助):n --新建分区
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p --默认创建主分区
分区号 (3,4,默认 3):3 --默认
起始 扇区 (41943040-1048575999,默认为 41943040):    --回车
将使用默认值 41943040
Last 扇区, +扇区 or +size{K,M,G} (41943040-1048575999,默认为 1048575999):    --回车
将使用默认值 1048575999
分区 3 已设置为 Linux 类型,大小设为 480 GiB

输入:w --保持修改

命令(输入 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)
正在同步磁盘。

将system 类型改成Linux LVM

[root@hadoop6 ~]# fdisk -l 

磁盘 /dev/sda:536.9 GB, 536870912000 字节,1048576000 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000ee5d4

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040  1048575999   503316480   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 字节

我们的新建分区/dev/sda3,的Id为83不是LVM的。所以,接下来使用fdisk将其改成Id为8e的LVM。

[root@hadoop6 ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。


命令(输入 m 获取帮助):m  
命令操作
   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)
命令(输入 m 获取帮助):t  --修改分区系统Id
分区号 (1-3,默认 3):3  --指定分区号
Hex 代码(输入 L 列出所有代码):8e  --指定要改成的id号,8e代表LVM
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 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)
正在同步磁盘。

fdisk -l 再查看一下是否改成8e 和Linux LVM

[root@hadoop6 ~]# fdisk -l

磁盘 /dev/sda:536.9 GB, 536870912000 字节,1048576000 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000ee5d4

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040  1048575999   503316480   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@hadoop6 ~]# reboot

4.扩充新分区

格式化新添加的分区,改为ext4类型

[root@hadoop6 ~]# mkfs.ext4 /dev/sda3
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
31457280 inodes, 125829120 blocks
6291456 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2273312768
3840 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, 20480000, 23887872, 71663616, 78675968, 
        102400000

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成     

添加新LVM到已有的LVM组,实现扩容

创建sda3:

[root@hadoop6 ~]# pvcreate /dev/sda3
WARNING: ext4 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sda3.
  Physical volume "/dev/sda3" successfully created.

查看创建结果:

[root@hadoop6 ~]# 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               2UuDxd-kchc-YxC2-VJYX-yK9r-qGMG-7H0fvi
   
  "/dev/sda3" is a new physical volume of "480.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name               
  PV Size               480.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               i60nWa-40d3-jFCg-F0Su-24FO-QQCh-Ky0QA7

**这里操作要根据上面中VG Name来定义用vgextend谁,我这里是centos那么我就用vgextend centos /dev/sda3 **

[root@hadoop6 ~]# vgextend centos /dev/sda3 
  Volume group "centos" successfully extended
[root@hadoop6 ~]# 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               2UuDxd-kchc-YxC2-VJYX-yK9r-qGMG-7H0fvi
   
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               centos
  PV Size               480.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              122879
  Free PE               122879
  Allocated PE          0
  PV UUID               i60nWa-40d3-jFCg-F0Su-24FO-QQCh-Ky0QA7

执行命令:lvextend -L +480G /dev/mapper/centos-root 进行扩容,容量大小,自己根据情况定义

[root@hadoop6 ~]# lvextend -L +480G /dev/mapper/centos-root
  Insufficient free space: 122880 extents needed, but only 122879 available

直接按最大容量扩充会报错

执行 pvdisplay 可以看到 /dev/sda3 可用的 PE 总数量是 122879,而每个 PE 大小是 4.00MiB,所以这个卷组实际的可用空间不是480G,122879*4/1024=479.99609375 而是479.99G

[root@hadoop6 ~]# lvextend -L +479.99G /dev/mapper/centos-root
  Rounding size to boundary between physical extents: 479.99 GiB.
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <496.99 GiB (127229 extents).
  Logical volume centos/root successfully resized.

执行命令:lvs 进行查看是否成功

[root@hadoop6 ~]# lvs
  LV   VG     Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- <496.99g                                                    
  swap centos -wi-ao----    2.00g 

此时df -h查看容量未变化

[root@hadoop6 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 7.8G     0  7.8G    0% /dev
tmpfs                    7.8G     0  7.8G    0% /dev/shm
tmpfs                    7.8G   12M  7.8G    1% /run
tmpfs                    7.8G     0  7.8G    0% /sys/fs/cgroup
/dev/mapper/centos-root   17G   17G  2.0M  100% /
/dev/sda1               1014M  150M  865M   15% /boot
cm_processes             7.8G     0  7.8G    0% /run/cloudera-scm-agent/process
tmpfs                    1.6G     0  1.6G    0% /run/user/0

查看磁盘空间采用的文件系统

[root@hadoop6 ~]# df -T
文件系统                类型        1K-块     已用    可用 已用% 挂载点
devtmpfs                devtmpfs  8111764        0 8111764    0% /dev
tmpfs                   tmpfs     8123804        0 8123804    0% /dev/shm
tmpfs                   tmpfs     8123804    11932 8111872    1% /run
tmpfs                   tmpfs     8123804        0 8123804    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs      17811456 17809512    1944  100% /
/dev/sda1               xfs       1038336   153464  884872   15% /boot
cm_processes            tmpfs     8123804        0 8123804    0% /run/cloudera-scm-agent/process
tmpfs                   tmpfs     1624764        0 1624764    0% /run/user/0

需要修改ext4文件系统为xfs文件系统

执行命令:xfs_growfs /dev/mapper/centos-root

[root@hadoop6 ~]# 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 130282496

再次查看

[root@hadoop6 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 7.8G     0  7.8G    0% /dev
tmpfs                    7.8G     0  7.8G    0% /dev/shm
tmpfs                    7.8G   12M  7.8G    1% /run
tmpfs                    7.8G     0  7.8G    0% /sys/fs/cgroup
/dev/mapper/centos-root  497G   17G  480G    4% /
/dev/sda1               1014M  150M  865M   15% /boot
cm_processes             7.8G     0  7.8G    0% /run/cloudera-scm-agent/process
tmpfs                    1.6G     0  1.6G    0% /run/user/0

到此,容量已经扩大,磁盘扩展成功。

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/mytangyh/p/15061101.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程

3060 9.8元 98元 1折
4550 9.8元