Projekt

Allgemein

Profil

Setup virtualboxhost60 » Historie » Revision 2

Revision 1 (Jeremias Keihsler, 17.01.2019 08:37) → Revision 2/5 (Jeremias Keihsler, 17.01.2019 08:38)

h1. Install Procedure for VirtualBox 6.0 

 h2. Requirements 

 To install VirtualBox you will need the following: 
 * a installed and supported operating system (e.g. Fedora29) 
 * root-access 
 * a fast internet connection 
 * [[repo_virtualbox|VirtualBox - Repo]] 

 h2. Preliminary Note 

 This procedure is based on a documentation supplied by www.proclos.com. 

 You may also find additional information at 
 * http://wiki.centos.org/HowTos/Virtualization/VirtualBox/ 
 * https://www.if-not-true-then-false.com/2010/install-virtualbox-with-yum-on-fedora-centos-red-hat-rhel/ 

 a possible solution to get the autostart-service up and running finally    is found here: 
 http://nathangiesbrecht.com/centos-7-virtualbox-vboxautostart-service-setup 

 h2. Install Virtual Box 

 

 h3. Install DKMS 

 Install Dynamic Kernel Module System: (automatically installs appropriate kernel modules per 
 selected version) 
 <pre><code class="bash"> 
 dnf yum install binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-devel dkms qt5-qtx11extras libxkbcommon 
 </code></pre> 

 

 h3. Install VirtualBox 

 <pre><code class="bash"> 
 yum install VirtualBox-6.0 
 </code></pre> 

 Rebuild kernel modules with following command: 
 <pre><code class="bash"> 
 /usr/lib/virtualbox/vboxdrv.sh setup 
 </code></pre> 

 h2. Setup vboxusers 

 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 

 <pre><code class="bash"> 
 usermod -a -G vboxusers username 
 </code></pre> 

 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. 

 h2. Start virtual box. 

 @GNOME-Menu->Applications->System Tools->Oracle VM VirtualBox@ 

 h2. Install VirtualBox-Extension 

 Download the extension from the virtualbox homepage www.virtualbox.org. 
 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 
 able to connect to guest machines using WindowsRemote Desktop-Client, which is very handy, if you need graphical access. 

 @VBextpack_update.sh@ 
 <pre> 
 #!/bin/bash 
 version=$(vboxmanage -v) 
 echo $version 
 var1=$(echo $version | cut -d 'r' -f 1) 
 echo $var1 
 var2=$(echo $version | cut -d 'r' -f 2) 
 echo $var2 
 file="Oracle_VM_VirtualBox_Extension_Pack-$var1-$var2.vbox-extpack" 
 echo $file 
 wget http://download.virtualbox.org/virtualbox/$var1/$file -O /tmp/$file 
 VBoxManage extpack uninstall "Oracle VM VirtualBox Extension Pack" 
 VBoxManage extpack install /tmp/$file --replace 
 </pre> 

 h2. Automatic Start/Stop of virtual machines systemd-style 


 this is taken from http://www.ericerfanian.com/automatically-starting-virtualbox-vms-on-archlinux-using-systemd/ 

 Add a file called @vboxvmservice@.service@ in @/etc/systemd/system/.@ 

 <pre> 
 [Unit] 
 Description=VBox Virtual Machine %i Service 
 Requires=systemd-modules-load.service 
 After=systemd-modules-load.service 

 [Service] 
 User=user 
 Group=vboxusers 
 ExecStart=/usr/bin/VBoxHeadless -s %i 
 ExecStop=/usr/bin/VBoxManage controlvm %i savestate 

 [Install] 
 WantedBy=multi-user.target 
 </pre> 

 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=’. 

 You can also replace ‘savestate’ in ‘ExecStop=’ with ‘poweroff’ or ‘acpipowerbutton’ to hard-stop the VM, or ask for a clean shut-down, respectively. 

 To enable one or more VMs at boot, enter: 

 <pre><code class="bash"> 
 systemctl enable vboxvmservice@vm_name.service 
 </code></pre> 
 h2. 5 Automatic Start/Stop of virtual machines SystemV-style 


 As an alternative to the following solution, someone might find https://sourceforge.net/projects/vboxtool/ interesting to investigate. 

 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. 
 )): 

 add the list of names of the virtual machines that should be started / stopped automatically in following files: 
 @/etc/virtualbox/machines_enabled_start@ and @/etc/virtualbox/machines_enabled_stop@ 

 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. 
 <pre><code class="bash"> 
 cd /etc 
 mkdir virtualbox 
 vim /etc/virtualbox/machines_enabled_start 
 </code></pre> 
 <pre><code class="bash"> 
 vim /etc/virtualbox/machines_enabled_stop 
 </code></pre> 

 Afterwards you can check the file-content by 
 <pre><code class="bash"> 
 cat /etc/virtualbox/machines_enabled_start 
 </code></pre> 
 the content should look like: 
 <pre><code class="bash"> 
 firewall-pfsense 
 clusterOne 
 clusterTwo 
 postgresql-solo 
 windows7-janus 
 </code></pre> 
 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. 
 <pre><code class="bash"> 
 cat /etc/virtualbox/machines_enabled_stop 
 </code></pre> 
 the content could look like: 
 <pre><code class="bash"> 
 windows7-janus 
 postgresql-solo 
 clusterTwo 
 clusterOne 
 firewall-pfsense 
 </code></pre> 

 Also create a file @/etc/init.d/vboxcontrol@ with following content: 

 Don't forget to set the correct user @VM_USER@ 
 <pre> 
 #! /bin/sh 
 # vboxcontrol     Startup script for VirtualBox Virtual Machines 
 # 
 # chkconfig: 345 98 02 
 # description: Manages VirtualBox VMs 
 # processname: vboxcontrol 
 # 
 # pidfile: /var/run/vboxcontrol/vboxcontrol.pid 
 # 
 ### BEGIN INIT INFO 
 # 
 ### END INIT INFO 
 # 
 # Version 20110509 by Jeremias Keihsler based on: 
 # Version 20090301 by Kevin Swanson <kswan.info> based on: 
 # Version 2008051100 by Jochem Kossen <jochem.kossen@gmail.com> 
 # http://farfewertoes.com 
 # 
 # Released in the public domain 
 # 
 # This file came with a README file containing the instructions on how 
 # to use this script. 
 # 

 # Source function library. 
 if [ -f /etc/init.d/functions ] ; then 
 . /etc/init.d/functions 
 elif [ -f /etc/rc.d/init.d/functions ] ; then 
 . /etc/rc.d/init.d/functions 
 else 
 exit 1 
 fi 

 ################################################################################ 
 # INITIAL CONFIGURATION 
 VBOXDIR="/etc/virtualbox" 
 VM_USER="OMB" 
 USE_NAT="no" 

 export PATH="${PATH:+$PATH:}/bin:/usr/bin:/usr/sbin:/sbin" 

 if [ -f $VBOXDIR/config ]; then 
 . $VBOXDIR/config 
 fi 

 SU="su $VM_USER -c" 
 VBOXMANAGE="VBoxManage -nologo" 

 ################################################################################ 
 # FUNCTIONS 

 # Determine if USE_NAT is set to "yes" 
 use_nat() { 
 if [ "$USE_NAT" = "yes" ]; then 
 return `true` 
 else 
 return `false` 
 fi 
 } 

 log_failure_msg() { 
 echo $1 
 } 

 log_action_msg() { 
 echo $1 
 } 

 # Check for running machines every few seconds; return when all machines are 
 # down 
 wait_for_closing_machines() { 
 RUNNING_MACHINES=`$SU "$VBOXMANAGE list runningvms" | wc -l` 
 if [ $RUNNING_MACHINES != 0 ]; then 
 sleep 2 
 wait_for_closing_machines 
 fi 
 } 

 ################################################################################ 
 # RUN 
 case "$1" in 
 start) 
 if [ -f /etc/virtualbox/machines_enabled_start ]; then 

 cat /etc/virtualbox/machines_enabled_start | while read VM; do 
 log_action_msg "Starting VM: $VM ..." 
 $SU "$VBOXMANAGE startvm "$VM" --type headless" 
 sleep 20 
 RETVAL=$? 
 done 
 touch /var/lock/subsys/vboxcontrol 
 fi 
 ;; 
 stop) 
 # NOTE: this stops all running VM's. Not just the ones listed in the 
 # config 
 cat /etc/virtualbox/machines_enabled_stop    | while read VM; do 
 log_action_msg "Shutting down VM: $VM ..." 
 $SU "$VBOXMANAGE controlvm "$VM" acpipowerbutton" 
 sleep 10 
 done 
 rm -f /var/lock/subsys/vboxcontrol 
 wait_for_closing_machines 
 ;; 
 export) 
 # NOTE: this stops and exports the listed VMs 
 cat /etc/virtualbox/machines_enabled_export    | while read VM; do 
   log_action_msg "Shutting down VM: $VM ..." 
   $SU "$VBOXMANAGE controlvm "$VM" acpipowerbutton" 
   /bin/echo -en "\a" > /dev/console 
   sleep 10 
 done 
 wait_for_closing_machines 
 JKE_DATE=$(date +%F) 
 cat /etc/virtualbox/machines_enabled_export    | while read VM; do 
   log_action_msg "Exporting VM: $VM ..." 
   $SU "$VBOXMANAGE export "$VM" -o "$VM"_"$JKE_DATE"."ova" --manifest --vsys 0 --version $JKE_DATE" 
 done 
 rm -f /var/lock/subsys/vboxcontrol 
 ;; 
 start-vm) 
 log_action_msg "Starting VM: $2 ..." 
 $SU "$VBOXMANAGE startvm "$2" --type headless" 
 ;; 
 stop-vm) 
 log_action_msg "Stopping VM: $2 ..." 
 $SU "$VBOXMANAGE controlvm "$2" acpipowerbutton" 
 ;; 
 poweroff-vm) 
 log_action_msg "Powering off VM: $2 ..." 
 $SU "$VBOXMANAGE controlvm "$2" poweroff" 
 ;; 
 export-vm) 
 # NOTE: this exports the given VM 
 log_action_msg "Exporting VM: $2 ..." 
 JKE_DATE=$(date +%F) 
 $SU "$VBOXMANAGE export "$2" -o "$2"_"$JKE_DATE"."ova" --manifest --vsys 0 --version $JKE_DATE" 
 ;; 
 status) 
 echo "The following virtual machines are currently running:" 
 $SU "$VBOXMANAGE list runningvms" | while read VM; do 
 echo -n "$VM (" 
 echo -n `$SU "VBoxManage showvminfo ${VM%% *}|grep Name:|sed -e 's/^Name:s*//g'"` 
 echo ')' 
 done 
 ;; 

 *) 
 echo "Usage: $0 {start|stop|status|export|start-vm <VM 
 name>|stop-vm <VM name>|poweroff-vm <VM name>}|export-vm <VMname>" 
 exit 3 
 esac 

 exit 0 
 </pre> 

 To make the file executeable do 
 <pre><code class="bash"> 
 chmod +x /etc/init.d/vboxcontrol 
 </code></pre> 
 Add vboxcontrol as service: 
 <pre><code class="bash"> 
 cd /etc/init.d 
 chkconfig --add vboxcontrol 
 </code></pre> 
 Manually start the service 
 <pre><code class="bash"> 
 service vboxcontrol start 
 </code></pre> 
 you can check the runlevels by 
 <pre><code class="bash"> 
 chkconfig --list vboxcontrol 
 </code></pre> 
 you should get an output like: 
 <pre><code class="bash"> 
 vboxcontrol       0:off     1:off     2:off     3:on     4:on     5:on     6:off 
 </code></pre> 

 With this setup, guest machines are also automatically shutdown cleanly in a host shutdown 
 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. 

 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. 

 h2. Problem 

 h3. KVM and Virtualbox side by side 

 VirtualBox might not work while KVM extensions are active 
 http://www.dedoimedo.com/computers/kvm-virtualbox.html may help