xref: /spdk/scripts/vagrant/Vagrantfile (revision bc0180f67f3b82d96298591f3c906346abd0b836)
1# -*- mode: ruby -*-
2# vi: set ft=ruby :
3
4require 'open3'
5def checkboxtype(distro)
6  localboxes, stderr, status = Open3.capture3("vagrant box list")
7  if localboxes.include? "spdk/"+distro
8    return "spdk/"+distro
9  else
10      case distro
11      when "centos7"
12        return "centos/7"
13      when "centos8"
14        return "centos/8"
15      when "ubuntu1604"
16        return "peru/ubuntu-16.04-server-amd64"
17      when "ubuntu1804"
18        return "peru/ubuntu-18.04-server-amd64"
19      when "fedora30"
20        return "generic/fedora30"
21      when "fedora31"
22        return "generic/fedora31"
23      when "arch"
24        return "generic/arch"
25      when "freebsd11"
26        return "generic/freebsd11"
27      when "freebsd12"
28        return "generic/freebsd12"
29      else
30        "Invalid argument #{distro}"
31        abort("Invalid argument!")
32      end
33  end
34end
35
36Vagrant.configure(2) do |config|
37
38  # Pick the right distro and bootstrap, default is fedora30
39  distro = ( ENV['SPDK_VAGRANT_DISTRO'] || "fedora30")
40  provider = (ENV['SPDK_VAGRANT_PROVIDER'] || "virtualbox")
41
42  # generic/freebsd boxes do not work properly with vagrant-proxyconf and
43  # have issues installing rsync and sshfs for syncing files. NFS is
44  # pre-installed, so use it.
45  # generic/fedora boxes on the other hand have problems running NFS
46  # service so use sshfs+rsync combo instead.
47  plugins_sync_backend = {type: :sshfs}
48  files_sync_backend = {type: "rsync", rsync__auto: false}
49  if distro.include? "freebsd"
50    plugins_sync_backend = {type: :nfs, nfs_udp: false}
51    files_sync_backend = {type: :nfs, nfs_udp: false,  mount_options: ['ro']}
52  end
53  config.vm.box = checkboxtype(distro)
54  config.vm.box_check_update = false
55  config.vm.synced_folder '.', '/vagrant', disabled: true
56
57  # Copy in the .gitconfig if it exists
58  if File.file?(File.expand_path("~/.gitconfig"))
59    config.vm.provision  "file", source: "~/.gitconfig", destination: ".gitconfig"
60  end
61
62  # Copy the tsocks configuration file for use when installing some spdk test pool dependencies
63  if File.file?("/etc/tsocks.conf")
64    $tsocks_copy = <<-SCRIPT
65    sudo -s
66    mv -f tsocks.conf /etc/tsocks.conf
67    chown root /etc/tsocks.conf
68    chmod 644 /etc/tsocks.conf
69    SCRIPT
70    config.vm.provision  "file", source: "/etc/tsocks.conf", destination: "tsocks.conf"
71    config.vm.provision "shell", inline: $tsocks_copy
72  end
73
74  # vagrant-cachier caches apt/yum etc to speed subsequent
75  # vagrant up
76  # to enable, run
77  # vagrant plugin install vagrant-cachier
78  #
79  if Vagrant.has_plugin?("vagrant-cachier")
80    config.cache.scope = :box
81    config.cache.synced_folder_opts = plugins_sync_backend
82  end
83
84  # use http proxy if avaiable
85  if ENV['http_proxy']
86    if Vagrant.has_plugin?("vagrant-proxyconf")
87      config.proxy.http     = ENV['http_proxy']
88      config.proxy.https    = ENV['https_proxy']
89      config.proxy.no_proxy = "localhost,127.0.0.1"
90    end
91
92    # Proxyconf does not seem to support FreeBSD boxes or at least it's
93    # docs do not mention that. Set up proxy configuration manually.
94    if distro.include?("freebsd")
95      $freebsd_proxy = <<-SCRIPT
96      sudo -s
97      echo "export http_proxy=#{ENV['http_proxy']}" >> /etc/profile
98      echo "export https_proxy=#{ENV['http_proxy']}" >> /etc/profile
99      echo "pkg_env: {http_proxy: #{ENV['http_proxy']}}" > /usr/local/etc/pkg.conf
100      chown root:wheel /usr/local/etc/pkg.conf
101      chmod 644 /usr/local/etc/pkg.conf
102      SCRIPT
103      config.vm.provision "shell", inline: $freebsd_proxy
104    end
105  end
106
107  # freebsd boxes in order to have spdk sources synced from host properly
108  # will use NFS with "ro" option enabled to prevent changes on host filesystem.
109  # To make sources usable in the guest VM we need to unmount them and use
110  # local copy.
111  if distro.include?("freebsd")
112    $freebsd_spdk_repo = <<-SCRIPT
113    sudo -s
114    cp -R /home/vagrant/spdk_repo/spdk /tmp/spdk
115    umount /home/vagrant/spdk_repo/spdk && rm -rf /home/vagrant/spdk_repo/spdk
116    mv /tmp/spdk /home/vagrant/spdk_repo/spdk
117    chown -R vagrant:vagrant /home/vagrant/spdk_repo/spdk
118    SCRIPT
119    config.vm.provision "shell", inline: $freebsd_spdk_repo
120  end
121
122  vmcpu=(ENV['SPDK_VAGRANT_VMCPU'] || 2)
123  vmram=(ENV['SPDK_VAGRANT_VMRAM'] || 4096)
124  spdk_dir=(ENV['SPDK_DIR'] || "none")
125  vmemulator=(ENV['SPDK_QEMU_EMULATOR'] || "")
126  emulated_nvme_types=(ENV['NVME_DISKS_TYPE'] || "nvme").split(',')
127  nvme_namespaces=(ENV['NVME_DISKS_NAMESPACES'] || "").split(',')
128  nvme_file=(ENV['NVME_FILE'] || "").split(',')
129  vagrantfile_dir=(ENV['VAGRANTFILE_DIR'] || "none")
130
131  config.ssh.forward_agent = true
132  config.ssh.forward_x11 = true
133  if ENV['VAGRANT_PASSWORD_AUTH'] == "1"
134    config.ssh.username = "vagrant"
135    config.ssh.password = "vagrant"
136  end
137
138  config.vm.provider "virtualbox" do |vb|
139    vb.customize ["modifyvm", :id, "--ioapic", "on"]
140    vb.memory = "#{vmram}"
141    vb.cpus = "#{vmcpu}"
142
143    nvme_disk=(ENV['NVME_FILE'] || "nvme_disk.img")
144    unless File.exist? (nvme_disk)
145      vb.customize ["createhd", "--filename", nvme_disk, "--variant", "Fixed", "--size", "1024"]
146      vb.customize ["storagectl", :id, "--name", "nvme", "--add", "pcie", "--controller", "NVMe", "--portcount", "1", "--bootable", "off"]
147      vb.customize ["storageattach", :id, "--storagectl", "nvme", "--type", "hdd", "--medium", nvme_disk, "--port", "0"]
148    end
149
150    #support for the SSE4.x instruction is required in some versions of VB.
151    vb.customize ["setextradata", :id, "VBoxInternal/CPUM/SSE4.1", "1"]
152    vb.customize ["setextradata", :id, "VBoxInternal/CPUM/SSE4.2", "1"]
153  end
154
155  # This setup was Tested on Fedora 27
156  # libvirt configuration need modern Qemu(tested on 2.10) & vagrant-libvirt in version 0.0.39+
157  # There are few limitation for SElinux - The file added outside libvirt must have proper SE ACL policy or setenforce 0
158  config.vm.provider "libvirt" do |libvirt, override|
159    libvirt.random_hostname = "1"
160    libvirt.disk_bus = "virtio"
161
162    # generic/freebsd boxes need to be explicitly run with SCSI bus,
163    # otherwise boot process fails on mounting the disk
164    if (distro.include?("freebsd"))
165      libvirt.disk_bus = "scsi"
166    end
167
168    if not vmemulator.empty?
169      libvirt.emulator_path = "#{vmemulator}"
170      libvirt.machine_type = "pc"
171    end
172
173    # we put nvme_disk inside default pool to eliminate libvirt/SELinux Permissions Problems
174    # and to be able to run vagrant from user $HOME directory
175
176    # Loop to create all emulated disks set
177    emulated_nvme_types.each_with_index { |disk, index|
178      if ENV['NVME_FILE']
179        nvme_disk_id="#{disk}" + "-#{index}"
180        nvme_disk="#{nvme_file["#{index}".to_i]}"
181      else
182        nvme_disk="/var/lib/libvirt/images/nvme_disk.img"
183      end
184
185      unless File.exist? (nvme_disk)
186        puts "If run with libvirt provider please execute create_nvme_img.sh"
187      end
188
189      if disk == "nvme"
190        libvirt.qemuargs :value => "-drive"
191        libvirt.qemuargs :value => "format=raw,file=#{nvme_disk},if=none,id=#{nvme_disk_id}"
192        libvirt.qemuargs :value => "-device"
193        if nvme_namespaces["#{index}".to_i].nil? || nvme_namespaces["#{index}".to_i] == "1"
194          libvirt.qemuargs :value => "nvme,drive=#{nvme_disk_id},serial=1234#{index}"
195        else
196          libvirt.qemuargs :value => "nvme,drive=#{nvme_disk_id},serial=1234#{index},namespaces=#{nvme_namespaces["#{index}".to_i]}"
197        end
198      elsif disk == "ocssd"
199        libvirt.qemuargs :value => "-drive"
200        libvirt.qemuargs :value => "format=raw,file=#{nvme_disk},if=none,id=#{nvme_disk_id}"
201        libvirt.qemuargs :value => "-device"
202        # create ocssd drive with special parameters
203        # lba_index=4 it is LBA namespace format, 4 means that block size is 4K and have 64B metadata
204        # lnum_lun, lnum_pln, lpgs_per_blk, lsecs_per_pg, lblks_per_pln this are parameters describing the device geometry
205        # we need to multiply these parameters by ourselves to have backend file minimal size:
206        # in our case: 4K * 8 * 2 * 1536 * 2 * 45 = 8640 MB
207        libvirt.qemuargs :value => "nvme,drive=#{nvme_disk_id},serial=deadbeef,oacs=0,namespaces=1,lver=2,lba_index=4,mdts=10,lnum_lun=8,lnum_pln=2,lpgs_per_blk=1536,lsecs_per_pg=2,lblks_per_pln=45,metadata=#{nvme_disk}_ocssd_md,nsdatafile=#{nvme_disk}_ocssd_blknvme.ns,laer_thread_sleep=3000,stride=4"
208      end
209    }
210
211    libvirt.driver = "kvm"
212    libvirt.graphics_type = "vnc"
213    libvirt.memory = "#{vmram}"
214    libvirt.cpus = "#{vmcpu}"
215    libvirt.video_type = "cirrus"
216
217    if ENV['VAGRANT_HUGE_MEM'] == "1"
218      libvirt.memorybacking :hugepages
219    end
220
221    # Optional field if we want use other storage pools than default
222    # libvirt.storage_pool_name = "vm"
223  end
224
225  # rsync the spdk directory if provision hasn't happened yet
226  # Warning: rsync does not work with freebsd boxes, so this step is disabled
227  if ENV['COPY_SPDK_DIR'] == "1" && spdk_dir != "none"
228    config.vm.synced_folder "#{spdk_dir}", "/home/vagrant/spdk_repo/spdk", files_sync_backend
229  end
230
231  # rsync artifacts from build
232  if ENV['COPY_SPDK_ARTIFACTS'] == "1"
233    config.vm.synced_folder "#{vagrantfile_dir}/output", "/home/vagrant/spdk_repo/output", plugins_sync_backend
234  end
235
236  # provision the vm with all of the necessary spdk dependencies for running the autorun.sh tests
237  if ENV['DEPLOY_TEST_VM'] == "1" && spdk_dir != "none"
238    config.vm.provision "shell" do |setup|
239      setup.path = "#{spdk_dir}/test/common/config/vm_setup.sh"
240      setup.privileged = false
241      setup.args = ["-u", "-i"]
242    end
243  end
244
245  # Copy in the user's tools if they exists
246  if File.directory?(File.expand_path("~/vagrant_tools"))
247    config.vm.synced_folder "~/vagrant_tools", "/home/vagrant/tools", files_sync_backend
248  end
249end
250