vagrant扩容教程

Linux系统
553
0
0
2022-07-06

vagrant扩容教程

前期操作

1.找到Vagrantfile 文件所在的目录,本机所在目录为

dl@dldeMacBook-Pro Pro2 % pwd
/Users/myself/vagrant/php/machine/Pro2
dl@dldeMacBook-Pro Pro2 % ls
Vagrantfile    box       share2    virtual2

2.启动项目

dl@dldeMacBook-Pro Pro2 % vagrant up
Bringing machine 'default' up with 'virtualbox' provider...

3.启动成功后会生成对应的vbox目录

dl@dldeMacBook-Pro / % cd /Users/dl/VirtualBox\ VMs/Pro2_default_1618586539591_12542
dl@dldeMacBook-Pro Pro2_default_1618586539591_12542 % pwd
/Users/dl/VirtualBox VMs/Pro2_default_1618586539591_12542
dl@dldeMacBook-Pro Pro2_default_1618586539591_12542 % ls
Logs                  Pro2_default_1618586539591_12542.vbox-prev box-disk1.vmdk
Pro2_default_1618586539591_12542.vbox
dl@dldeMacBook-Pro Pro2_default_1618586539591_12542 %

4.查看当前可用空间,只有8.4G长期使用肯定不够,所以需要扩充虚拟机硬盘

[root@localhost vagrant]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root  8.4G  1.2G  7.2G   15% /
devtmpfs                 488M     0  488M    0% /dev
tmpfs                    497M     0  497M    0% /dev/shm
tmpfs                    497M  6.5M  491M    2% /run
tmpfs                    497M     0  497M    0% /sys/fs/cgroup
/dev/sda1                497M  119M  379M   24% /boot
vagrant                  224G  178G   46G   80% /vagrant
var_www_html             224G  178G   46G   80% /var/www/html

5\. 停止虚拟机,因为下面需要扩容了,需要先停止虚拟机

vagrant halt
扩充虚拟机配置

1.开始保存 老的uuid,进入VirtualBox VMs目录,查看并记录原磁盘uuid,留作后用,其中 902de835-da8e-467e-ab01-b8e1643c6422 为现在的uuid

dl@dldeMacBook-Pro Pro2_default_1618586539591_12542 % vboxmanage showhdinfo "box-disk1.vmdk"
UUID:           95942ed1-608b-4443-b705-429a2c73db02
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       /Users/dl/VirtualBox VMs/Pro2_default_1618586539591_12542/box-disk1.vmdk
Storage format: VMDK
Format variant: dynamic default
Capacity:       10140 MBytes
Size on disk:   1591 MBytes
Encryption:     disabled
In use by VMs:  Pro2_default_1618586539591_12542 (UUID: f49f7c04-9386-46f3-be68-5a05de6ff23a)
dl@dldeMacBook-Pro Pro2_default_1618586539591_12542 %

2. 克隆磁盘,vmdk格式无法调整大小,需要转成vdi格式

dl@dldeMacBook-Pro Pro2_default_1618586539591_12542 % vboxmanage clonehd ./box-disk1.vmdk new-virtualdisk.vdi --format vdi
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone medium created in format 'vdi'. UUID: c8bcaf5d-f4b7-407c-8c49-8f3cd3c8856f

3.# 调整克隆磁盘的大小,这里调整为200G(多了不影响,数字可以大了填写)

dl@dldeMacBook-Pro Pro2_default_1618586539591_12542 % vboxmanage modifyhd new-virtualdisk.vdi --resize 209600
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

4.# 在克隆磁盘的基础上再克隆vdi格式的磁盘

dl@dldeMacBook-Pro Pro2_default_1618586539591_12542 % vboxmanage clonehd new-virtualdisk.vdi resized.vmdk --format vmdk
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone medium created in format 'vmdk'. UUID: 992d2453-af92-4ab9-891d-ea647d330a0b

5.替换以及备份

windows:
ren box-disk1.vmdk box-disk1-backup.vmdk
ren resized.vmdk box-disk1.vmdk
linux:
mv box-disk1.vmdk box-disk1-backup.vmdk
mv resized.vmdk box-disk1.vmdk
dl@dldeMacBook-Pro Pro2_default_1618586539591_12542 % mv box-disk1.vmdk box-disk1-backup.vmdk
dl@dldeMacBook-Pro Pro2_default_1618586539591_12542 % mv resized.vmdk box-disk1.vmdk

6.这一步没用,主要记录命令的,不用操心

dl@dldeMacBook-Pro Pro2_default_1618586539591_12542 % vboxmanage list vms
"Pro1_default_1548122083943_7572" {138d01ed-7f35-4e2d-a3d2-4b11382ab159}
"Pro2_default_1618586539591_12542" {f49f7c04-9386-46f3-be68-5a05de6ff23a}
dl@dldeMacBook-Pro Pro2_default_1618586539591_12542 % vboxmanage showvminfo f49f7c04-9386-46f3-be68-5a05de6ff23a
Name:                        Pro2_default_1618586539591_12542
Groups:                      /
Guest OS:                    Red Hat (64-bit)
UUID:                        f49f7c04-9386-46f3-be68-5a05de6ff23a
Config file:                 /Users/dl/VirtualBox VMs/Pro2_default_1618586539591_12542/Pro2_default_1618586539591_12542.vbox
Snapshot folder:             /Users/dl/VirtualBox VMs/Pro2_default_1618586539591_12542/Snapshots
Log folder:                  /Users/dl/VirtualBox VMs/Pro2_default_1618586539591_12542/Logs

7. !!!此时启动虚机或查看磁盘信息会报错,提示uuid不匹配,因为磁盘已经变了,需要改回之前记录的uuid

dl@dldeMacBook-Pro Pro2_default_1618586539591_12542 % vboxmanage internalcommands sethduuid box-disk1.vmdk 95942ed1-608b-4443-b705-429a2c73db02
UUID changed to: 95942ed1-608b-4443-b705-429a2c73db02

8 done,可以重启虚机了,可根据需要在虚机上进行磁盘分配,这里不再展开

vagrant up
正式扩容

1.进入虚拟机

vagrant reload #重新开启虚拟机
vagrant ssh # 进入虚拟机

2.查看磁盘详情

[root@localhost vagrant]# fdisk -l
磁盘 /dev/sda:219.8 GB, 219781529600 字节,429260800 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00095a8e

 设备 Boot      Start         End      Blocks   Id  System/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    20766719     9870336   8e  Linux LVM
/dev/sda3        20766720   429260799   204247040   8e  Linux LVM

磁盘 /dev/mapper/centos-root:8996 MB, 8996782080 字节,17571840 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/mapper/centos-swap:1065 MB, 1065353216 字节,2080768 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

3.操作下面的,一定要按照下面的步骤操作,其中 t 3 8e 很重要

fdisk /dev/sda
按p显示分区表,默认是 sda1 和 sda2。
按n新建主分区。
按p设置为主分区。
输入3设置为第三分区。
输入一次回车设置默认磁盘位置。
再输入一次回车设置默认磁盘位置。
输入t改变分区格式
输入3选择第三分区
输入8e格式成LVM格式
输入w执行保存

4.创建物理卷

[root@localhost vagrant]# pvcreate /dev/sda3
 Physical volume "/dev/sda3" successfully created5.显示物理卷属,并找到物理卷的名字  
[root@localhost vagrant]# pvdisplay | grep "VG Name"
 VG Name               centos VG Name6.显示所有卷组的属性  
[root@localhost vagrant]# vgdisplay
 --- Volume group --- VG Name               centos System ID Format                lvm2 Metadata Areas        1 Metadata Sequence No  3 VG Access             read/write VG Status             resizable MAX LV                0 Cur LV                2 Open LV               2 Max PV                0 Cur PV                1 Act PV                1 VG Size               9.41 GiB PE Size               4.00 MiB Total PE              2409 Alloc PE / Size       2399 / 9.37 GiB Free  PE / Size       10 / 40.00 MiB VG UUID               NJWfOH-An0K-Hm9Q-8Tjs-yq2x-ZWXi-L2vP7i 7.使用vgextend命令可以动态扩展卷组,它通过向卷组中添加物理卷来增加卷组的容量。

[root@localhost vagrant]# vgextend centos /dev/sda3
 Volume group "centos" successfully extended 8.查看文件系统的名字 为 /dev/mapper/centos-root
 [root@localhost vagrant]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root  8.4G  1.2G  7.2G   15% /
devtmpfs                 488M     0  488M    0% /dev
tmpfs                    497M     0  497M    0% /dev/shm
tmpfs                    497M  6.5M  491M    2% /run
tmpfs                    497M     0  497M    0% /sys/fs/cgroup
/dev/sda1                497M  119M  379M   24% /boot
vagrant                  224G  178G   46G   80% /vagrant
var_www_html             224G  178G   46G   80% /var/www/html

9.扩展逻辑卷 lvextend指令用于在线扩展逻辑卷的空间大小,而不中断应用程序对逻辑卷的访问。

[root@localhost vagrant]# lvextend /dev/mapper/centos-root /dev/sda3
 Size of logical volume centos/root changed from 8.38 GiB (2145 extents) to 203.16 GiB (52009 extents). Logical volume root successfully resized 

10.同步文件系统

[root@localhost vagrant]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=549120 blks
 =                       sectsz=512   attr=2, projid32bit=1 =                       crc=0        finobt=0data     =                       bsize=4096   blocks=2196480, imaxpct=25
 =                       sunit=0      swidth=0 blksnaming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
 =                       sectsz=512   sunit=0 blks, lazy-count=1realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2196480 to 53257216

11.重启虚拟机

[root@localhost vagrant]# reboot
Connection to 127.0.0.1 closed by remote host.
Connection to 127.0.0.1 closed.

12.重启并校验

dl@dldeMacBook-Pro Pro2 % vagrant reload
==> default: Attempting graceful shutdown of VM...
dl@dldeMacBook-Pro Pro2 % vagrant ssh
Last login: Fri Apr 16 17:56:12 2021 from 10.0.2.2
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ sudo su
[root@localhost vagrant]#

13 查看是否生效,这时候已经扩容好了,是不是比扩容iphone还简单

[root@localhost vagrant]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root  204G  1.2G  202G    1% /
devtmpfs                 488M     0  488M    0% /dev
tmpfs                    497M     0  497M    0% /dev/shm
tmpfs                    497M  6.5M  491M    2% /run
tmpfs                    497M     0  497M    0% /sys/fs/cgroup
/dev/sda1                497M  119M  379M   24% /boot
vagrant                  224G  178G   46G   80% /vagrant
var_www_html             224G  178G   46G   80% /var/www/html
[root@localhost vagrant]#
其他

1.开始操作硬盘

[root@Cent0S7 dnmp]# sudo 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.

2.按n新建主分区。

Command(m for help):p
Disk /dev/sda: 85.9 GB,85899345920 bytes,167772160 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 1abel type: dos
Disk identifier: 0x00091118

PI Device Boot Start En Blocks System
/dev/da1 2048 209/199 1048576 83 Linux
/dev/sda2 2099200 719999 14310400 8e Linux LVM
Command (m for help):n
Partition type:
primary (2 primary,0 extended,2 free)
d
e extended

3 按p设置为主分区

Select (default p):p
输入3设置为第三分区。

Partition number (3,4,default 3):3
First sector (30720000-167772159, default 30720000):
Using default value 30720000
Last sector,+sectors or +size{K,M,G} (30720000-167772159,default 167772159):
Using default value 167772159
Partition 3 of type Linux and of size 65.4 GiB is set

4 输入t改变分区格式

Command (m for help):t

5输入3选择第三分区

Partition number (1-3,deault 3):3

6 输入8e格式成LVM格式

Hex code (type L to list all codes):8e
Changed type of partition 'Linux' to 'Linux LVM' 

7 输入w执行保存Coand (m for help):w

The partition table has been a
Calling ioct1() to re-read partition table.
MARNING:Re-reading the partition table failed with error 16: Device or resource busy.
The kernel stil1 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.