Skip to content

Instantly share code, notes, and snippets.

@reox
Last active March 31, 2018 20:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reox/8b0f44d3bfa7bcccf8367e1a514de33d to your computer and use it in GitHub Desktop.
Save reox/8b0f44d3bfa7bcccf8367e1a514de33d to your computer and use it in GitHub Desktop.
Create a new Debian container for KVM and import it using virt-install
#!/bin/bash
set -e
# Name of the VM
IMAGE=some-fancy-name
# IP address for VM
IP="192.168.122.30"
MIRROR=http://mirror.hetzner.de/debian/packages
PACKAGES="openssh-server htop vim"
PASSWORD=$(pwgen 20 1)
# Generate a new mac address for this machine
MAC="52:54:00$(echo "$IMAGE" | md5sum | head -c 6 | sed -e 's/\(..\)/:\1/g')"
echo "[new kvm] set MAC to $MAC, set IP to $IP"
virsh net-update default add ip-dhcp-host "<host mac='$MAC' name='$IMAGE' ip='$IP' />" --live --config
vmdebootstrap \
--verbose \
--grub \
--image=/srv/kvm/$IMAGE.img \
--size=40G \
--mirror="$MIRROR" \
--distribution=buster \
--hostname=$IMAGE \
--enable-dhcp \
--user="vmmanage/$PASSWORD" \
--package="$PACKAGES" \
--lock-root-password \
--systemd-networkd \
--sudo \
--convert-qcow2 \
--sparse
virt-install \
-n $IMAGE \
--memory=4096 \
--vcpus=4 \
--disk /srv/kvm/$IMAGE.img,device=disk,bus=virtio,format=qcow2 \
-w network=default,model=virtio,mac="$MAC" \
--noautoconsole \
--autostart \
--os-variant debian \
--import
echo "The password is $PASSWORD"
echo "This VM should be available at:"
echo "ssh vmmanage@$IP"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment