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