set DIR recognization to be able to execute script outside of project dir and create copy for modified img file

Signed-off-by: Roger Sikorski <roger@sikorski.cloud>
This commit is contained in:
Roger Sikorski 2023-03-17 16:49:57 +01:00
parent 2ce0a6b735
commit f6a0beed5a
Signed by: RogerSik
GPG Key ID: 207958C85208EAC4

View File

@ -1,6 +1,10 @@
#!/bin/bash #!/bin/bash
CURL_LAST_LTS_RELEASE=$(curl -s https://changelogs.ubuntu.com/meta-release-lts | grep Dist: | tail -n1) CURL_LAST_LTS_RELEASE=$(curl -s https://changelogs.ubuntu.com/meta-release-lts | grep Dist: | tail -n1)
LAST_LTS_RELEASE=${CURL_LAST_LTS_RELEASE:6} LAST_LTS_RELEASE=${CURL_LAST_LTS_RELEASE:6}
DIR=$(
cd "$(dirname "$0")" || exit 1
pwd
)
echo "" echo ""
echo "This script will download and setup an vm with the newest Ubuntu LTS Version ($LAST_LTS_RELEASE)" echo "This script will download and setup an vm with the newest Ubuntu LTS Version ($LAST_LTS_RELEASE)"
@ -11,7 +15,7 @@ if [ "$EUID" -ne 0 ]; then
exit exit
fi fi
if [ ! -f "ssh-keys.pub" ]; then if [ ! -f "$DIR/ssh-keys.pub" ]; then
echo "Warning ssh-keys.pub doesn't exists" echo "Warning ssh-keys.pub doesn't exists"
echo "Will abort now" echo "Will abort now"
exit exit
@ -28,12 +32,15 @@ read -r VM_NAME
echo "Ensure libguestfs-tools is installed" echo "Ensure libguestfs-tools is installed"
apt install libguestfs-tools -y apt install libguestfs-tools -y
echo "Create copy of $LAST_LTS_RELEASE-server-cloudimg-amd64 for image modification"
cp -r "/tmp/$LAST_LTS_RELEASE-server-cloudimg-amd64.img" "/tmp/$LAST_LTS_RELEASE-server-cloudimg-amd64.modified.img"
echo "Install qemu-guest-agent on Ubuntu image" echo "Install qemu-guest-agent on Ubuntu image"
virt-customize -a "/tmp/$LAST_LTS_RELEASE-server-cloudimg-amd64.img" --install qemu-guest-agent virt-customize -a "/tmp/$LAST_LTS_RELEASE-server-cloudimg-amd64.modified.img" --install qemu-guest-agent
echo "Create Proxmox VM image from Ubuntu Cloud Image" echo "Create Proxmox VM image from Ubuntu Cloud Image"
qm create "$VM_ID" --memory 1024 --balloon 0 --cores 2 --net0 virtio,bridge=vmbr0 --scsihw virtio-scsi-pci qm create "$VM_ID" --memory 1024 --balloon 0 --cores 2 --net0 virtio,bridge=vmbr0 --scsihw virtio-scsi-pci
qm set "$VM_ID" --scsi0 local-lvm:0,import-from="/tmp/$LAST_LTS_RELEASE-server-cloudimg-amd64.img" qm set "$VM_ID" --scsi0 local-lvm:0,import-from="/tmp/$LAST_LTS_RELEASE-server-cloudimg-amd64.modified.img"
qm set "$VM_ID" --agent enabled=1,fstrim_cloned_disks=1 qm set "$VM_ID" --agent enabled=1,fstrim_cloned_disks=1
echo "Create Cloud-Init Disk and configure boot" echo "Create Cloud-Init Disk and configure boot"
@ -45,5 +52,8 @@ qm set "$VM_ID" \
--name "$VM_NAME" \ --name "$VM_NAME" \
--onboot 1 \ --onboot 1 \
--ciuser "devops" \ --ciuser "devops" \
--sshkeys ssh-keys.pub \ --sshkeys "$DIR/ssh-keys.pub" \
--ipconfig0 "ip=dhcp,ip6=dhcp" --ipconfig0 "ip=dhcp,ip6=dhcp"
echo "Cleanup: delete modified cloudimg"
rm -f "/tmp/$LAST_LTS_RELEASE-server-cloudimg-amd64.modified.img"