xref: /spdk/scripts/vagrant/Vagrantfile (revision 2ef4855e83b1bdb8358b5575d83712d8f0a19fcd)
1# -*- mode: ruby -*-
2# vi: set ft=ruby :
3
4require 'open3'
5def get_box_type(distro)
6  spdk_distro = 'spdk/' + distro
7  localboxes, stderr, status = Open3.capture3("vagrant box list")
8  return spdk_distro if localboxes.include?(spdk_distro)
9
10  distro_to_type = {
11    'centos7'    => 'centos/7',
12    'centos8'    => 'centos/8',
13    'ubuntu1604' => 'peru/ubuntu-16.04-server-amd64',
14    'ubuntu1804' => 'peru/ubuntu-18.04-server-amd64',
15    'ubuntu2004' => 'peru/ubuntu-20.04-server-amd64',
16    'fedora31'   => 'generic/fedora31',
17    'fedora32'   => 'generic/fedora32',
18    'fedora33'   => 'generic/fedora33',
19    'arch'       => 'generic/arch',
20    'freebsd12'  => 'generic/freebsd12',
21  }
22  abort("Invalid argument! #{distro}") unless distro_to_type.key?(distro)
23
24  return distro_to_type[distro]
25end
26
27def setup_proxy(config,distro)
28  return unless ENV['http_proxy']
29
30  if Vagrant.has_plugin?("vagrant-proxyconf")
31    config.proxy.http     = ENV['http_proxy']
32    config.proxy.https    = ENV['https_proxy']
33    config.proxy.no_proxy = "localhost,127.0.0.1"
34  end
35
36  # Proxyconf does not seem to support FreeBSD boxes or at least it's
37  # docs do not mention that. Set up proxy configuration manually.
38  if distro.include?("freebsd")
39    $freebsd_proxy = <<-SCRIPT
40    sudo -s
41    echo "export http_proxy=#{ENV['http_proxy']}" >> /etc/profile
42    echo "export https_proxy=#{ENV['http_proxy']}" >> /etc/profile
43    echo "pkg_env: {http_proxy: #{ENV['http_proxy']}}" > /usr/local/etc/pkg.conf
44    chown root:wheel /usr/local/etc/pkg.conf
45    chmod 644 /usr/local/etc/pkg.conf
46    SCRIPT
47    config.vm.provision "shell", inline: $freebsd_proxy
48  end
49end
50
51def copy_gitconfig(config)
52  src_path = '~/.gitconfig'
53  return unless File.file?(File.expand_path(src_path))
54
55  config.vm.provision  "file", source: src_path, destination: ".gitconfig"
56end
57
58def copy_tsocks(config)
59  tsocks_file = 'tsocks.conf'
60  tsocks_file_path = '/etc/' + tsocks_file
61
62  return unless File.file?(tsocks_file_path)
63
64  $tsocks_copy_cmd = <<-SCRIPT
65  sudo -s
66  mv -f "#{tsocks_file}" "#{tsocks_file_path}"
67  chown root "#{tsocks_file_path}"
68  chmod 644 "#{tsocks_file_path}"
69  SCRIPT
70
71  config.vm.provision  "file", source: tsocks_file_path, destination: tsocks_file
72  config.vm.provision "shell", inline: $tsocks_copy_cmd
73end
74
75def copy_vagrant_tools(config,files_sync_backend)
76  src_path = '~/vagrant_tools'
77  return unless File.directory?(File.expand_path(src_path))
78
79  config.vm.synced_folder src_path, "/home/vagrant/tools", files_sync_backend
80end
81
82def copy_spdk_dir(config, files_sync_backend)
83  return unless ENV['COPY_SPDK_DIR'] == "1"
84  return unless ENV['SPDK_DIR']
85
86  config.vm.synced_folder ENV['SPDK_DIR'], '/home/vagrant/spdk_repo/spdk', files_sync_backend
87end
88
89def copy_spdk_artifacts(config, plugins_sync_backend)
90  return unless ENV['COPY_SPDK_ARTIFACTS'] == "1"
91
92  vagrantfile_dir=(ENV['VAGRANTFILE_DIR'] || "none")
93  config.vm.synced_folder "#{vagrantfile_dir}/output", "/home/vagrant/spdk_repo/output", plugins_sync_backend
94end
95
96def make_spdk_local_copy_of_nfs(config,distro)
97  user_group = 'vagrant:vagrant'
98
99  spdk_path = '/home/vagrant/spdk_repo/spdk'
100  spdk_tmp_path = '/tmp/spdk'
101  $spdk_repo_cmd = <<-SCRIPT
102  sudo -s
103  cp -R '#{spdk_path}' '#{spdk_tmp_path}'
104  umount '#{spdk_path}' && rm -rf '#{spdk_path}'
105  mv '#{spdk_tmp_path}' '#{spdk_path}'
106  chown -R #{user_group} '#{spdk_path}'
107  SCRIPT
108
109  config.vm.provision "shell", inline: $spdk_repo_cmd
110end
111
112def get_nvme_disk(disk, index)
113  if ENV['NVME_FILE']
114    nvme_file = ENV['NVME_FILE'].split(',')
115    nvme_disk = nvme_file[index]
116  else
117    nvme_disk = '/var/lib/libvirt/images/nvme_disk.img'
118  end
119
120  unless File.exist? (nvme_disk)
121    puts 'If run with libvirt provider please execute create_nvme_img.sh'
122  end
123
124  return nvme_disk
125end
126
127def setup_nvme_disk(libvirt, disk, index)
128  nvme_disk_id = disk + '-' + index.to_s
129  nvme_disk = get_nvme_disk(disk, index)
130
131  nvme_namespaces=(ENV['NVME_DISKS_NAMESPACES'] || "").split(',')
132  nvme_cmbs=(ENV['NVME_CMB'] || "").split(',')
133  nvme_pmrs=(ENV['NVME_PMR'] || "").split(',')
134
135  libvirt.qemuargs :value => "-drive"
136  libvirt.qemuargs :value => "format=raw,file=#{nvme_disk},if=none,id=#{nvme_disk_id}"
137  libvirt.qemuargs :value => "-device"
138  nvme_drive = "nvme,drive=#{nvme_disk_id},serial=1234#{index}"
139  if !nvme_namespaces[index].nil? && nvme_namespaces[index] != "1"
140    nvme_drive << ",namespaces=#{nvme_namespaces[index]}"
141  end
142
143  pmr_cmdline = ""
144
145  if !nvme_cmbs[index].nil? && nvme_cmbs[index] == "true"
146    # Fix the size of the buffer to 128M
147    nvme_drive << ",cmb_size_mb=128"
148  end
149  if !nvme_pmrs[index].nil?
150    pmr_path, pmr_size = nvme_pmrs[index].split(':')
151    if !File.exist?(pmr_path)
152      abort("#{pmr_path} does not exist, aborting")
153    end
154    if pmr_size.nil?
155      pmr_size = "16M"
156    end
157    nvme_drive << ",pmrdev=pmr#{index}"
158    pmr_cmdline = "memory-backend-file,id=pmr#{index},share=on,mem-path=#{pmr_path},size=#{pmr_size}"
159  end
160  libvirt.qemuargs :value => nvme_drive
161  if pmr_cmdline != ""
162    libvirt.qemuargs :value => "-object"
163    libvirt.qemuargs :value => pmr_cmdline
164  end
165end
166
167def setup_ocssd_disk(libvirt, disk, index)
168  nvme_disk_id = disk + '-' + index.to_s
169  nvme_disk = get_nvme_disk(disk, index)
170
171  libvirt.qemuargs :value => "-drive"
172  libvirt.qemuargs :value => "format=raw,file=#{nvme_disk},if=none,id=#{nvme_disk_id}"
173  libvirt.qemuargs :value => "-device"
174  # create ocssd drive with special parameters
175  # lba_index=4 it is LBA namespace format, 4 means that block size is 4K and have 64B metadata
176  # lnum_lun, lnum_pln, lpgs_per_blk, lsecs_per_pg, lblks_per_pln this are parameters describing the device geometry
177  # we need to multiply these parameters by ourselves to have backend file minimal size:
178  # in our case: 4K * 8 * 2 * 1536 * 2 * 45 = 8640 MB
179  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"
180end
181
182def setup_ssh(config)
183  config.ssh.forward_agent = true
184  config.ssh.forward_x11 = true
185  if ENV['VAGRANT_PASSWORD_AUTH'] == "1"
186    config.ssh.username = "vagrant"
187    config.ssh.password = "vagrant"
188    config.ssh.private_key_path = nil
189  end
190end
191
192def deploy_test_vm(config, distro, plugins_sync_backend)
193  return unless ENV['DEPLOY_TEST_VM'] == "1"
194  return unless ENV['COPY_SPDK_DIR'] == "1"
195  return unless ENV['SPDK_DIR']
196
197  # use http proxy if avaiable
198  setup_proxy(config, distro)
199
200  # Copy the tsocks configuration file for use when installing some spdk test pool dependencies
201  copy_tsocks(config)
202
203  # freebsd boxes in order to have spdk sources synced from
204  # host properly will use NFS with "ro" option enabled to prevent changes
205  # on host filesystem.
206  # To make sources usable in the guest VM we need to unmount them and use
207  # local copy.
208  make_spdk_local_copy_of_nfs(config,distro) if plugins_sync_backend[:type] == :nfs
209
210  config.vm.provision "shell" do |setup|
211    setup.inline = "/home/vagrant/spdk_repo/spdk/test/common/config/vm_setup.sh"
212    setup.privileged = false
213    setup.args = ["-u", "-i"]
214  end
215end
216
217def setup_virtualbox(config, vmcpu, vmram)
218  config.vm.provider "virtualbox" do |vb|
219    vb.customize ["modifyvm", :id, "--ioapic", "on"]
220    vb.memory = vmram
221    vb.cpus = vmcpu
222
223    nvme_disk=(ENV['NVME_FILE'] || "nvme_disk.img")
224    unless File.exist? (nvme_disk)
225      vb.customize ["createhd", "--filename", nvme_disk, "--variant", "Fixed", "--size", "1024"]
226      vb.customize ["storagectl", :id, "--name", "nvme", "--add", "pcie", "--controller", "NVMe", "--portcount", "1", "--bootable", "off"]
227      vb.customize ["storageattach", :id, "--storagectl", "nvme", "--type", "hdd", "--medium", nvme_disk, "--port", "0"]
228    end
229
230    #support for the SSE4.x instruction is required in some versions of VB.
231    vb.customize ["setextradata", :id, "VBoxInternal/CPUM/SSE4.1", "1"]
232    vb.customize ["setextradata", :id, "VBoxInternal/CPUM/SSE4.2", "1"]
233  end
234end
235
236def setup_libvirt(config, vmcpu, vmram, distro)
237  emulated_nvme_types=(ENV['NVME_DISKS_TYPE'] || "nvme").split(',')
238
239  config.vm.provider "libvirt" do |libvirt, override|
240    libvirt.random_hostname = "1"
241    libvirt.driver = "kvm"
242    libvirt.graphics_type = "vnc"
243    libvirt.memory = vmram
244    libvirt.cpus = vmcpu
245    libvirt.video_type = "cirrus"
246
247    if (distro.include?("freebsd"))
248      # generic/freebsd boxes need to be explicitly run with SCSI bus,
249      # otherwise boot process fails on mounting the disk
250      libvirt.disk_bus = "scsi"
251    elsif (distro.include?("arch"))
252      # Run generic/arch boxes explicitly with IDE bus,
253      # otherwise boot process fails on mounting the disk
254      libvirt.disk_bus = "ide"
255    else
256      libvirt.disk_bus = "virtio"
257    end
258
259    if ENV['SPDK_QEMU_EMULATOR']
260      libvirt.emulator_path = ENV['SPDK_QEMU_EMULATOR']
261      libvirt.machine_type = "pc"
262    end
263
264    # we put nvme_disk inside default pool to eliminate libvirt/SELinux Permissions Problems
265    # and to be able to run vagrant from user $HOME directory
266
267    # Loop to create all emulated disks set
268    emulated_nvme_types.each_with_index { |disk, index|
269      if disk == "nvme"
270        setup_nvme_disk(libvirt, disk, index)
271      elsif disk == "ocssd"
272        setup_ocssd_disk(libvirt, disk, index)
273      end
274    }
275
276    # Add network interface for openstack tests
277    if ENV['SPDK_OPENSTACK_NETWORK'] == "1"
278      libvirt.qemuargs :value => "-device"
279      libvirt.qemuargs :value => "virtio-net,netdev=openstack.0"
280      libvirt.qemuargs :value => "-netdev"
281      libvirt.qemuargs :value => "user,id=openstack.0"
282    end
283
284    if ENV['VAGRANT_HUGE_MEM'] == "1"
285      libvirt.memorybacking :hugepages
286    end
287
288    # Optional field if we want use other storage pools than default
289    # libvirt.storage_pool_name = "vm"
290  end
291end
292
293#################################################################################################
294# Pick the right distro and bootstrap, default is fedora31
295distro = (ENV['SPDK_VAGRANT_DISTRO'] || "fedora31")
296provider = (ENV['SPDK_VAGRANT_PROVIDER'] || "virtualbox")
297
298# Get all variables for creating vm
299vmcpu = (ENV['SPDK_VAGRANT_VMCPU'] || 2)
300vmram = (ENV['SPDK_VAGRANT_VMRAM'] || 4096)
301
302# generic/freebsd boxes do not work properly with vagrant-proxyconf and
303# have issues installing rsync and sshfs for syncing files. NFS is
304# pre-installed, so use it.
305# generic/fedora boxes on the other hand have problems running NFS
306# service so use sshfs+rsync combo instead.
307if (get_box_type(distro).include?("generic/freebsd"))
308  files_sync_backend = {type: :nfs, nfs_udp: false,  mount_options: ['ro']}
309  plugins_sync_backend = {type: :nfs, nfs_udp: false}
310else
311  # Remove --copy-links from default rsync cmdline since we do want to sync
312  # actual symlinks as well. Also, since copy is made between host and its
313  # local VM we don't need to worry about saturating the local link so skip
314  # the compression to speed up the whole transfer.
315  files_sync_backend = {type: "rsync", rsync__auto: false, rsync__args: ["--archive", "--verbose", "--delete"]}
316  plugins_sync_backend = {type: :sshfs}
317end
318
319Vagrant.configure(2) do |config|
320  config.vm.box = get_box_type(distro)
321  config.vm.box_check_update = false
322  config.vm.synced_folder '.', '/vagrant', disabled: true
323
324  # Copy in the .gitconfig if it exists
325  copy_gitconfig(config)
326
327  # Copy in the user's tools if they exists
328  copy_vagrant_tools(config,files_sync_backend)
329
330  # rsync the spdk directory if provision hasn't happened yet
331  # Warning: rsync does not work with freebsd boxes, so this step is disabled
332  copy_spdk_dir(config, files_sync_backend)
333
334  # rsync artifacts from build
335  copy_spdk_artifacts(config, plugins_sync_backend)
336
337  # Setup SSH
338  setup_ssh(config)
339
340  # Virtualbox configuration
341  setup_virtualbox(config,vmcpu,vmram)
342
343  # This setup was Tested on Fedora 27
344  # libvirt configuration need modern Qemu(tested on 2.10) & vagrant-libvirt in version 0.0.39+
345  # There are few limitation for SElinux - The file added outside libvirt must have proper SE ACL policy or setenforce 0
346  setup_libvirt(config,vmcpu,vmram,distro)
347
348  # provision the vm with all of the necessary spdk dependencies for running the autorun.sh tests
349  deploy_test_vm(config, distro, plugins_sync_backend)
350end
351