Projekt

Allgemein

Profil

Setup virtualboxhost50 » Historie » Version 1

Jeremias Keihsler, 13.01.2017 08:57

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