-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtualbox-dnsmasq.sh
executable file
·246 lines (202 loc) · 5.44 KB
/
virtualbox-dnsmasq.sh
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/bash
# https://ramonnogueira.wordpress.com/2013/03/29/simple-way-to-run-dnsmasq-for-virtualbox-guest-dhcp/
set -euo pipefail
export VIRTUALBOX_FOLDER_PATH="./.virtualbox"
export DNSMASQ_FOLDER_PATH="./.dnsmasq"
archlive::build()
{
cd "./archlive"
sudo rm -f -v work/build.make_*
sudo ./build.sh -v
cd -
}
ipxe::make_iso()
{
rm "./ipxe-efi.iso" || true
cd "./ipxe/src"
make clean
rm -Rf "./bin" || true
make \
"bin-x86_64-efi/ipxe.efi" \
EMBED="../../arch.ipxe" \
DEBUG="script"
mkdir -p "efi_tmp/EFI/BOOT" || true
cp "bin-x86_64-efi/ipxe.efi" "efi_tmp/EFI/BOOT/bootx64.efi"
genisoimage -o "../../ipxe-efi.iso" "./efi_tmp"
cd "../.."
test -f "./ipxe-efi.iso"
}
virtualbox::init()
{
mkdir -p "${VIRTUALBOX_FOLDER_PATH}" || true
mkdir -p "${VIRTUALBOX_FOLDER_PATH}/machines" || true
(
export VBOX_USER_HOME="${VIRTUALBOX_FOLDER_PATH}"
VBoxManage setproperty \
"machinefolder" "$( readlink -f "${VIRTUALBOX_FOLDER_PATH}" )/machines"
VBoxManage setextradata global \
"GUI/Input/HostKeyCombination" "65377"
)
}
virtualbox::hard_drive::create()
{
declare vdi_file_path="${1}"
declare vdi_file_size_in_bytes="${2}"
(
export VBOX_USER_HOME="${VIRTUALBOX_FOLDER_PATH}"
mkdir -p "$( dirname "${vdi_file_path}" )" || true
VBoxManage createmedium \
disk \
--filename="${vdi_file_path}" \
--sizebyte="${vdi_file_size_in_bytes}"
)
}
virtualbox::vm::create()
{
declare vm_name="${1}"
declare vm_ram_size_in_bytes="${2}"
declare vdi_file_path="${3}"
declare iso_file_path="${4}"
(
export VBOX_USER_HOME="${VIRTUALBOX_FOLDER_PATH}"
VBoxManage createvm \
--name "${vm_name}" \
--ostype="ArchLinux_64" \
--register
VBoxManage modifyvm \
"${vm_name}" \
--memory="$(( ${vm_ram_size_in_bytes} / 1024 / 1024 ))" \
--acpi="on" \
--boot1="dvd" \
--boot2="disk" \
--boot3="none" \
--firmware="efi" \
--vram="256"
VBoxManage storagectl "${vm_name}" \
--name="SATA Controller" \
--add="sata" \
--portcount=2
VBoxManage storageattach "${vm_name}" \
--storagectl="SATA Controller" \
--port=0 \
--device=0 \
--type="hdd" \
--medium="${vdi_file_path}"
VBoxManage storageattach "${vm_name}" \
--storagectl="SATA Controller" \
--port=1 \
--device=0 \
--type="dvddrive" \
--medium="${iso_file_path}"
VBoxManage modifyvm "${vm_name}" \
--nic1="hostonly" \
--hostonlyadapter1="vboxnet0"
)
}
virtualbox::ui::start()
{
(
export VBOX_USER_HOME="${VIRTUALBOX_FOLDER_PATH}"
VirtualBox
)
}
virtualbox::start_vm()
{
:
}
dnsmasq::init()
{
mkdir -p "${DNSMASQ_FOLDER_PATH}" || true
}
dnsmasq::start()
{
declare dnsmasq_iface="${1}"
declare dnsmasq_dhcp_range="${2}"
declare dnsmasq_domain="${3}"
declare dnsmasq_host_name="${4}"
declare dnsmasq_host_ip="${5}"
{
sudo resolvectl dns "${dnsmasq_iface}" "${dnsmasq_host_ip}"
sudo dnsmasq \
--conf-file="/dev/null" \
--except-interface="lo0" \
--interface="${dnsmasq_iface}" \
--bind-interfaces \
--dhcp-range="${dnsmasq_dhcp_range}" \
--dhcp-leasefile="${DNSMASQ_FOLDER_PATH}/dnsmasq-leasefile.${dnsmasq_domain}" \
--local="/${dnsmasq_domain}/" \
--expand-hosts \
--domain="${dnsmasq_domain}" \
--address="/${dnsmasq_host_name}.${dnsmasq_domain}/${dnsmasq_host_ip}" \
--log-queries \
--log-facility=- \
--user="$( id -un )"
sudo resolvectl revert "${dnsmasq_iface}"
} &
}
http_server::start()
{
{
sudo darkhttpd "./srv" --port "80"
} &
}
http_server::stop()
{
sudo pkill "darkhttpd" || true
}
dnsmasq::stop()
{
#FIXME We need to use the good PID
pkill "dnsmasq" || true
}
main()
{
rm -Rf "${VIRTUALBOX_FOLDER_PATH}"
archlive::build
#ipxe::make_iso
dnsmasq::init
virtualbox::init
declare iface="vboxnet0"
declare network_prefix="192.168.56"
declare dnsmasq_lease_time="10m"
declare dnsmasq_domain="virtualbox"
declare netmask="255.255.255.0"
declare host_name="virtualbox"
declare host_ip="${network_prefix}.1"
declare dnsmasq_dhcp_range="${network_prefix}.102,${network_prefix}.200,${dnsmasq_lease_time}"
VBOX_USER_HOME=${VIRTUALBOX_FOLDER_PATH} \
VBoxManage dhcpserver \
remove \
--ifname "${iface}"
VBOX_USER_HOME=${VIRTUALBOX_FOLDER_PATH} \
VBoxManage hostonlyif \
ipconfig "${iface}" \
--ip "${host_ip}" \
--netmask "${netmask}"
mkdir -p "./srv/arch/x86_64" || true
cp \
"./archlive/work/iso/arch/x86_64/airootfs.sfs" \
"./srv/arch/x86_64/airootfs.sfs"
cp \
"./archlive/work/iso/arch/boot/x86_64/vmlinuz" \
"./srv/vmlinuz"
cp \
"./archlive/work/iso/arch/boot/x86_64/archiso.img" \
"./srv/archiso.img"
cp \
"./archlive/work/iso/arch/boot/intel_ucode.img" \
"./srv/intel_ucode.img"
http_server::start
dnsmasq::start "${iface}" "${dnsmasq_dhcp_range}" "${dnsmasq_domain}" "${host_name}" "${host_ip}"
declare vm_name="archlinux"
declare vm_ram_size_in_bytes=$(( 2 * 1024 * 1024 * 1024 ))
declare vdi_file_path="${VIRTUALBOX_FOLDER_PATH}/machines/${vm_name}/drive.vdi"
declare vdi_file_size=$(( 2 * 1024 * 1024 * 1024 ))
declare iso_file_path="./ipxe-efi.iso"
virtualbox::hard_drive::create "${vdi_file_path}" "${vdi_file_size}"
virtualbox::vm::create "${vm_name}" "${vm_ram_size_in_bytes}" "${vdi_file_path}" "${iso_file_path}"
virtualbox::ui::start
dnsmasq::stop
http_server::stop
}
main "${@}"