vb.customize 'storageattach' 第一次挂载我的磁盘,但在 vagrant halt; vagrant up 后更改会丢失。

4
我刚接触 Vagrant,正在尝试为我创建的虚拟机添加第二个磁盘。我已经学会了在首次启动虚拟机时附加磁盘,但是当我关闭虚拟机并再次启动(使用“vagrant up --provision”确保提供者运行)时,对磁盘所做的更改都会丢失。
我两次都运行了日志记录,并且第二次运行(在机器最初配置后重新启动)的日志输出显示正在执行 storageattach 命令,但我在 "/dev/shm" 下创建的每个文件(这似乎是第二个磁盘的挂载点)都消失了。
失败模式是:
vagrant up ...
 touch /dev/shm/some.file
 ls /dev/shm/some.file   # see output here... 

停止vagrant虚拟机:

运行vagrant虚拟机并重新进行配置:

ls /dev/shm/some.file     #  no such file or directory.. where did it go ? 

任何建议都将不胜感激。
我的Vagrantfile如下:
...
Vagrant.require_version ">= 1.4.3"
VAGRANTFILE_API_VERSION = "2"
disk = './secondDisk.vdi'
BOX_NAME="test"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.define :master do |master|
        master.vm.box = "centos65"
        master.vm.box_url = "https://github.com/2creatives/vagrant-centos/releases/download/v6.5.1/centos65-x86_64-20131205.box"
        master.vm.provider "virtualbox" do |v|
          v.customize ["modifyvm", :id, "--memory", "4196"]
          v.name = BOX_NAME
        end
        master.vm.network :private_network, ip: "192.168.33.10"
        master.vm.hostname = BOX_NAME
    end

    config.vm.synced_folder(".", "/vagrant",
        :owner => "vagrant",
        :group => "vagrant",
        :mount_options => ['dmode=777','fmode=777']
    )
    config.vm.provider "virtualbox" do |vb|
        unless File.exist?(disk)
            vb.customize ['createhd', '--filename', disk, '--variant', 'Fixed', '--size', 1 * 1024]
        end
        vb.customize ['storageattach', :id,  '--storagectl', 'SATA', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', disk]
    end
end

以下是第二次执行 'vagrant up --provision' 的日志输出结果。[ 我使用 --provision 来确保每次 vagrant up 都完成所有配置步骤 ]:

INFO sanedefaults: Automatically figuring out whether to enable/disable NAT DNS proxy...
 INFO subprocess: Starting process: ["C:\\Program Files\\Oracle\\VirtualBox\\VBoxManage.exe", "modifyvm", "ea5c09                              e7-11e7-4630-a7ca-ec66461b9eb6", "--natdnsproxy1", "on"]
DEBUG subprocess: Selecting on IO
DEBUG subprocess: Waiting for process to exit. Remaining to timeout: 32000
DEBUG subprocess: Exit status: 0
 INFO warden: Calling IN action: #<VagrantPlugins::ProviderVirtualBox::Action::Customize:0x3dc9818>
 INFO interface: info: Running 'pre-boot' VM customizations...
 INFO interface: info: ==> master: Running 'pre-boot' VM customizations...
==> master: Running 'pre-boot' VM customizations...
 INFO subprocess: Starting process: ["C:\\Program Files\\Oracle\\VirtualBox\\VBoxManage.exe", "storageattach", "e                              a5c09e7-11e7-4630-a7ca-ec66461b9eb6", "--storagectl", "SATA", "--port", "1", "--device", "0", "--type", "hdd", "-                              -medium", "./secondDisk.vdi"]
DEBUG subprocess: Selecting on IO
DEBUG subprocess: Waiting for process to exit. Remaining to timeout: 32000
DEBUG subprocess: Exit status: 0

你有按照这个网址 https://gist.github.com/leifg/4713995 的设置进行操作吗?有什么不同吗? - BMW
我有一些来自我的同事彼得·威尔斯的建议,我现在将发布。我将尝试创建一个小的“挂载”脚本,以与我的 Vagrant 配方集成,并在成功后发布完整结果。>>(来自彼得):您正在寻找 /dev/shm 下的额外硬盘,但根据 mount 命令的输出:tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")因此,/dev/shm 实际上是一个临时文件系统。第二个硬盘已连接,但没有分区并且未挂载。 - Chris Bedford
1个回答

6
感谢宝马公司提供精心设计和时尚的答案,以及Peter的帮助。引用的文章(gist.github.com/leifg/4713995)包含了所需的方法,下面我将在Vagrant脚本和相应的引导文件中复现这些方法,从而创建一个新添加的第二磁盘的文件系统,并将其添加到/etc/fstab中。这完全解决了我的问题【不再出现数据消失】。
Vagrantfile:
Vagrant.require_version ">= 1.4.3"
VAGRANTFILE_API_VERSION = "2"

disk = './secondDisk.vdi' 
BOX_NAME="test"


Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.define :master do |master|
        master.vm.box = "centos65"
        master.vm.box_url = "https://github.com/2creatives/vagrant-centos/releases/download/v6.5.1/centos65-x86_64-20131205.box"
        master.vm.provider "virtualbox" do |v|
          v.customize ["modifyvm", :id, "--memory", "4196"]
          v.name = BOX_NAME
        end
        master.vm.network :private_network, ip: "192.168.33.10"
        master.vm.hostname = BOX_NAME
    end

    config.vm.synced_folder(".", "/vagrant",
        :owner => "vagrant",
        :group => "vagrant",
        :mount_options => ['dmode=777','fmode=777']
    )

    # create the second disk and attach it
    config.vm.provider "virtualbox" do |vb|
        unless File.exist?(disk)
            vb.customize ['createhd', '--filename', disk, '--variant', 'Fixed', '--size', 1 * 1024]
        end

        vb.customize ['storageattach', :id,  '--storagectl', 'SATA', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', disk]
    end

    # NEW - invoke script which  partitions the new disk (/dev/sdb) 
    # and create mount directives in /etc/fstab
    #config.vm.provision :shell, path: "bootstrap.sh"  
    config.vm.provision "shell" do |shell|
        shell.inline = "sudo /vagrant/bootstrap.sh"  
    end
end

Bootstrap脚本:

#!/bin/bash  -x

#   configure and mount second disk 
#
yum install -y parted
parted /dev/sdb mklabel msdos
parted /dev/sdb mkpart primary 512 100%
mkfs.xfs /dev/sdb1
mkdir /mnt/disk
echo `blkid /dev/sdb1 | awk '{print$2}' | sed -e 's/"//g'` /mnt/disk   xfs   noatime,nobarrier   0   0 >> /etc/fstab
mount /mnt/disk

不错的回答。只有一个问题:parted /dev/sdb mkpart primary 512 100%。"512"是以MB为单位,所以分区从512MB开始。 - Pierre Emmanuel Lallemant
1
谢谢。我已经有一段时间没有做这些东西了,但如果我回去做的话,我会记住你的评论;^) - Chris Bedford

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接