Last active
February 3, 2022 14:54
-
-
Save lbogdan/11a3df688bb238698556127cdeea6034 to your computer and use it in GitHub Desktop.
createvm.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
IMAGE_QCOW2="AlmaLinux-8-GenericCloud-8.5-20211119.x86_64.qcow2" | |
IMAGE_URL="https://repo.almalinux.org/almalinux/8/cloud/x86_64/images/$IMAGE_QCOW2" | |
IMAGE_VMDK="AlmaLinux-8-GenericCloud-8.5-20211119.x86_64.vmdk" | |
VMS_PATH="/Users/lbogdan/Virtual Machines.localized" | |
VM_NAME="control" | |
USERNAME="lbogdan" | |
PASSWD='$5$hIk2gCtfc6MamyXQ$YJo.HLrDlRvRzD3.hfzH67.8nhVo5CbDJy822R6Gm.A' | |
SSH_PUBLIC_KEY="ssh-rsa ..." | |
DOMAIN="localdomain" | |
INTERFACE="eth0" | |
IP="192.168.92.10" | |
GATEWAY="192.168.92.2" | |
CPU="2" | |
MEMORY="2048" # MB | |
GUEST_OS="rhel8-64" | |
if [ ! -f "$IMAGE_QCOW2" ]; then | |
echo "* downloading image" | |
curl -LO "$IMAGE_URL" | |
else | |
echo "~ image downloaded" | |
fi | |
if [ ! -f "$IMAGE_VMDK" ]; then | |
echo "* converting image" | |
qemu-img convert -O vmdk "$IMAGE_QCOW2" "$IMAGE_VMDK" | |
else | |
echo "~ image converted" | |
fi | |
VM_PATH="$VMS_PATH/$VM_NAME.vmwarevm" | |
if [ ! -d "$VM_PATH" ]; then | |
echo "* creating VM" | |
mkdir "$VM_PATH" | |
cp -v "$IMAGE_VMDK" "$VM_PATH/$IMAGE_VMDK" | |
/Applications/VMware\ Fusion.app/Contents/Library/vmware-vdiskmanager -c -s 100MB -a lsilogic -t 0 "$VM_PATH/storage.vmdk" | |
VMX_FILE="$VM_PATH/$VM_NAME.vmx" | |
cat >"$VMX_FILE" <<EOT | |
.encoding = "UTF-8" | |
config.version = "8" | |
virtualHW.version = "18" | |
mks.enable3d = "TRUE" | |
pciBridge0.present = "TRUE" | |
pciBridge4.present = "TRUE" | |
pciBridge4.virtualDev = "pcieRootPort" | |
pciBridge4.functions = "8" | |
pciBridge5.present = "TRUE" | |
pciBridge5.virtualDev = "pcieRootPort" | |
pciBridge5.functions = "8" | |
pciBridge6.present = "TRUE" | |
pciBridge6.virtualDev = "pcieRootPort" | |
pciBridge6.functions = "8" | |
pciBridge7.present = "TRUE" | |
pciBridge7.virtualDev = "pcieRootPort" | |
pciBridge7.functions = "8" | |
vmci0.present = "TRUE" | |
hpet0.present = "TRUE" | |
nvram = "$VM_NAME.nvram" | |
virtualHW.productCompatibility = "hosted" | |
powerType.powerOff = "soft" | |
powerType.powerOn = "soft" | |
powerType.suspend = "soft" | |
powerType.reset = "soft" | |
displayName = "$VM_NAME" | |
usb.vbluetooth.startConnected = "TRUE" | |
guestOS = "$GUEST_OS" | |
tools.syncTime = "TRUE" | |
tools.upgrade.policy = "upgradeAtPowerCycle" | |
sound.autoDetect = "TRUE" | |
sound.fileName = "-1" | |
numvcpus = "$CPU" | |
vcpu.hotadd = "TRUE" | |
memsize = "$MEMORY" | |
mem.hotadd = "TRUE" | |
sata0.present = "TRUE" | |
nvme0.present = "TRUE" | |
nvme0:0.fileName = "$IMAGE_VMDK" | |
nvme0:0.present = "TRUE" | |
sata0:1.autodetect = "TRUE" | |
sata0:1.deviceType = "cdrom-image" | |
sata0:1.fileName = "cloudinit.iso" | |
sata0:1.startConnected = "TRUE" | |
sata0:1.present = "TRUE" | |
svga.graphicsMemoryKB = "8388608" | |
ethernet0.connectionType = "nat" | |
ethernet0.addressType = "generated" | |
ethernet0.virtualDev = "vmxnet3" | |
ethernet0.linkStatePropagation.enable = "TRUE" | |
serial0.fileType = "thinprint" | |
serial0.fileName = "thinprint" | |
ethernet0.present = "TRUE" | |
extendedConfigFile = "$VM_NAME.vmxf" | |
floppy0.present = "FALSE" | |
ehci:0.parent = "-1" | |
ehci:0.port = "0" | |
ehci:0.deviceType = "video" | |
ehci:0.present = "TRUE" | |
nvme0:1.fileName="storage.vmdk" | |
nvme0:1.present="TRUE" | |
EOT | |
touch meta-data | |
cat >user-data <<EOT | |
#cloud-config | |
users: | |
- name: $USERNAME | |
ssh_authorized_keys: | |
- $SSH_PUBLIC_KEY | |
sudo: ['ALL=(ALL) NOPASSWD:ALL'] | |
shell: /bin/bash | |
lock_passwd: false | |
passwd: $PASSWD | |
fqdn: $VM_NAME.$DOMAIN | |
hostname: $VM_NAME | |
manage_etc_hosts: true | |
runcmd: | |
- mkfs.xfs /dev/nvme0n2 | |
- mkdir /s | |
- echo '/dev/nvme0n2 /s xfs defaults 0 0' >>/etc/fstab | |
- mount -a | |
EOT | |
cat >network-config <<EOT | |
version: 1 | |
config: | |
- type: physical | |
name: $INTERFACE | |
subnets: | |
- type: static | |
address: $IP | |
netmask: 255.255.255.0 | |
gateway: $GATEWAY | |
dns_nameservers: | |
- 1.1.1.1 | |
- 8.8.8.8 | |
dns_search: | |
- $DOMAIN | |
EOT | |
mkisofs -output cloudinit.iso -volid cidata -joliet -rock {meta-data,user-data,network-config} | |
# mkisofs -output cloudinit.iso -volid cidata -joliet -rock {meta-data,user-data} | |
cp -v cloudinit.iso "$VM_PATH" | |
vmrun start "$VMX_FILE" # nogui | |
else | |
echo "~ VM already exists, exiting" | |
exit | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment