#!/bin/bash # generates user data, meta data, and seed.iso for cloud init. for debian # requires the name of the vm as an argument # # osinfo-query os to list all available vm types to deploy. debian is debian12 # use no cloud and some other tools because cloud init does not work image_url="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-nocloud-amd64.qcow2" sshkeysdir="/root/k" scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #&& echo $scriptdir dir_path="/var/lib/libvirt/images/.image_store" src_file="${dir_path}/debian12.qcow2" config_dir="/var/lib/libvirt/images/.temp" user_data="${config_dir}/user-data" meta_data="${config_dir}/meta-data" seed_iso="${config_dir}/seed.iso" # note that cloud init specifically looks for "user-data" and "meta-data" in the seed.iso disk. do not deviate from the naming convention. if [ $# -ne 1 ]; then echo "Usage: $0 " exit 1 fi sudo mkdir -p "$dir_path" # make image store dir sudo mkdir -p "$config_dir" [ ! -f "$src_file" ] && echo "source image does not exist! downloading..." && sudo wget -O "$src_file" "$image_url" # First create a command that will generate all the ssh-inject options SSH_INJECT_OPTS=$(find ${sshkeysdir} -name "*.pub" -exec echo "--ssh-inject user:file:{}" \;) flag_file="${dir_path}/debian12_customized" # Check if customization has already been done if [ -f "$flag_file" ]; then echo "Image already customized, skipping customization..." else echo "Customizing image..." # to install virt customise tool sudo dnf install -y libguestfs libguestfs-tools libvirt virt-install virt-manager guestfs-tools export LIBGUESTFS_BACKEND=direct sudo -E virt-customize -a /var/lib/libvirt/images/.image_store/debian12.qcow2 \ --run-command 'useradd -m -s /bin/bash user' \ $SSH_INJECT_OPTS \ --update \ --install openssh-server,git \ --run-command 'echo "user ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers.d/user' \ --run-command 'sed -i "s/PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config' \ --run-command 'sed -i "s/#PasswordAuthentication yes/PasswordAuthentication no/" /etc/ssh/sshd_config' \ --run-command 'cd /home/user && git clone https://git.0nom.ch/keys && chown -R user:user keys && sudo -u user ./keys/add-ssh-keys.sh' \ --run-command 'tee -a /etc/bashrc > /dev/null << "EOF" LS_COLORS=$LS_COLORS:'\''di=38;5;135:ex=00;32:'\'' ; export LS_COLORS PS1='\''[\[\033[01;32m\]\u\[\033[00m\]@\h \[\033[38;5;135m\]\W\[\033[00m\]]\$ '\'' EOF' && \ # Create flag file after successful customization sudo touch "$flag_file" fi