LoginSignup
1
2

More than 1 year has passed since last update.

Vagrantチートシート

Last updated at Posted at 2019-10-11

synced_folderで.gitを除外する

Vagrantfile
config.vm.synced_folder ".", "/vagrant", type: "rsync", rsync__exclude: ".git/"

双方向に読み書きできるsynced folderをつくる

type:"virtualbox"をつけると双方向に読み書きできるsynced folderになる。
Guest Additionが必須。

Vagrantfile
config.vm.synced_folder "./output", "/vagrant/output", type:"virtualbox"

以下のようにすると、Vagrantfileのあるディレクトリを通常のrsyncで同期しつつ、outputディレクトリだけ双方向で同期できる。
vagrantでビルド環境を構築するときなどに便利。

Vagrantfile
config.vm.synced_folder "./", "/vagrant", type: "rsync", rsync__exclude: ".git/"
config.vm.synced_folder "./output", "/vagrant/output", type:"virtualbox"

VMを再起動してプロビジョニングをやりなおす

$ vagrant reload --provision

vagrantコマンドのサブコマンドを補完してほしい

$ brew install vagrant-completion

boxのGuest Additionsのバージョンを上げる

$ vagrant plugin install vagrant-vbguest

destroyしてupすると自動的に新しいGuest Additionsをビルドするようになる。

プライベートネットワークのゲスト同士が通信できない

192.168.33.1はホストのIPアドレスとして割り当てられているので、ゲストでこのアドレスを使用することができない。(設定はできてしまうし警告も出る)
ゲスト同士で疎通できないときはうっかり割り当てていないか確認する。

VMのディスク容量を増やす

vagrant-disksizeを入れる

vagrant up

vagrant ssh

partedでパーティション容量を拡張する

resize2fsでファイルシステムを拡張する

Stderr: VBoxManage: error: Cannot unregister the machine とかいわれてdestroyできない

VBoxHeadlessのプロセスをkillしてからvagrant destroyやりなおしたらよいっぽい。

めも

  config.vm.define "fakeroot" do |host|
    host.vm.hostname = "fakeroot"
    host.vm.network "private_network", ip: "192.168.56.254", netmask: "24"
    host.vm.provision "file", source: "./setup_gateway.sh", destination: "/tmp/"
    host.vm.provision "shell", inline: <<-SHELL
      chmod +x /tmp/setup_gateway.sh
      sudo /tmp/setup_gateway.sh fakeroot 10.50.0.1
      rm /etc/tinc/machikado_network/hosts/syamimomo
      sed -i -e '/ConnectTo = syamimomo/ d' /etc/tinc/machikado_network/tinc.conf
    SHELL
    host.trigger.after :up do |trigger|
      #trigger.run = {inline: "(vagrant ssh fakeroot -c 'cat /etc/tinc/machikado_network/hosts/fakeroot') > #{File.join(tmpdir, "fakeroot")} 2>/dev/null"}
      #trigger.run_remote = {inline: "uname -a"}
      trigger.ruby do |env, machine|
        machine.communicate.sudo("cat /etc/tinc/machikado_network/hosts/fakeroot | awk '/-----BEGIN RSA PUBLIC KEY-----/,/-----END RSA PUBLIC KEY-----/' | grep -v -e '-----BEGIN RSA PUBLIC KEY-----' -e '-----END RSA PUBLIC KEY-----' | tr -d '\r\n' | grep .") do |type, output|
          $store[:fakeroot_key] = output
        end
      end
    end
  end

  # まちかどネットワークゲートウェイの想定のVM
  config.vm.define "gateway" do |host|
    host.vm.hostname = "gateway"
    host.vm.network "private_network", ip: "192.168.56.2", netmask: "24"
    host.vm.network "private_network", ip: "192.168.57.2", netmask: "24"
    host.vm.provision "file", source: "./setup_gateway.sh", destination: "/tmp/"
    host.vm.provision "shell", inline: <<-SHELL
      chmod +x /tmp/setup_gateway.sh
      sudo /tmp/setup_gateway.sh gateway 10.50.0.2
    SHELL
    host.trigger.after :up do |trigger|
      trigger.run_remote = {inline: "echo '#{$store[:fakeroot_key]}' | fold "}
      trigger.ruby do |env, machine|
        k = $store[:fakeroot_key]
        k.scan(/.{1,64}/)
        #puts "gateway trigger.ruby: "
        #puts $store[:fakeroot]
        #machine.communicate.sudo("cat /etc/tinc/machikado_network/hosts/fakeroot") do |type, output|
        #  $store[:fakeroot] = output
        #end
      end
    end
  end
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2