Projekt

Allgemein

Profil

Setup virtualboxhost71 » Historie » Version 1

Jeremias Keihsler, 17.09.2024 07:38

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