VM虛擬機擴容

一、配置

CentOs-7
vm15

二、原因

df -hl查看虛擬機內存的時候,空間不足,導致好多操作不行

Linux查看文件大小的簡單指令
在這裏插入圖片描述

三、開始擴容

3.1、擴充虛擬機硬盤大小

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

3.2、擴充虛擬機的分區

# 1、查看當前虛擬機的分區
fdisk /dev/sda
# 2、輸入 p  (查看已有分區)
Command (m for help): p

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

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

# 3、創建分區
Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
# 4、確認創建
Select (default p): p
# 5、輸入要分區的序號
Partition number (3,4, default 3): 4
# 6、回車
First sector (41943040-83886079, default 41943040):
Using default value 41943040
# 7、回車
Last sector, +sectors or +size{K,M,G} (41943040-83886079, default 83886079):
Using default value 83886079
Partition 4 of type Linux and of size 20 GiB is set
# 8、再用 p查看是否新建出分區
Command (m for help): p

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

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda4        41943040    83886079    20971520   83  Linux
# 9、保存結果
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.
# 10、重啓虛擬機後生效

3.3、真正分配資源

[root@node132 /root]#pvcreate /dev/sda4
  Physical volume "/dev/sda4" successfully created.
[root@node132 /root]#df -hl
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G   17G  536M  97% /
devtmpfs                 473M     0  473M   0% /dev
tmpfs                    489M     0  489M   0% /dev/shm
tmpfs                    489M  7.2M  482M   2% /run
tmpfs                    489M     0  489M   0% /sys/fs/cgroup
/dev/sda1               1014M  161M  854M  16% /boot
tmpfs                     98M  8.0K   98M   1% /run/user/42
tmpfs                     98M     0   98M   0% /run/user/0
[root@node132 /root]#vgextend /dev/mapper/centos /dev/sda4
  Volume group "centos" successfully extended
 # 這塊20G報錯了
[root@node132 /root]#vextend -L +20G /dev/mapper/centos-root
bash: vextend: command not found...
[root@node132 /root]#lvextend -L +20G /dev/mapper/centos-root
  Insufficient free space: 5120 extents needed, but only 5119 available
# 所以我又執行的10G
[root@node132 /root]#lvextend -L +10G /dev/mapper/centos-root
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <27.00 GiB (6911 extents).
  Logical volume centos/root successfully resized.

執行這一步的話可能會報錯(Bad magic)

[root@node132 /root]#resize2fs /dev/mapper/centos-root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-root
Couldn't find valid filesystem superblock.

解決方法

# 1、確認文件系統是xfs:
[root@node132 /etc]#cat /etc/fstab
/dev/mapper/centos-root /                       xfs     defaults        0 0

# 2、xfs用以下命令來擴磁盤空間:
[root@node132 /etc]#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 7076864

3.4、查看結果看是否擴充成功(之前是98%現在是62%)

[root@node132 /etc]#df -hl
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   27G   17G   11G  62% /
devtmpfs                 473M     0  473M   0% /dev
tmpfs                    489M     0  489M   0% /dev/shm
tmpfs                    489M  7.2M  482M   2% /run
tmpfs                    489M     0  489M   0% /sys/fs/cgroup
/dev/sda1               1014M  161M  854M  16% /boot
tmpfs                     98M   12K   98M   1% /run/user/42
tmpfs                     98M     0   98M   0% /run/user/0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章