Files
init.sh/modules/create_vm.sh
2022-12-17 20:19:03 +01:00

132 lines
3.6 KiB
Bash

# ------------------------------------------------------------------------------
# Create VBox VM
# This file is part of the init.sh project
# Copyright (c) 2019-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
# ------------------------------------------------------------------------------
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# https://opensource.org/licenses/BSD-3-Clause
# ------------------------------------------------------------------------------
# Variable:
# To be defined
# ------------------------------------------------------------------------------
export VER_create_vm="0.0.2"
export DEP_create_vm="upgrade_dist install_pkg"
create_vm()
{
if [[ $WITH_VM != "yes" ]]; then
prnt W "That computer is not configured for virtualisation, nothing to do."
return 0
fi
pkginst virtualbox
# Create welcoming dirs
mkdir -pv $VM_ROOT/{.config/VirtualBox,vms/$VM_NAME}
# Create symbolic links (because of rights only main user can start it)
ln -sv $VM_ROOT/.config/VirtualBox /root/.config/VirtualBox
ln -sv $VM_ROOT/.config/VirtualBox /home/$MAINUSER/.config/VirtualBox
local accel_2d=off
case $VM_OS in
Windows*)
accel_2d=on
;;
esac
# Create emty VM
local targetdir=$VM_ROOT/vms/$VM_NAME
vboxmanage createvm --ostype $VM_OS --basefolder $targetdir \
--name $VM_NAME --register
# Give main caracteristics
vboxmanage modifyvm $VM_NAME \
--cpus $VM_CPU --memory $VM_MEM --vram $VM_VID_MEM \
--boot1 $VM_BOOT1 --VM_BOOT2 $VM_BOOT2 --boot3 $VM_BOOT3 \
--nic1 bridged --bridgeadapter1 $VM_IF_BRIDGE \
--accelerate2dvideo $accel_2d \
--clipboard bidirectional --draganddrop disabled
# Add a SATA controler
vboxmanage storagectl $VM_NAME \
--name sata0 --add sata --controller IntelAHCI --bootable on \
--hostiocache on --portcount 6
# Create a virtual HDD
vboxmanage createmedium \
--size $VM_DISK_SIZE --variant Fixed --filename $targetdir/$VM_NAME.vdi
# Connect the created HDD to the VM
vboxmanage storageattach $VM_NAME \
--storagectl sata0 --port 1 --device 0 --type hdd \
--medium $targetdir/$VM_NAME.vdi
unset targetdir accel_2d
# Add empty DVD
vboxmanage storageattach $VM_NAME --storagectl sata0 --port 2 --device 0 \
--medium emptydrive
# Add shares
local share= i=0
for share in $VM_SHARES_NAME; do
(( i+=1 ))
local j=0 hostpath=""
for path in $VM_SHARES_PATH; do
(( j+=1 ))
if [[ $i -eq $j ]]; then
hostpath=$path
fi
done
unset j
vboxmanage sharedfolder add $VM_NAME \
--name ${VM_SHARES_NAME,,} --hostpath $hostpath
done
unset share i
}
precheck_create_vm()
{
if [[ $WITH_VM == "yes" ]]; then
if [[ -z $VM_NAME ]]; then
prnt E "The virtual machine must have a name."
die 181
fi
if [[ -z VM_CPU || -z VM_MEM || -z VM_OS || -z VM_ROOT || \
-z VM_BOOT1 || -z VM_BOOT2 || -z VM_BOOT3 || -z VM_VID_MEM ||
-z VM_IF_BRIDGE || -z VM_DISK_SIZE ]]; then
prnt E "A necessary declaration for the virtual machine is absent!"
die 181
else
prnt I "The virtual machine \"$VM_NAME\" will be created in $VM_ROOT..."
fi
local share= i=0 j=0
for share in $VM_SHARES_NAME; do
(( i+=1 ))
done
for share in $VM_SHARES_PATH; do
(( j+=1 ))
done
unset share
if [[ $i -eq $j ]]; then
prnt I "The virtual machine will access $i directories from the host."
else
prnt E "The number of share and path to share is different!"
die 182
fi
else
prnt I "No virtual machine to create."
fi
}
export -f create_vm
export -f precheck_create_vm
# EOF