-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVagrantfile
155 lines (142 loc) · 6.67 KB
/
Vagrantfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
CONFIG_BUILDER_IP = '10.10.0.2'
CONFIG_BUILDER_DHCP_RANGE = '10.10.0.100,10.10.0.200,10m'
# do not connect the builder to a external network tru the given bridge.
CONFIG_BUILDER_EXTERNAL_IP = nil
CONFIG_BUILDER_BRIDGE_NAME = nil
CONFIG_BUILDER_EXTERNAL_DHCP_RANGE = nil
# connect the builder to a external network tru the given bridge.
# NB comment this block when not required.
CONFIG_BUILDER_BRIDGE_NAME = 'br-rpi'
CONFIG_BUILDER_EXTERNAL_IP = '10.3.0.2'
CONFIG_BUILDER_EXTERNAL_DHCP_RANGE = '10.3.0.100,10.3.0.200,10m'
# this environment libvirt network name.
CONFIG_NETWORK_NAME = "#{File.basename(File.dirname(__FILE__))}0"
# to make sure the vms are created sequentially, we
# have to force a --no-parallel execution.
ENV['VAGRANT_NO_PARALLEL'] = 'yes'
# enable typed triggers.
# NB this is needed to modify the libvirt domain scsi controller model to virtio-scsi.
ENV['VAGRANT_EXPERIMENTAL'] = 'typed_triggers'
require 'open3'
def virtual_machines
return [] unless File.exists? 'shared/machines.json'
machines = JSON.load(File.read('shared/machines.json')).select{|m| m['type'] == 'virtual'}
machines.each_with_index.map do |m, i|
(firmware, boot) = m['name'].split('-')
[m['name'], firmware, boot, m['ip'], m['mac']]
end
end
Vagrant.configure('2') do |config|
config.vm.box = 'ubuntu-20.04-amd64'
config.vm.provider :libvirt do |lv, config|
lv.cpus = 2
#lv.cpu_mode = 'host-passthrough'
#lv.nested = true
lv.memory = 2*1024
lv.keymap = 'pt'
config.vm.synced_folder '.', '/vagrant', type: 'nfs', nfs_version: '4.2', nfs_udp: false
end
config.vm.define :builder do |config|
config.vm.provider :libvirt do |lv|
lv.cpus = 4
lv.memory = 4*1024
end
config.vm.hostname = 'builder.test'
config.vm.network :private_network,
ip: CONFIG_BUILDER_IP,
libvirt__dhcp_enabled: false,
libvirt__forward_mode: 'none'
if CONFIG_BUILDER_BRIDGE_NAME
config.vm.network :public_network,
ip: CONFIG_BUILDER_EXTERNAL_IP,
dev: CONFIG_BUILDER_BRIDGE_NAME,
mode: 'bridge',
type: 'bridge'
end
config.vm.provision :shell, path: 'provision-base.sh'
config.vm.provision :shell, path: 'provision-iptables.sh'
config.vm.provision :shell, path: 'provision-docker.sh'
config.vm.provision :shell, path: 'provision-meshcommander.sh'
config.vm.provision :shell, path: 'provision-loki.sh'
config.vm.provision :shell, path: 'provision-grafana.sh', args: [CONFIG_BUILDER_IP]
config.vm.provision :shell, path: 'provision-pxe-server.sh', args: [CONFIG_BUILDER_IP, CONFIG_BUILDER_DHCP_RANGE, CONFIG_BUILDER_EXTERNAL_IP, CONFIG_BUILDER_EXTERNAL_DHCP_RANGE]
config.vm.provision :shell, path: 'provision-ipxe.sh', args: [CONFIG_BUILDER_IP]
config.vm.provision :shell, path: 'provision-machinator.sh'
config.vm.provision :shell, path: 'provision-linuxkit.sh', args: [CONFIG_BUILDER_IP]
config.vm.provision :shell, path: 'summary.sh', run: 'always'
end
virtual_machines.each do |name, firmware, boot, ip, mac|
config.vm.define name do |config|
config.ssh.username = 'root'
config.ssh.sudo_command = '%c'
config.vm.guest = 'alpine' # LinuxKit is Alpine based.
config.vm.box = 'empty'
config.vm.provider :libvirt do |lv, config|
config.vm.box = nil
lv.loader = '/usr/share/ovmf/OVMF.fd' if firmware == 'uefi'
lv.storage :file, :size => '30G', :device => 'sda', :bus => 'scsi', :discard => 'unmap', :cache => 'unsafe'
if boot == 'iso'
lv.storage :file, :device => :cdrom, :path => "#{Dir.pwd}/shared/linuxkit-example#{firmware == 'bios' && '' || '-'+firmware}.iso"
lv.boot 'cdrom'
else
lv.boot 'network'
end
lv.management_network_name = CONFIG_NETWORK_NAME
lv.management_network_mac = mac
lv.management_network_address = "#{CONFIG_BUILDER_IP}/24"
lv.graphics_type = 'spice'
lv.video_type = 'virtio'
# set some BIOS settings that will help us identify this particular machine.
#
# QEMU | Linux
# --------------------+----------------------------------------------
# type=1,manufacturer | /sys/devices/virtual/dmi/id/sys_vendor
# type=1,product | /sys/devices/virtual/dmi/id/product_name
# type=1,version | /sys/devices/virtual/dmi/id/product_version
# type=1,serial | /sys/devices/virtual/dmi/id/product_serial
# type=1,sku | dmidecode
# type=1,uuid | /sys/devices/virtual/dmi/id/product_uuid
# type=3,manufacturer | /sys/devices/virtual/dmi/id/chassis_vendor
# type=3,family | /sys/devices/virtual/dmi/id/chassis_type
# type=3,version | /sys/devices/virtual/dmi/id/chassis_version
# type=3,serial | /sys/devices/virtual/dmi/id/chassis_serial
# type=3,asset | /sys/devices/virtual/dmi/id/chassis_asset_tag
[
'type=1,manufacturer=your vendor name here',
'type=1,product=your product name here',
'type=1,version=your product version here',
'type=1,serial=your product serial number here',
'type=1,sku=your product SKU here',
"type=1,uuid=00000000-0000-4000-8000-#{mac.tr(':', '')}",
'type=3,manufacturer=your chassis vendor name here',
#'type=3,family=1', # TODO why this does not work on qemu from ubuntu 18.04?
'type=3,version=your chassis version here',
'type=3,serial=your chassis serial number here',
"type=3,asset=your chassis asset tag here #{name}",
].each do |value|
lv.qemuargs :value => '-smbios'
lv.qemuargs :value => value
end
config.vm.synced_folder '.', '/vagrant', disabled: true
config.trigger.before :'VagrantPlugins::ProviderLibvirt::Action::StartDomain', type: :action do |trigger|
trigger.ruby do |env, machine|
# modify the scsi controller model to virtio-scsi.
# see https://github.com/vagrant-libvirt/vagrant-libvirt/pull/692
# see https://github.com/vagrant-libvirt/vagrant-libvirt/issues/999
stdout, stderr, status = Open3.capture3(
'virt-xml', machine.id,
'--edit', 'type=scsi',
'--controller', 'model=virtio-scsi')
if status.exitstatus != 0
raise "failed to run virt-xml to modify the scsi controller model. status=#{status.exitstatus} stdout=#{stdout} stderr=#{stderr}"
end
end
end
end
end
end
config.trigger.before :up do |trigger|
trigger.only_on = ['builder']
trigger.run = {path:'./vagrant-trigger-before-up.sh'}
end
end