默认情况下,磁盘大小为8GB,对于我的目的来说太小了。我想要做以下几点:
- 增加现有磁盘的大小以及其中的文件系统,而不需要销毁和重新创建我的虚拟机。
- 配置Vagrant,以便将来为此项目创建更大的磁盘。
安装这个插件: vagrant plugin install vagrant-disksize
编辑Vagrantfile
:
Vagrant.configure('2') do |config|
...
config.vm.box = 'ubuntu/xenial64'
config.disksize.size = '50GB'
...
end
vagrant halt && vagrant up
vagrant reload
一起使用sudo cfdisk /dev/sda
工具来调整我的/dev/sda1
分区,以适应所有新可用的空间。然后我告诉文件系统利用那个空间,使用命令sudo resize2fs -p -F /dev/sda1
。 - tutuDajuju主机操作系统:Windows 10
,Virtual Box:6.0
,虚拟机操作系统:Ubuntu Xenial
。 - Binita Bharati/opt
,然后使用rsync
将您的文件转移过去,以解决这个问题。毕竟,puppet vagrant boxes只用于测试目的。
原因是:VBoxManage modifyhd
只能处理本地VDI镜像。然而,vagrant基础镜像基本上是使用VMDK
格式导出的OVF/OVA。
--resize x
选项(其中x是所需的新总空间,以兆字节为单位)允许您更改现有映像的容量;这将调整虚拟磁盘的逻辑大小,而不会对物理大小造成太大影响。[37]目前,这仅适用于VDI和VHD格式,仅适用于动态分配的变体,并且只能用于扩展(而非缩小)容量。
步骤:
VBoxManage clonehd in.vmdk out.vdi --format VDI
,然后重新附加它(使用图形界面更容易)。VBoxManage modifyhd box.vdi --resize 15360
来调整大小,将容量增加到15GB。resize2fs -p -F DEVICE
。Vagrant.configure("2") do |config|
...
file_to_disk = File.realpath( "." ).to_s + "/disk.vdi"
if ARGV[0] == "up" && ! File.exist?(file_to_disk)
puts "Creating 5GB disk #{file_to_disk}."
vb.customize [
'createhd',
'--filename', file_to_disk,
'--format', 'VDI',
'--size', 5000 * 1024 # 5 GB
]
vb.customize [
'storageattach', :id,
'--storagectl', 'SATA Controller',
'--port', 1, '--device', 0,
'--type', 'hdd', '--medium',
file_to_disk
]
...
config.vm.provision "shell", path: "scripts/add_new_disk.sh"
...
end
在这里,add_new_disk.sh
shell脚本的样子是这样的:
set -e
set -x
if [ -f /etc/disk_added_date ]
then
echo "disk already added so exiting."
exit 0
fi
sudo fdisk -u /dev/sdb <<EOF
n
p
1
t
8e
w
EOF
pvcreate /dev/sdb1
vgextend VolGroup /dev/sdb1
lvextend /dev/VolGroup/lv_root
resize2fs /dev/VolGroup/lv_root
date > /etc/disk_added_date
config.vm.provider :virtualbox do |vb|
块中添加,以使事情更清晰明了。 - B Tlvextend
命令时出现“请指定大小或范围”错误,请尝试使用lvextend -l +100%FREE /dev/VolGroup/lv_root
命令。 - Andomarvagrant plugin install vagrant-disksize
如果你想在启动Vagrant时确保用户安装了插件,你可以在Vagrantfile的开头添加这个。
# Install vagrant-disksize to allow resizing the vagrant box disk.
unless Vagrant.has_plugin?("vagrant-disksize")
raise Vagrant::Errors::VagrantError.new, "vagrant-disksize plugin is missing. Please install it using 'vagrant plugin install vagrant-disksize' and rerun 'vagrant up'"
end
在 Vagrantfile 中设置所需的磁盘大小
vagrant.configure('2') do |config|
config.disksize.size = '50GB'
end
更新现有的vagrant box
``` 详细信息请参考https://packetpushers.net/ubuntu-extend-your-default-lvm-space/。
- undefinedVagrantfile
(参见下面的示例),在shell中设置VAGRANT_EXPERIMENTAL="disks"
环境变量,然后运行vagrant up
。Vagrant 2.2.19
上测试过的示例Vagrantfile
,使用基于LVM的bento/ubuntu-20.04
基础镜像:
Vagrant.configure("2") do |config|
...
config.vm.disk :disk, size: "150GB", primary: true
config.vm.box = "bento/ubuntu-20.04"
config.vm.box_version = "202005.21.0"
config.vm.box_check_update = false
...
end
vagrant up
过程中看到类似以下输出(或更多):==> vagrant: You have requested to enabled the experimental flag with the following features:
==> vagrant:
==> vagrant: Features: disks
==> vagrant:
==> vagrant: Please use with caution, as some of the features may not be fully
==> vagrant: functional yet.
Bringing machine 'server' up with 'virtualbox' provider...
==> server: Clearing any previously set forwarded ports...
==> server: Clearing any previously set network interfaces...
==> server: Preparing network interfaces based on configuration...
server: Adapter 1: nat
==> server: Forwarding ports...
server: 22 (guest) => 49222 (host) (adapter 1)
server: 4200 (guest) => 49280 (host) (adapter 1)
server: 4243 (guest) => 49243 (host) (adapter 1)
==> server: Configuring storage mediums...
server: Disk 'vagrant_primary' needs to be resized. Resizing disk...
这个过程可能需要一段时间才能完成(在我的情况下,需要1到2小时)。
使用vagrant ssh
进入您的虚拟机,然后使用以下命令(基于this answer)收集有关您的文件系统的信息,然后扩展LVM卷。如果您的基础框不使用LVM,则不应使用这些命令。
# Understand your partitions.
> lsblk
> df -lhT /
> sudo fdisk -l /dev/sda
> sudo vgdisplay
> sudo parted /dev/sda print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 161GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 538MB 537MB primary fat32 boot
2 539MB 68.7GB 68.2GB extended
5 539MB 68.7GB 68.2GB logical lvm
# Resize your partitions.
> sudo parted /dev/sda resizepart 2 100%
> sudo parted /dev/sda resizepart 5 100%
> sudo parted /dev/sda print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 161GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 538MB 537MB primary fat32 boot
2 539MB 161GB 161GB extended
5 539MB 161GB 161GB logical lvm
# Resize your LVM physical volume to match the new partition extents.
> sudo pvresize /dev/sda5
> sudo vgdisplay
Alloc PE / Size 16255 / <63.50 GiB
Free PE / Size 22016 / 86.00 GiB
# Resize your LVM logical volume to fill the available Free space.
> sudo lvdisplay
--- Logical volume ---
LV Path /dev/vgvagrant/root
> sudo lvextend -l+100%FREE /dev/vgvagrant/root
Size of logical volume vgvagrant/root changed from <62.54 GiB (16010 extents) to <148.54 GiB (38026 extents).
Logical volume vgvagrant/root successfully resized.
# Apply the changes to the filesystem.
> sudo resize2fs /dev/vgvagrant/root
> df -lhT /
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vgvagrant-root ext4 146G 55G 85G 40% /
在主机上安装插件:
vagrant plugin install vagrant-disksize
编辑vagrant文件以设置磁盘大小:
vagrant.configure('2') do |config| config.vm.box = 'centos/7' config.disksize.size = '50GB' end
3:启动虚拟机 4:安装growpart并将磁盘大小扩展到最大
yum install epel-release -y
yum install cloud-utils-growpart.x86_64 -y
growpart /dev/sda 1
5:下载gparted live iso镜像并将其作为光驱附加到虚拟机,使用Virtualbox GUI引导到gpartedlive 重新启动使用gparted live
6:使用gparted检查分区以清除错误标志
sudo df -h
[vagrant@rhel-cdk ~]$ sudo df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-root 8G 2,5G 6,5G 28% /
devtmpfs 234M 0 234M 0% /dev
tmpfs 245M 0 245M 0% /dev/shm
tmpfs 245M 4,3M 241M 2% /run
tmpfs 245M 0 245M 0% /sys/fs/cgroup
/dev/sda2 297M 134M 164M 45% /boot
tmpfs 49M 0 49M 0% /run/user/1000
/dev/mapper/VolGroup00-root
文件系统对应的卷组确实有额外的空间(我想,在虚拟机中是动态分配的)。sudo vgdisplay VolGroup00
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 11
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size 39,70 GiB
PE Size 4,00 MiB
Total PE 10164
Alloc PE / Size 7872 / 30,75 GiB
Free PE / Size 2292 / 8,95 GiB
VG UUID JBVwpl-13KX-HbQw-FqUa-CA9w-swpF-dF6glm
所剩下的事情只有:
a)将我们的逻辑卷大小增加10G左右
sudo lvextend -L+10G /dev/VolGroup00/root
和
b)更新您的根文件系统,以便它意识到这个变化
sudo xfs_growfs /dev/mapper/VolGroup00-root
(请注意,至少对于rhel 7.2,resize2fs
不能用于此目的)。
之后,我的文件系统就有了额外的空间:
sudo df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-root 18G 2,5G 16G 14% /
devtmpfs 234M 0 234M 0% /dev
tmpfs 245M 0 245M 0% /dev/shm
tmpfs 245M 4,3M 241M 2% /run
tmpfs 245M 0 245M 0% /sys/fs/cgroup
/dev/sda2 297M 134M 164M 45% /boot
tmpfs 49M 0 49M 0% /run/user/1000
... 在虚拟机层面没有干预
###########################################################################################################
# The in-line script that will be used to grow our disk partition to the size specified in our Vagrantfile.
###########################################################################################################
$script = <<-SCRIPT
#!/bin/bash
ROOT_DISK_DEVICE="/dev/sda"
ROOT_DISK_DEVICE_PART="/dev/sda5"
LV_PATH=`sudo lvdisplay -c | sed -n 1p | awk -F ":" '{print $1;}'`
FS_PATH=`df / | sed -n 2p | awk '{print $1;}'`
ROOT_FS_SIZE=`df -h / | sed -n 2p | awk '{print $2;}'`
echo "The root file system (/) has a size of $ROOT_FS_SIZE"
echo "> Increasing disk size of $ROOT_DISK_DEVICE to available maximum"
sudo parted /dev/sda resizepart 2 100%
sudo parted /dev/sda resizepart 5 100%
sudo pvresize $ROOT_DISK_DEVICE_PART
sudo lvextend -l +100%FREE $LV_PATH
sudo resize2fs -p $FS_PATH
ROOT_FS_SIZE=`df -h / | sed -n 2p | awk '{print $2;}'`
echo "The root file system (/) has a size of $ROOT_FS_SIZE"
exit 0
SCRIPT
# Fail if the vagrant-disksize plugin is not installed
unless Vagrant.has_plugin?("vagrant-disksize")
raise 'vagrant-disksize is not installed!'
end
Vagrant.configure("2") do |config|
config.disksize.size = "150GB"
config.vm.provision "shell", inline: $script
end
kitchen.yml
中添加以下小片段: vagrantfiles:
- Vagrantfile
provision: True
driver:
name: vagrant