侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

Resize a Hard Disk for a Virtual Machine

2023-11-18 星期六 / 0 评论 / 0 点赞 / 48 阅读 / 12886 字

vagrant box “centos mini版”磁盘空间就8GB,完全不够用,在网上找到了“调整虚拟机的硬盘大小”的资料,现在扩展 192.168.2.207 到 100G一些假设您在主机上使用C

vagrant box “centos mini版”磁盘空间就8GB,完全不够用,在网上找到了“调整虚拟机的硬盘大小”的资料,现在扩展 192.168.2.207 到 100G

一些假设

您在主机上使用Cygwin或Linux命令行终端

  • VirtualBox安装路径在您的Windows(因此Cygwin bash)PATH环境变量中
  • vagrant boxes 存放在 /home/vagrant/dev
  • 您的Cygwin HOME路径与Windows相同%USERPROFILE%(请参阅如何在安装后更改Cygwin HOME文件夹)
  • VirtualBox在默认位置创建新的虚拟机 ~/VirtualBox/ VMs/

确定207 的虚拟磁盘位置

cd /home/data/VirtualBox/ VMs[root@localhost VirtualBox VMs]# lltotal 0drwx------ 3 root root 131 Mar 13 04:48 ai_default_1489390490739_11292drwx------ 3 root root 147 Dec 30 01:27 appBackend_default_1483065544021_24430drwx------ 3 root root 135 Mar 31 20:38 mysql_default_1483014696712_4321drwx------ 3 root root 145 Dec 29 07:40 taojinyun_default_1483015223615_68727drwx------ 3 root root 135 Mar 13 02:14 test_default_1482912811775_56919

发现 Downloads_default_1482908322748_17591 没有找到对应的虚拟机于是

mv Downloads_default_1482908322748_17591 bak

便于以后安心的删除

vagrant box listaiCentOS7           (virtualbox, 0)     ai_default_1489390490739_11292appBackendCentOS7   (virtualbox, 0)     appBackend_default_1483065544021_24430mysqlCentOS7        (virtualbox, 0)     mysql_default_1483014696712_4321taojinyunDevCentOS7 (virtualbox, 0)     taojinyun_default_1483015223615_68727testCentOS7         (virtualbox, 0)     test_default_1482912811775_56919vboxmanage list runningvms"vm207_default_1491469943499_44735" {33f77282-d2f9-4eae-973a-4e26bfa9abf3}"test_default_1482912811775_56919" {52767702-6e8a-4951-9a66-a1d5045bff89}"mysql_default_1483014696712_4321" {003531ef-f730-429d-853a-7bdf9904dac5}"taojinyun_default_1483015223615_68727" {f1ff3dc1-782a-4517-a5a2-d4e4fcc75add}"appBackend_default_1483065544021_24430" {346d48df-dae1-4b1d-8774-16e8ee5067b5}"ai_default_1489390490739_11292" {c7eacb4a-3beb-4f33-9bbc-b36c3b1b38d3}

调整硬盘大小的步骤

1 使用Vagrant停止虚拟机

cd /home/vagrant/devvagrant halt

2 找到VirtuaBox VM和连接到其SATA控制器的HDD

cd /home/data/VirtualBox/ VMs/vm207_default_1491469943499_44735VBoxManage showvminfo vm207_default_1491469943499_44735 | grep ".vmdk"

该showvminfo命令应该显示在VMDK类型的HDD的文件系统上的位置以及它附加到的Controller的名称 - 它将如下所示:

SATA Controller (0, 0): /home/data/VirtualBox VMs/vm207_default_1491469943499_44735/box-disk1.vmdk (UUID: eed0c68c-d82a-4181-82ca-3b0781e29907)

3 将VMDK类型的磁盘克隆到VDI类型的磁盘,以便可以调整大小

cd /root/VirtualBox/ VMs/vm207_default_1491469943499_44735// 上面地址是下面地址的软链接 (我在下面目录操作的)cd /home/data/VirtualBox/ VMs/vm207_default_1491469943499_44735VBoxManage clonehd "box-disk1.vmdk" "vm207-100.vdi.vdi" --format VDI

注意:我们这样做是因为VMDK类型的磁盘无法通过VirtualBox调整大小。它具有额外的好处,允许我们在调整大小操作期间保留原始磁盘备份。

VBoxManage clonehd 如果报错:

    VBoxManage: error: Cannot register the hard disk '/home/data/VirtualBox VMs/vm207_default_1491469943499_44735/box-disk1.vmdk' {94d5960b-4800-4a57-9994-397f6d6cdcb0} because a hard disk '/root/VirtualBox VMs/vm207_default_1491469943499_44735/box-disk1.vmdk' with UUID {94d5960b-4800-4a57-9994-397f6d6cdcb0} already exists    VBoxManage: error: Details: code NS_ERROR_INVALID_ARG (0x80070057), component VirtualBoxWrap, interface IVirtualBox, callee nsISupports    VBoxManage: error: Context: "OpenMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType, enmAccessMode, fForceNewUuidOnOpen, pMedium.asOutParam())" at line 179 of file VBoxManageDisk.cpp

解决办法需要重新生成新的UUID,virtualbox有这个命令, 试一试:

vboxmanage internalcommands sethduuid box-disk1.vmdkUUID changed to: 54bb055f-06fb-49c8-8767-adb3ddc0a366

这样vdi虚拟盘救生成了新的UUID

http://88fly.blog.163.com/blog/static/12268039020130253633387/

4 查看磁盘当前有多大,以确定在调整大小时有多大

信息将显示当前大小和格式变体。如果使用动态分配创建磁盘,则 “Format variant”将为“dynamic default”。

VBoxManage showhdinfo "vm207-100.vdi.vdi"    UUID:           fd3709ad-29a4-4a51-939b-6821d5ca07f2    Parent UUID:    base    State:          created    Type:           normal (base)    Location:       /home/data/VirtualBox VMs/vm207_default_1491469943499_44735/vm207-100.vdi.vdi    Storage format: VDI    Format variant: dynamic default    Capacity:       10140 MBytes    Size on disk:   1203 MBytes    Encryption:     disabled

5 调整克隆的磁盘大小以给它更多的空间

下面的大小参数以兆字节(1024字节= 1兆字节)给出。因为这个磁盘是使用动态分配创建的,所以我将把它调整到100吉字节。

VBoxManage modifyhd "vm207-100.vdi.vdi" --resize 102400
.

注意:如果使用动态分配创建磁盘(请参阅上一步),则磁盘的物理大小将不需要与其逻辑大小相匹配- 这意味着您可以创建一个非常大的逻辑磁盘,只有使用空间的时候才在物理磁盘上增加尺寸。

. .

提示:要使用在线计算器将千兆字节值转换为兆字节。

.

6 找出存储控制器的名称,以将新调整大小的磁盘附加到它上面

VBoxManage showvminfo vm207_default_1491469943499_44735 | grep "Storage"    Storage Controller Name (0):            IDE Controller    Storage Controller Type (0):            PIIX4    Storage Controller Instance Number (0): 0    Storage Controller Max Port Count (0):  2    Storage Controller Port Count (0):      2    Storage Controller Bootable (0):        on    Storage Controller Name (1):            SATA Controller    Storage Controller Type (1):            IntelAhci    Storage Controller Instance Number (1): 0    Storage Controller Max Port Count (1):  30    Storage Controller Port Count (1):      1    Storage Controller Bootable (1):        on

7 将新调整大小的磁盘附加到虚拟机的存储控制器。在我们的例子中,我们将使用相同的名称进行存储控制器SATA Controller,如上面的步骤所示。

VBoxManage storageattach vm207_default_1491469943499_44735 --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium vm207-100.vdi.vdi

8 使用Vagrant重新启动虚拟机

cd /home/vagrant/dev/vm207vagrant up

如果没有报错,直接下一步了;如果启动报错

    default: SSH address: 127.0.0.1:2222    default: SSH username: vagrant    default: SSH auth method: private key    default: Warning: Remote connection disconnect. Retrying...    default: Warning: Authentication failure. Retrying...    default: Warning: Authentication failure. Retrying...    default: Warning: Authentication failure. Retrying...    ...

Ctrol+C 强制退出,执行:

vboxmanage controlvm vm207_default_1491469943499_44735 keyboardputscancode 1c

在走下一步,

9 通过ssh在虚拟机上以root身份打开命令行shell

vagrant sshsudo su
账户默认密码是 vagrantvagrant [email protected]'s password:

10 查找映射文件系统的逻辑卷的名称(即 /dev/mapper/centos-root)

df

11 查找所有分区的物理卷(或设备)的名称(即 /dev/sda)

fdisk -l

12 创建一个用作Linux LVM的新主分区

fdisk /dev/sda
  • 按 p 打印分区表以标识分区数。默认情况下有两个 - sda1和sda2。

  • 按 n 创建一个新的主分区。

  • 按 p 主分区。

  • 按 3 分区号,按分区表的输出打印。

  • 按 Enter 键2次 以接受默认的第一个和最后一个柱面。

  • 按 t 更改系统的分区ID

  • 按 3 选择新创建的分区

  • 键入 8e 以更改分区的十六进制代码Linux LVM

  • 按下 w 将更改写入分区表。

  • Press p to print the partition table to identify the number of partitions. By default there are two - sda1 and sda2.

  • Press n to create a new primary partition.

  • Press p for primary.

  • Press 3 for the partition number, depending the output of the partition table print.

  • Press Enter two times to accept the default First and Last cylinder.

  • Press t to change the system's partition ID

  • Press 3 to select the newly creation partition

  • Type 8e to change the Hex Code of the partition for Linux LVM

  • Press w to write the changes to the partition table.

13 重新启动机器,然后再次重新插入,并再次切换到root用户

reboot// 大概要等个一分钟的样子在执行下面的命令vagrant sshsudo su -

14 使用刚刚创建的新主分区创建新的物理卷。

pvcreate /dev/sda3

15 找出逻辑卷映射所属的卷组的名称(即 centos )

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

16 扩展卷组以使用新创建的物理卷。

vgextend centos /dev/sda3

17 扩展逻辑卷以使用现在可用的更多的卷组大小

您可以告诉它以兆字节,千兆或太字节来添加一定量的空间,并控制磁盘的增长:(我用下面命令成了)

lvextend -L+20G /dev/mapper/centos-root

或者如果要使用现在可用于卷组的所有可用空间:

lvextend -l +100%FREE /dev/mapper/centos-root

18 调整文件系统的大小以占用逻辑卷中提供的空间

resize2fs /dev/mapper/centos-root

在centos 7下,这一步会出错

resize2fs 1.42.9 (28-Dec-2013)resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-rootCouldn't find valid filesystem superblock.

这时,只需要使用 xfs_growfs 命令替换就行了

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=0    data     =                       bsize=4096   blocks=2196480, imaxpct=25             =                       sunit=0      swidth=0 blks    naming   =version 2              bsize=4096   ascii-ci=0 ftype=0    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 2196480 to 25824256

19 Verfiy,现在有更多的空间可用

df -h

20 使用vagrant重新启动VM可能是一个好主意,以确保所有服务正在运行正确,现在有更多的空间可用。退出root用户,退出vagrant用户和ssh会话,然后告诉vagrant重启机器。

exitexitvagrant reload --provision

参考:

https://gist.github.com/christopher-hopper/9755310

https://www.madcoder.cn/vagrant-box-resize.html

https://www.v2ex.com/t/146608

https://ma.ttias.be/increase-expand-xfs-filesystem-in-red-hat-rhel-7-cento7/

广告 广告

评论区