provision/kvm/vclone
2024-10-17 15:04:16 -07:00

32 lines
970 B
Bash
Executable file

#!/bin/bash
if [[ $# -ne 2 ]]; then
echo "USAGE: $0 <source> <dest>"
exit 0
else
orig="$1"
dest="$2"
fi
sudo virt-clone --original "${orig}" --name "${dest}" --auto-clone
sudo virt-sysprep \
-d "${dest}" \
--operations defaults,-ssh-userdir \
--hostname "${dest}" \
--edit /etc/hosts:"s/${orig}/${dest}/" \
--edit /etc/hostname:"s/${orig}/${dest}/" \
--edit /etc/motd:"s/${orig}/${dest}/" \
--firstboot-command 'dpkg-reconfigure openssh-server' \
--firstboot-command 'systemctl restart sshd'
getLinkLocalAddress() {
local macAddress=$(sudo grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' /etc/libvirt/qemu/${dest}.xml)
printf "%02x%s" $(( 16#${macAddress:0:2} ^ 2#00000010 )) "${macAddress:2}" \
| sed -E -e 's/([0-9a-zA-Z]{2})*/0x\0|/g' \
| tr -d ':\n' \
| xargs -d '|' \
printf "fe80::%02x%02x:%02xff:fe%02x:%02x%02x"
}
printf "The link-local IP address of %s is:\n %s\n" "${dest}" "$(getLinkLocalAddress)"