Projekt

Allgemein

Profil

Setup virtualboxhost60 » Historie » Version 5

Jeremias Keihsler, 14.01.2019 11:16

1 1 Jeremias Keihsler
h1. Install Procedure for VirtualBox 6.0
2
3
h2. Requirements
4
5
To install VirtualBox you will need the following:
6
* a installed and supported operating system (e.g. CentOS 7.x)
7
* root-access
8
* a fast internet connection
9
* [[repo_epel|EPEL]]
10
* [[repo_virtualbox|VirtualBox - Repo]]
11
12
h2. Preliminary Note
13
14
This procedure is based on a documentation supplied by www.proclos.com.
15
16
You may also find additional information at
17
* http://wiki.centos.org/HowTos/Virtualization/VirtualBox/
18 3 Jeremias Keihsler
* https://www.if-not-true-then-false.com/2010/install-virtualbox-with-yum-on-fedora-centos-red-hat-rhel/
19 1 Jeremias Keihsler
20
a possible solution to get the autostart-service up and running finally  is found here:
21
http://nathangiesbrecht.com/centos-7-virtualbox-vboxautostart-service-setup
22
23
h2. Install Virtual Box
24
25
h3. Install DKMS
26
27
Install Dynamic Kernel Module System: (automatically installs appropriate kernel modules per
28
selected version)
29
<pre><code class="bash">
30 4 Jeremias Keihsler
yum install binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-devel dkms
31 1 Jeremias Keihsler
</code></pre>
32
33
h3. Install VirtualBox
34
35
<pre><code class="bash">
36
yum install VirtualBox-6.0
37
</code></pre>
38 5 Jeremias Keihsler
39
Rebuild kernel modules with following command:
40
<pre><code class="bash">
41
/usr/lib/virtualbox/vboxdrv.sh setup
42
</code></pre>
43
44
45 1 Jeremias Keihsler
Start virtual box. @GNOME-Menu->Applications->System Tools->Oracle VM VirtualBox@
46 5 Jeremias Keihsler
47 1 Jeremias Keihsler
48
h2. Install VirtualBox-Extension
49
50
Download the extension from the virtualbox homepage www.virtualbox.org.
51
Add the extension (VirtualBox 6.0.x Oracle VM VirtualBox Extension Pack) using @Preferences->Extensions->Add@. This extension is needed to attach usb devices to the guest machines and to be
52
able to connect to guest machines using WindowsRemote Desktop-Client, which is very handy, if you need graphical access.
53
54
@VBextpack_update.sh@
55
<pre>
56
#!/bin/bash
57
version=$(vboxmanage -v)
58
echo $version
59
var1=$(echo $version | cut -d 'r' -f 1)
60
echo $var1
61
var2=$(echo $version | cut -d 'r' -f 2)
62
echo $var2
63
file="Oracle_VM_VirtualBox_Extension_Pack-$var1-$var2.vbox-extpack"
64
echo $file
65
wget http://download.virtualbox.org/virtualbox/$var1/$file -O /tmp/$file
66
VBoxManage extpack uninstall "Oracle VM VirtualBox Extension Pack"
67
VBoxManage extpack install /tmp/$file --replace
68
</pre>
69
70
h2. Setup vboxusers
71
72
The Linux installers create the system user group vboxusers during installation. Any system user who is going to use USB devices from VirtualBox guests must be a member of that group. A user can be made a member of the group vboxusers through the GUI user/group management or at the command line with
73
74
<pre><code class="bash">
75
usermod -a -G vboxusers username
76
</code></pre>
77
78
Note that adding an active user to that group will require that user to log out and back in again. This should be done manually after successful installation of the package.
79
80
h2. Automatic Start/Stop of virtual machines systemd-style
81
82
83
this is taken from http://www.ericerfanian.com/automatically-starting-virtualbox-vms-on-archlinux-using-systemd/
84
85
Add a file called @vboxvmservice@.service@ in @/etc/systemd/system/.@
86
87 3 Jeremias Keihsler
<pre>
88 1 Jeremias Keihsler
[Unit]
89
Description=VBox Virtual Machine %i Service
90
Requires=systemd-modules-load.service
91
After=systemd-modules-load.service
92
93
[Service]
94
User=user
95
Group=vboxusers
96
ExecStart=/usr/bin/VBoxHeadless -s %i
97
ExecStop=/usr/bin/VBoxManage controlvm %i savestate
98
99
[Install]
100
WantedBy=multi-user.target
101
</pre>
102
103
Replace the user after user= with the username you want to run the VMs. Make sure that the user is in the vboxusers group. If you need to delay the start of the VMs until after your network is started or a network share is mounted, locate the service you want to wait for using ‘systemctl’ and then substitute the service, mount, or network behind ‘After=’.
104
105
You can also replace ‘savestate’ in ‘ExecStop=’ with ‘poweroff’ or ‘acpipowerbutton’ to hard-stop the VM, or ask for a clean shut-down, respectively.
106
107
To enable one or more VMs at boot, enter:
108
109
<pre><code class="bash">
110
systemctl enable vboxvmservice@vm_name.service
111
</code></pre>
112
h2. 5 Automatic Start/Stop of virtual machines SystemV-style
113
114
115
As an alternative to the following solution, someone might find https://sourceforge.net/projects/vboxtool/ interesting to investigate.
116
117
To automatically run the guest machines in headless mode on host machine startup, do the following((A new feature of VirtualBox 4.2 is a supplied init-script to automatically start/stop virtual-machines. This I've not tested yet. The old 4.1 way is still working fine. So this section remains as 4.1 until something better is tested.
118
)):
119
120
add the list of names of the virtual machines that should be started / stopped automatically in following files:
121
@/etc/virtualbox/machines_enabled_start@ and @/etc/virtualbox/machines_enabled_stop@
122
123
Each virtual machine name has to in a separate line. You can create and fill the files by the use of @vim@ or any other text-editor.
124
<pre><code class="bash">
125
cd /etc
126
mkdir virtualbox
127
vim /etc/virtualbox/machines_enabled_start
128
</code></pre>
129
<pre><code class="bash">
130
vim /etc/virtualbox/machines_enabled_stop
131
</code></pre>
132
133
Afterwards you can check the file-content by
134
<pre><code class="bash">
135
cat /etc/virtualbox/machines_enabled_start
136
</code></pre>
137
the content should look like:
138
<pre><code class="bash">
139
firewall-pfsense
140
clusterOne
141
clusterTwo
142
postgresql-solo
143
windows7-janus
144
</code></pre>
145
As the machines are started and stopped in the same order as in the files being listed you maybe want to stop the machines in reverse order.
146
<pre><code class="bash">
147
cat /etc/virtualbox/machines_enabled_stop
148
</code></pre>
149
the content could look like:
150
<pre><code class="bash">
151
windows7-janus
152
postgresql-solo
153
clusterTwo
154
clusterOne
155
firewall-pfsense
156
</code></pre>
157
158
Also create a file @/etc/init.d/vboxcontrol@ with following content:
159
160
Don't forget to set the correct user @VM_USER@
161
<pre>
162
#! /bin/sh
163
# vboxcontrol   Startup script for VirtualBox Virtual Machines
164
#
165
# chkconfig: 345 98 02
166
# description: Manages VirtualBox VMs
167
# processname: vboxcontrol
168
#
169
# pidfile: /var/run/vboxcontrol/vboxcontrol.pid
170
#
171
### BEGIN INIT INFO
172
#
173
### END INIT INFO
174
#
175
# Version 20110509 by Jeremias Keihsler based on:
176
# Version 20090301 by Kevin Swanson <kswan.info> based on:
177
# Version 2008051100 by Jochem Kossen <jochem.kossen@gmail.com>
178
# http://farfewertoes.com
179
#
180
# Released in the public domain
181
#
182
# This file came with a README file containing the instructions on how
183
# to use this script.
184
#
185
186
# Source function library.
187
if [ -f /etc/init.d/functions ] ; then
188
. /etc/init.d/functions
189
elif [ -f /etc/rc.d/init.d/functions ] ; then
190
. /etc/rc.d/init.d/functions
191
else
192
exit 1
193
fi
194
195
################################################################################
196
# INITIAL CONFIGURATION
197
VBOXDIR="/etc/virtualbox"
198
VM_USER="OMB"
199
USE_NAT="no"
200
201
export PATH="${PATH:+$PATH:}/bin:/usr/bin:/usr/sbin:/sbin"
202
203
if [ -f $VBOXDIR/config ]; then
204
. $VBOXDIR/config
205
fi
206
207
SU="su $VM_USER -c"
208
VBOXMANAGE="VBoxManage -nologo"
209
210
################################################################################
211
# FUNCTIONS
212
213
# Determine if USE_NAT is set to "yes"
214
use_nat() {
215
if [ "$USE_NAT" = "yes" ]; then
216
return `true`
217
else
218
return `false`
219
fi
220
}
221
222
log_failure_msg() {
223
echo $1
224
}
225
226
log_action_msg() {
227
echo $1
228
}
229
230
# Check for running machines every few seconds; return when all machines are
231
# down
232
wait_for_closing_machines() {
233
RUNNING_MACHINES=`$SU "$VBOXMANAGE list runningvms" | wc -l`
234
if [ $RUNNING_MACHINES != 0 ]; then
235
sleep 2
236
wait_for_closing_machines
237
fi
238
}
239
240
################################################################################
241
# RUN
242
case "$1" in
243
start)
244
if [ -f /etc/virtualbox/machines_enabled_start ]; then
245
246
cat /etc/virtualbox/machines_enabled_start | while read VM; do
247
log_action_msg "Starting VM: $VM ..."
248
$SU "$VBOXMANAGE startvm "$VM" --type headless"
249
sleep 20
250
RETVAL=$?
251
done
252
touch /var/lock/subsys/vboxcontrol
253
fi
254
;;
255
stop)
256
# NOTE: this stops all running VM's. Not just the ones listed in the
257
# config
258
cat /etc/virtualbox/machines_enabled_stop  | while read VM; do
259
log_action_msg "Shutting down VM: $VM ..."
260
$SU "$VBOXMANAGE controlvm "$VM" acpipowerbutton"
261
sleep 10
262
done
263
rm -f /var/lock/subsys/vboxcontrol
264
wait_for_closing_machines
265
;;
266
export)
267
# NOTE: this stops and exports the listed VMs
268
cat /etc/virtualbox/machines_enabled_export  | while read VM; do
269
  log_action_msg "Shutting down VM: $VM ..."
270
  $SU "$VBOXMANAGE controlvm "$VM" acpipowerbutton"
271
  /bin/echo -en "\a" > /dev/console
272
  sleep 10
273
done
274
wait_for_closing_machines
275
JKE_DATE=$(date +%F)
276
cat /etc/virtualbox/machines_enabled_export  | while read VM; do
277
  log_action_msg "Exporting VM: $VM ..."
278
  $SU "$VBOXMANAGE export "$VM" -o "$VM"_"$JKE_DATE"."ova" --manifest --vsys 0 --version $JKE_DATE"
279
done
280
rm -f /var/lock/subsys/vboxcontrol
281
;;
282
start-vm)
283
log_action_msg "Starting VM: $2 ..."
284
$SU "$VBOXMANAGE startvm "$2" --type headless"
285
;;
286
stop-vm)
287
log_action_msg "Stopping VM: $2 ..."
288
$SU "$VBOXMANAGE controlvm "$2" acpipowerbutton"
289
;;
290
poweroff-vm)
291
log_action_msg "Powering off VM: $2 ..."
292
$SU "$VBOXMANAGE controlvm "$2" poweroff"
293
;;
294
export-vm)
295
# NOTE: this exports the given VM
296
log_action_msg "Exporting VM: $2 ..."
297
JKE_DATE=$(date +%F)
298
$SU "$VBOXMANAGE export "$2" -o "$2"_"$JKE_DATE"."ova" --manifest --vsys 0 --version $JKE_DATE"
299
;;
300
status)
301
echo "The following virtual machines are currently running:"
302
$SU "$VBOXMANAGE list runningvms" | while read VM; do
303
echo -n "$VM ("
304
echo -n `$SU "VBoxManage showvminfo ${VM%% *}|grep Name:|sed -e 's/^Name:s*//g'"`
305
echo ')'
306
done
307
;;
308
309
*)
310
echo "Usage: $0 {start|stop|status|export|start-vm <VM
311
name>|stop-vm <VM name>|poweroff-vm <VM name>}|export-vm <VMname>"
312
exit 3
313
esac
314
315
exit 0
316
</pre>
317
318
To make the file executeable do
319
<pre><code class="bash">
320
chmod +x /etc/init.d/vboxcontrol
321
</code></pre>
322
Add vboxcontrol as service:
323
<pre><code class="bash">
324
cd /etc/init.d
325
chkconfig --add vboxcontrol
326
</code></pre>
327
Manually start the service
328
<pre><code class="bash">
329
service vboxcontrol start
330
</code></pre>
331
you can check the runlevels by
332
<pre><code class="bash">
333
chkconfig --list vboxcontrol
334
</code></pre>
335
you should get an output like:
336
<pre><code class="bash">
337
vboxcontrol     0:off   1:off   2:off   3:on   4:on   5:on   6:off
338
</code></pre>
339
340
With this setup, guest machines are also automatically shutdown cleanly in a host shutdown
341
process. The shell-script @vboxcontrol@ processes the file @machines_enabled_stop@. The first machines is sent a ACPI-Poweroff-Signal, after 10 seconds the second machine and so on. After the last machine have got the ACPI-Poweroff-Signal the script waits until all machines have reached power-off-status. By doing so a crashed or hanging virtual machine will prevent a shutdown of the host-system.
342
343
At boot-time each virtual machine being listed in the file @machines_enabled_start@ is being started according to the order within the file. The time gap between each machine is 20 seconds.
344
345
h2. Problem
346
347
h3. KVM and Virtualbox side by side
348
349
VirtualBox might not work while KVM extensions are active
350
http://www.dedoimedo.com/computers/kvm-virtualbox.html may help