#!/usr/bin/env bash # safety first set -euf -o pipefail # check we have what we need type virt-customize type wget type qemu-img # get the cloud image - btw this is how they are built https://salsa.debian.org/cloud-team/debian-cloud-images/-/blob/master/doc/details.md rm -fv debian-13-genericcloud-amd64.qcow2 wget https://cloud.debian.org/images/cloud/trixie/latest/debian-13-genericcloud-amd64.qcow2 # install wget (for cloud-init), set the root password to "password" and set timezone to UTC virt-customize -a debian-13-genericcloud-amd64.qcow2 --install wget --root-password password:password --timezone UTC # also save the root password hash, we'll need it later virt-customize -a debian-13-genericcloud-amd64.qcow2 --run-command 'getent shadow root | cut -d: -f2 > /etc/cloud/default-root-hash && chmod 600 /etc/cloud/default-root-hash' # install any additional software virt-customize -a debian-13-genericcloud-amd64.qcow2 --install mc --install screen --install tmux --install jnettop --install iptraf-ng --install tcpdump \ --install net-tools --install iperf3 --install netcat-openbsd --install netcat-traditional --install telnet --install busybox # disable console blanking, revert to eth0-like interface names, set tty0 as main tty instead of ttyS0 virt-customize -a debian-13-genericcloud-amd64.qcow2 --edit '/etc/default/grub:s/consoleblank=0(?! biosdevname)/consoleblank=0 biosdevname=0 net.ifnames=0/' \ --edit '/etc/default/grub:s/console=tty0 console=ttyS0,115200/console=ttyS0,115200 console=tty0/' \ --run-command 'update-grub' ## optional - enable autologin on tty1 and ttyS0 #virt-customize -a debian-13-genericcloud-amd64.qcow2 \ # --mkdir /etc/systemd/system/getty@tty1.service.d \ # --append-line '/etc/systemd/system/getty@tty1.service.d/override.conf:[Service]' \ # --append-line '/etc/systemd/system/getty@tty1.service.d/override.conf:ExecStart=' \ # --append-line '/etc/systemd/system/getty@tty1.service.d/override.conf:ExecStart=-/sbin/agetty --autologin root --noclear %I $TERM' \ # --mkdir /etc/systemd/system/serial-getty@ttyS0.service.d \ # --append-line '/etc/systemd/system/serial-getty@ttyS0.service.d/override.conf:[Service]' \ # --append-line '/etc/systemd/system/serial-getty@ttyS0.service.d/override.conf:ExecStart=' \ # --append-line '/etc/systemd/system/serial-getty@ttyS0.service.d/override.conf:ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud 115200,57600,38400,9600 %I $TERM' # apply the CloudStack cloud-init config # step 1 - generate the files echo "policy: enabled" > ds-identify.cfg cat << 'EOF' > 99_cloudstack.cfg disable_root: 0 system_info: default_user: name: root lock_passwd: false sudo: false groups: [] ssh_pwauth: true ssh_deletekeys: false datasource_list: ['ConfigDrive','CloudStack'] cloud_config_modules: - [set-passwords, always] - [ssh, always] datasource: CloudStack: max_wait: 20 timeout: 5 retries: 2 EOF # step 2 - put them in the guest virt-customize -a debian-13-genericcloud-amd64.qcow2 \ --upload ds-identify.cfg:/etc/cloud/ds-identify.cfg \ --upload 99_cloudstack.cfg:/etc/cloud/cloud.cfg.d/99_cloudstack.cfg # customise the /etc/issue banner similar to the SystemVMs cat << 'EOF' > etc-issue.txt __?.o/ Apache CloudStack Default Template ( )# https://cloudstack.apache.org (___(_) \S \n \l \4 \6 EOF # and put it in the guest - perhaps all these copy operations could be bundled together for speed virt-customize -a debian-13-genericcloud-amd64.qcow2 --upload etc-issue.txt:/etc/issue # the question of the default password "password" - there'll be cases where this template/VM gets exposed to the Internet, I reckon that we should deal with it this way: # - if the password has not been changed - we do this via comparison against the hash we saved at the beginning of the script - then no password based SSH allowed for root # - if the password has been changed - assuming by cloud-init - then we let root SSH in with the password, whatever that may be, assuming a secure one in comparison # - we achieve this via per-once cloud-init script which runs _after_ the password module cat << 'EOF' > root-ssh-pwlogin.sh #!/bin/sh default=$(cat /etc/cloud/default-root-hash) current=$(getent shadow root | cut -d: -f2) if [ -n "$current" ] && [ "$current" != "$default" ]; then printf 'PermitRootLogin yes\n' > /etc/ssh/sshd_config.d/10-root-sshpwlogin.conf systemctl restart ssh logger -t cloud-init "Default insecure password no longer in place, allowing root user in via SSH password." fi EOF chmod +x root-ssh-pwlogin.sh virt-customize -a debian-13-genericcloud-amd64.qcow2 --run-command 'mkdir -p /var/lib/cloud/scripts/per-once' \ --upload root-ssh-pwlogin.sh:/var/lib/cloud/scripts/per-once/root-ssh-pwlogin.sh # take care of guest tools - we build images for various hypervisor by means of --virt parameter, values = kvm, xen or vmware xe_url=https://github.com/xenserver/xe-guest-utilities/releases/download/v10.0.0/xe-guest-utilities_10.0.0-1_amd64.deb # we assume KVM if no virt parameter is passed to the script # wrap a streamOptimized vmdk into an OVA (OVF descriptor + manifest + disk) # usage: make_ova (expects .vmdk to exist) make_ova() { name=$1 src=$2 # only the top-level "virtual size:" line; child nodes (the qcow2's file node) # are indented in qemu-img's output and would otherwise match too capacity=$(qemu-img info "$src" | sed -n 's/^virtual size: .*(\([0-9]*\) bytes)$/\1/p') case $capacity in ''|*[!0-9]*) echo "could not determine virtual size of $src" >&2; exit 1 ;; esac vmdk_size=$(stat -c %s "$name.vmdk") cat > "$name.ovf" < Virtual disks Networks VM Network $name $name Other Linux 64-bit Virtual hardware Virtual Hardware Family 0 vmx-07 hertz * 10^6 1 virtual CPU 1 3 1 byte * 2^20 256 MB of memory 2 4 256 0 SCSI controller 0 3 lsilogic 6 0 IDE controller 0 4 5 0 Hard disk 1 ovf:/disk/vmdisk1 5 3 17 0 false CD/DVD drive 1 6 4 vmware.cdrom.remotepassthrough 15 EOF for f in "$name.ovf" "$name.vmdk"; do printf 'SHA256(%s)= %s\n' "$f" "$(sha256sum "$f" | cut -d' ' -f1)" done > "$name.mf" # the OVF descriptor must be the first file in the archive tar --format=ustar -cf "$name.ova" "$name.ovf" "$name.mf" "$name.vmdk" rm -f "$name.ovf" "$name.mf" "$name.vmdk" } # we assume KVM if no virt parameter is passed to the script virt=kvm while [ $# -gt 0 ]; do case $1 in --virt) virt=$2; shift ;; --virt=*) virt=${1#*=} ;; *) echo "unknown option: $1" >&2; exit 1 ;; esac shift done case $virt in kvm) set -- --install qemu-guest-agent ;; vmware) set -- --install open-vm-tools,linux-image-amd64 \ --run-command 'DEBIAN_FRONTEND=noninteractive apt-get purge -y linux-image-cloud-amd64 $(dpkg-query -W -f="\${Package}\n" "linux-image-*-cloud-amd64") && apt-get autoremove -y' ;; xen) set -- --run-command "wget -qO /tmp/xe.deb $xe_url && apt-get install -y /tmp/xe.deb && rm /tmp/xe.deb" ;; *) echo "usage: $0 --virt kvm|vmware|xen" >&2; exit 1 ;; esac base=debian-13-genericcloud-amd64.qcow2 name=debian-13-$virt work=$name.qcow2 # customize a per-hypervisor copy so the guest tools of one build # never end up in another cp "$base" "$work" virt-customize -a "$work" "$@" # virt-customize annoyingly sets a machine-id which is not ideal for a template, needs resetting virt-customize -a "$work" --run-command 'apt-get clean && apt-get autoremove -y' virt-customize -a "$work" --truncate /etc/machine-id virt-sparsify --in-place "$work" case $virt in kvm) # the customized qcow2 is the KVM template as is ;; xen) qemu-img convert -f qcow2 -O vpc -o subformat=dynamic,force_size "$work" "$name.vhd" bzip2 -f "$name.vhd" # CloudStack accepts .vhd.bz2 for XenServer rm -f "$work" ;; vmware) qemu-img convert -f qcow2 -O vmdk -o subformat=streamOptimized,adapter_type=lsilogic "$work" "$name.vmdk" make_ova "$name" "$work" rm -f "$work" ;; esac