Last active
July 1, 2024 13:23
-
-
Save dosaboy/3b9681198c5c2e934a547786706d4b41 to your computer and use it in GitHub Desktop.
create_vm.sh
This file contains 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 -eux | |
ARCH=${1:-arm64} | |
IMAGES=/home/ubuntu/images | |
URL=https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-${ARCH}.img | |
LIBVIRT_IMAGES=/var/lib/libvirt/images | |
if ! [[ -d $LIBVIRT_IMAGES ]]; then | |
sudo apt install -y virtinst libvirt-daemon-system cloud-image-utils | |
sudo usermod -a -G libvirt-qemu $USER | |
newgrp libvirt-qemu | |
fi | |
sudo chown $USER:$USER $LIBVIRT_IMAGES | |
if ! [[ -e ${IMAGES}/`basename $URL` ]]; then | |
mkdir -p ${IMAGES} | |
wget -O ${IMAGES}/`basename $URL` $URL | |
sudo cp ${IMAGES}/`basename $URL` ${LIBVIRT_IMAGES} | |
fi | |
for NAME in testvm{1..2}; do | |
ROOT_IMG=${LIBVIRT_IMAGES}/${NAME}.img | |
SEED_IMG=${LIBVIRT_IMAGES}/${NAME}-seed.img | |
sudo rm -f ${ROOT_IMG} ${SEED_IMG} | |
cat <<- EOF > user-data.${NAME} | |
#cloud-config | |
user: ubuntu | |
password: ubuntu | |
hostname: ${NAME} | |
ssh_import_id: [hopem, joalif] | |
chpasswd: { expire: False } | |
ssh_pwauth: True | |
users: | |
- name: ubuntu | |
sudo: ALL=(ALL) NOPASSWD:ALL | |
packages: | |
- arp-scan | |
- ipcalc | |
- iperf3 | |
apt_get_upgrade_subcommand: "dist-upgrade" | |
package_upgrade: True | |
manage_etc_hosts: True | |
EOF | |
touch meta-data.${NAME} | |
cloud-localds ${SEED_IMG} user-data.${NAME} meta-data.${NAME} | |
qemu-img create -F qcow2 -b ${LIBVIRT_IMAGES}/`basename $URL` -f qcow2 ${ROOT_IMG} 30G | |
virsh destroy ${NAME} || true | |
virsh undefine ${NAME} --nvram || true | |
virt-install \ | |
--name=${NAME} \ | |
--osinfo=ubuntujammy \ | |
--connect=qemu:///system --ram=8192 --cpu host-passthrough --vcpus=8 --hvm \ | |
--virt-type=kvm \ | |
--boot hd \ | |
--graphics vnc --noautoconsole --os-type=linux --accelerate \ | |
--disk=${ROOT_IMG},bus=virtio,sparse=true \ | |
--disk=${SEED_IMG},bus=virtio,format=raw \ | |
--network=network=default,model=virtio \ | |
--print-xml > domain.xml | |
virsh define domain.xml | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment