-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkvm.sh
executable file
·205 lines (167 loc) · 13.5 KB
/
mkvm.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
# TODO:
# check if fedora workstation/server and/or debian iso is present
# download if not
# check what instance of VM we are on
# increment by 1
BASEFOLDER="/home/gmarselis/VirtualBox VM"
VMNAME="ldap-server"
OSTYPE="Debian_64"
CPUS=4
MEMORY=4096
VRAM=256
DATE="$(date --iso)"
FILENAME="${BASEFOLDER}/media/${VMNAME}-$date.vdi"
DISKSIZE=60000
HOSTPATH='/home/gmarselis/Downloads/iso_images'
MEDIUM="${HOSTPATH}/debian-11.6.0-amd64-netinst.iso"
#MEDIUM="${HOSTPATH}/Fedora-Server-dvd-x86_64-37-1.7.iso"
#MEDIUM="${HOSTPATH}/Fedora-Workstation-Live-x86_64-37-1.7.iso"
AUTO_MOUNT_POINT="/mnt/downloads"
PARAVIRT_PROVIDER="kvm" # if set to default, under linux will still be set to KVM
CPU_HOTPLUG="off"
BRIDGEADAPTER1="$(ifconfig | awk -F: '/^en/ { print $1 }')"
ADDITIONS_ISO="/home/gmarselis/Downloads/VBoxGuestAdditions_7.0.6.iso"
HOSTNAME="molly-test-1-$date"
MACADDRESS="080027C48B19"
# 080027C48B18
# 080027C48B19
# 080027C48B20
# 080027C48B21
# 080027C48B22
FQDN="$HOSTNAME.marsel.is"
SHUTDOWNTIMEOUT="2"
NETNAME="natnetwork1"
NETWORK="192.168.15.0/24"
# Delete the registered VM
/usr/bin/VBoxManage unregistervm "${VMNAME}" --delete 2> /dev/null
# Delete the created VDI
vboxmanage closemedium disk "${FILENAME}" --delete 2> /dev/null
# Register VM
/usr/bin/VBoxManage createvm --name="${VMNAME}" --ostype="${OSTYPE}" --register --basefolder="${BASEFOLDER}" \
&& echo "/usr/bin/VBoxManage createvm --name=\"${VMNAME}\" --ostype=\"${OSTYPE}\" --register --basefolder=\"${BASEFOLDER}\""
[[ $? -gt 0 ]] && exit
# Give it EFI boot # EFI64 is required *only* for MacOS
/usr/bin/VBoxManage modifyvm "${VMNAME}" --firmware="efi" \
&& echo "/usr/bin/VBoxManage modifyvm \""${VMNAME}"\" --firmware=\"efi\""
[[ $? -gt 0 ]] && exit
# Set mouse, keyboard and usb settings
/usr/bin/VBoxManage modifyvm "${VMNAME}" --mouse="usb" --keyboard="usb" --clipboard-mode="bidirectional" --drag-and-drop="bidirectional" --monitor-count="1" --usb-ehci="off" --usb-ohci="off" --usb-xhci="on" \
&& echo "/usr/bin/VBoxManage modifyvm \"${VMNAME}\" --mouse=\"usb\" --keyboard=\"usb\" --clipboard-mode=\"bidirectional\" --drag-and-drop=\"bidirectional\" --monitor-count=\"1\" --usb-ehci=\"off\" --usb-ohci=\"off\" --usb-xhci=\"on\""
[[ $? -gt 0 ]] && exit
# set video controler and video memory
/usr/bin/VBoxManage modifyvm "${VMNAME}" --graphicscontroller="VMSVGA" --vram="${VRAM}" --accelerate-3d="on" --accelerate-2d-video="on" \
&& echo "/usr/bin/VBoxManage modifyvm \"${VMNAME}\" --graphicscontroller=\"VMSVGA\" --vram=\"${VRAM}\" --accelerate-3d=\"on\" --accelerate-2d-video=\"on\""
[[ $? -gt 0 ]] && exit
# Turn PAE on # we need PAE as a prerequsite to mboot into true 64-bit mode
/usr/bin/VBoxManage modifyvm "${VMNAME}" --pae="on" --long-mode="on" \
&& echo "/usr/bin/VBoxManage modifyvm \"${VMNAME}\" --pae=\"on\" --long-mode=\"on\""
[[ $? -gt 0 ]] && exit
# set memory and cpu cores # --cpu-hotplug=on
/usr/bin/VBoxManage modifyvm "${VMNAME}" --cpus="${CPUS}" --memory="${MEMORY}" --cpu-hotplug="on" \
&& echo "/usr/bin/VBoxManage modifyvm \"${VMNAME}\" --cpus=\"${CPUS}\" --memory=\"${MEMORY}\" --cpu-hotplug=\"on\"" # --cpu-hotplug="on"
[[ $? -gt 0 ]] && exit
# Set bios parameters for VM # --triple-fault-reset=on do not apply
# iommu=intel-> none. We do not need PCIe passthrough
/usr/bin/VBoxManage modifyvm "${VMNAME}" --description="Work VM for NVI" --acpi="on" --ioapic="on" --cpu-profile="host" --hpet="on" --hwvirtex="on" --apic="on" --x2apic="on" --paravirt-provider="${PARAVIRT_PROVIDER}" --nested-paging="on" --largepages="on" --vtx-vpid="on" --vtx-ux="on" --nested-hw-virt="off" --chipset="ich9" --iommu="none" --tpm-type="2.0" --bios-apic="x2apic" --rtc-use-utc="on" \
&& echo "/usr/bin/VBoxManage modifyvm \"${VMNAME}\" --description=\"Work VM for NVI\" --acpi=\"on\" --ioapic=\"on\" --cpu-profile=\"host\" --hpet=\"on\" --hwvirtex=\"on\" --apic=\"on\" --x2apic=\"on\" --paravirt-provider=\"${PARAVIRT_PROVIDER}\" --nested-paging=\"on\" --largepages=\"on\" --vtx-vpid=\"on\" --vtx-ux=\"on\" --nested-hw-virt=\"off\" --chipset=\"ich9\" --iommu=\"none\" --tpm-type=\"2.0\" --bios-apic=\"x2apic\" --rtc-use-utc=\"on\""
# Spectre attacks, mitigatte
/usr/bin/VBoxManage modifyvm "${VMNAME}" --ibpb-on-vm-entry="on" --ibpb-on-vm-exit="on" --spec-ctrl="on" --l1d-flush-on-sched="off" \
&& echo "/usr/bin/VBoxManage modifyvm \"${VMNAME}\" --ibpb-on-vm-entry=\"on\" --ibpb-on-vm-exit=\"on\" --spec-ctrl=\"on\" --l1d-flush-on-sched=\"off\""
[[ $? -gt 0 ]] && exit
# Disable all audio # regardless of whether you give --audio-controller or not, the default value is ac97. Furthermore, the --audio="none" is beind depreciated in the future.
# i'm stucking it in now, so i can completely disable the audiot and we will see.
/usr/bin/VBoxManage modifyvm "${VMNAME}" --audio="none" --audio-driver="none" --audio-controller="ac97" --audio-in="off" --audio-out="off" \
&& echo "/usr/bin/VBoxManage modifyvm \"${VMNAME}\" --audio=\"none\" --audio-driver=\"none\" --audio-controller=\"ac97\" --audio-in=\"off\" --audio-out=\"off\""
[[ $? -gt 0 ]] && exit
# Create a non-fixed, single, disk image
/usr/bin/VBoxManage createmedium disk --filename="${FILENAME}" --size="${DISKSIZE}" --format="VDI" --variant="Standard" \
&& echo "/usr/bin/VBoxManage createmedium disk --filename=\"${FILENAME}\" --size=\"${DISKSIZE}\" --format=\"VDI\" --variant=\"Standard\""
[[ $? -gt 0 ]] && exit
# Create an NVME controller for the disk
/usr/bin/VBoxManage storagectl "${VMNAME}" --controller="NVMe" --add="pcie" --name="NVME Controller" --hostiocache="on" --bootable="on" \
&& echo "/usr/bin/VBoxManage storagectl \"${VMNAME}\" --controller=\"NVMe\" --add=\"pcie\" --name=\"NVME Controller\" --hostiocache=\"on\" --bootable=\"on\""
[[ $? -gt 0 ]] && exit
# Create a SATA controller for the dvd
/usr/bin/VBoxManage storagectl "${VMNAME}" --controller="IntelAhci" --add="sata" --name="SATA Controller" --portcount="1" --hostiocache="on" --bootable="on" \
&& echo "/usr/bin/VBoxManage storagectl \"${VMNAME}\" --controller=\"IntelAhci\" --add=\"sata\" --name=\"SATA Controller\" --portcount=\"1\" --hostiocache=\"on\" --bootable=\"on\""
[[ $? -gt 0 ]] && exit
# Attach the VDI image to the NVME controller
/usr/bin/VBoxManage storageattach "${VMNAME}" --storagectl="NVME Controller" --device="0" --port="0" --type="hdd" --medium="${FILENAME}" --nonrotational="on" \
&& echo "/usr/bin/VBoxManage storageattach "${VMNAME}" --storagectl=\"NVME Controller\" --device=\"0\" --port=\"0\" --type=\"hdd\" --medium=\"${FILENAME}\" --nonrotational=\"on\""
[[ $? -gt 0 ]] && exit
# Attach the DVD boot image to the SATA controller
# /usr/bin/VBoxManage storageattach "${VMNAME}" --storagectl="SATA Controller" --device="0" --port="0" --type="dvddrive" --medium="${MEDIUM}" \
# && echo "/usr/bin/VBoxManage storageattach \"${VMNAME}\" --storagectl=\"SATA Controller\" --device=\"0\" --port=\"0\" --type=\"dvddrive\" --medium=\"${MEDIUM}\""
# [[ $? -gt 0 ]] && exit
# Set NIC1 to bridged # ifconfig | awk -F: '/^en/ { print $1 }' for the name of the interface
# --nic-boot-prio1="1" -> 0 is the default, 1 is the highest, 3, 4 lower; order therefore is [ 1, 0, 2, 3, 4]
# /usr/bin/VBoxManage modifyvm "${VMNAME}" --nic1="bridged" --bridgeadapter1="${BRIDGEADAPTER1}" --cable-connected1="on" --nic-boot-prio1="1" --nic-promisc1="deny" --mac-address1="$MACADDRESS" \
# && echo "/usr/bin/VBoxManage modifyvm \"${VMNAME}\" --nic1=\"bridged\" --bridgeadapter1=\"${BRIDGEADAPTER1}\" --cable-connected1=\"on\" --nic-boot-prio1=\"1\" --nic-promisc1=\"deny\" --mac-address1=\"$MACADDRESS\""
# [[ $? -gt 0 ]] && exit
# Declare a natnetwork and
# Set NIC1 to NAT
# --nic-boot-prio1="1" -> 0 is the default, 1 is the highest, 3, 4 lower; order therefore is [ 1, 0, 2, 3, 4]
# /usr/bin/VBoxManage natnetwork stop --netname "${NETNAME}"
# /usr/bin/VBoxManage natnetwork remove --netname "${NETNAME}"
# /usr/bin/VBoxManage natnetwork add --netname "${NETNAME}" --network "${NETWORK}" --enable --dhcp on \
# && echo "/usr/bin/VBoxManage natnetwork add --netname \""${NETNAME}"\" --network \"${NETWORK}\" --enable --dhcp \"on\""
# /usr/bin/VBoxManage natnetwork start --netname "${NETNAME}" \
# && echo "/usr/bin/VBoxManage natnetwork start --netname \""${NETNAME}"\""
# [[ $? -gt 0 ]] && exit
/usr/bin/VBoxManage modifyvm "${VMNAME}" --nic1="natnetwork" --cable-connected1="on" --nic-boot-prio1="1" --nic-promisc1="deny" --mac-address1="$MACADDRESS" \
&& echo "/usr/bin/VBoxManage modifyvm \"${VMNAME}\" --nic1=\"natnetwork\" --bridgeadapter1=\"${BRIDGEADAPTER1}\" --cable-connected1=\"on\" --nic-boot-prio1=\"1\" --nic-promisc1=\"deny\" --mac-address1=\"$MACADDRESS\""
[[ $? -gt 0 ]] && exit
# Set boot order: PXEboot from net, dvd, disk and none # --bios-pxe-debug="on"
# /usr/bin/VBoxManage modifyvm "${VMNAME}" --boot1 "net" --boot2 "dvd" --boot3 "disk" --boot4 "none" \
# && echo "/usr/bin/VBoxManage modifyvm \"${VMNAME}\" --boot1 \"net\" --boot2 \"dvd\" --boot3 \"disk\" --boot4 \"none\"
/usr/bin/VBoxManage modifyvm "${VMNAME}" --boot1 "net" --boot2 "none" --boot3 "none" --boot4 "none" --bios-pxe-debug="on" \
&& echo "/usr/bin/VBoxManage modifyvm \"${VMNAME}\" --boot1 \"net\" --boot2 \"none\" --boot3 \"none\" --boot4 \"none\""
[[ $? -gt 0 ]] && exit
# Set boot resolution to N
# where N -> Resolution
# 0 -> 640 x 480
# 1 -> 800 x 600
# 2 -> 1024 x 768
# 3 -> 1280 x 1024
# 4 -> 1440 x 900
# 5 -> 1920 x 1200
/usr/bin/VBoxManage setextradata "${VMNAME}" VBoxInternal2/EfiGraphicsResolution "5" \
&& echo "/usr/bin/VBoxManage setextradata \"${VMNAME}\" VBoxInternal2/EfiGraphicsResolution \"5\""
[[ $? -gt 0 ]] && exit
# Set a shared folder for initial file transfers
/usr/bin/VBoxManage sharedfolder add "${VMNAME}" --name="downloads" --hostpath="${HOSTPATH}" --automount --auto-mount-point="${AUTO_MOUNT_POINT}" \
&& echo "/usr/bin/VBoxManage sharedfolder add \"${VMNAME}\" --name=\"downloads\" --hostpath=\"${HOSTPATH}\" --automount --auto-mount-point=\"${AUTO_MOUNT_POINT}\""
[[ $? -gt 0 ]] && exit
# Alter the logo, boot time and uefi boot menu
# --bios-boot-menu= disabled | menuonly | messageandmenu
/usr/bin/VBoxManage modifyvm "${VMNAME}" --bios-logo-fade-in="off" --bios-logo-fade-out="off" --bios-logo-display-time=0 --bios-boot-menu="disabled" \
&& echo "/usr/bin/VBoxManage modifyvm \"${VMNAME}\" --bios-logo-fade-in=\"off\" --bios-logo-fade-out=\"off\" --bios-logo-display-time=0 --bios-boot-menu=\"disabled\""
# for future use
# --teleporter-address=0.0.0.0 allows VBox to listen to *all* requests for teleportation. Limit as needed
# --cpuid-portability-level=0 makes available all CPU features to the host? but there is no guaratnee what level presents what feature?
# --teleporter-port="6000" Port 6000 is set by me, given in the manual as an example, but not official and/or standardized
# VBoxManage modifyvm $vmname --teleporter="on" --teleporter-port="6000" --teleporter-address="0.0.0.0" --teleporter-password=******* --teleporter-password-file="/home/captaincrunch/password.txt" --cpuid-portability-level=0
# start vm, to test settings
# /usr/bin/VBoxManage startvm "${VMNAME}" #&& echo "/usr/bin/VBoxManage startvm \"${VMNAME}\""
#sleep ${SHUTDOWNTIMEOUT}
# terminate VM
#/usr/bin/VBoxManage controlvm "${VMNAME}" poweroff
# Unattended Debian and debian-like systems install
# VBoxManage unattended detect <--iso=install-iso> [--machine-readable]
# Detects the guest operating system (OS) on the specified installation ISO and displays the result.
# This can be used as input when creating a VM for the ISO to be installed in.
# --package-selection-adjustment="minimal" --image-index="number" --script-template="file" --post-install-template="file" --post-install-command="command" --extra-install-kernel-parameters="params"
# --auxiliary-base-path="path" # useful only for Debian
# Running VBoxManage unattended install creates (always?) four files in the Virtual Box file folder:
# Unattended-<GUID>-aux-iso.viso
# Unattended-<GUID>-grub.cfg
# Unattended-<GUID>-preseed.cfg
# Unattended-<GUID>-vboxpostinstall.sh
# Fedora 37
# VBoxManage unattended install "${VMNAME}" --iso="$medium" --user="gmarselis" --password="12345" --full-user-name="George Marselis" --install-additions --additions-iso="$additions_iso" --locale="en_US" --country="NO" --time-zone="UTC+1" --hostname="$fqdn" --language="en_US" --auxiliary-base-path="D:\kot"&& echo "VBoxManage unattended install `"$vmname`" --iso=`"$medium`" --user=`"gmarselis`" --password=`"12345`" --full-user-name=`"George Marselis`" --install-additions --additions-iso=`"$additions_iso`" --locale=`"en_US`" --country=`"NO`" --time-zone=`"UTC+1`" --hostname=`"$fqdn`" --dry-run --language=`"en_US`""
# Debian Buster
#/usr/bin/VBoxManage unattended install "${VMNAME}" --iso="/home/gmarselis/Downloads/debian-11.6.0-amd64-netinst.iso" --user="gmarselis" --password="12345" --full-user-name="George Marselis" --install-additions --additions-iso="${ADDITIONS_ISO}" --locale="en_US" --country="NO" --time-zone="UTC+1" --hostname="$fqdn" --dry-run --language="en_US" && echo "/usr/bin/VBoxManage unattended install \"${VMNAME}\" --iso=\"/home/gmarselis/Downloads/debian-11.6.0-amd64-netinst.iso\" --user=\"gmarselis\" --password=\"12345\" --full-user-name=\"George Marselis\" --install-additions --additions-iso=\"${ADDITIONS_ISO}\" --locale=\"en_US\" --country=\"NO\" --time-zone=\"UTC+1\" --hostname=\"${FQDN}\" --dry-run --language=\"en_US\""
# Autostarting VM During Host System Boot
# the "1" is set by me. There are no suggestions in the manual.
# VBoxManage modifyvm $vmname --autostart-enabled="on" --autostart-delay="1"