# ------------------------------------------------------------------------------ # Create VBox VM # This file is part of the init.sh project # Copyright (c) 2019-2021 Geoffray Levasseur # ------------------------------------------------------------------------------ # 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.1" export DEP_create_vm="upgrade_dist install_pkg" create_vm() { if [[ $WITH_VM != "yes" ]]; then prnt I "Cette machine n'est pas configurée pour la virtualisation, rien à faire." 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 "La machine virtuelle à créer doit avoir un nom." 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 "Une variable de description de la machine virtuelle n'est pas déclarée !" die 181 else prnt I "La machine virtuelle \"$VM_NAME\" sera crée dans $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 "La machine virtuelle aura accès à $i répertoires de l'hôte." else prnt E "Le nombre de partage et de chemin à partager diffère !" die 182 fi else prnt I "Aucune machine virtuelle à installer." fi } export -f create_vm export -f precheck_create_vm # EOF