Skip to content

Instantly share code, notes, and snippets.

@kimdcottrell
Last active October 15, 2023 20:51
Show Gist options
  • Save kimdcottrell/b249371169a5110061b3da4a5e20553a to your computer and use it in GitHub Desktop.
Save kimdcottrell/b249371169a5110061b3da4a5e20553a to your computer and use it in GitHub Desktop.
A simple makefile for using qemu/kvm to run an ubuntu lunar 23.04 server image on a host ubuntu 23.04 box.
all: run-ubuntu-image
.PHONY: check-kvm-modules
check-kvm-modules: ## Check if kvm modules are loaded
lsmod | grep kvm
# INFO: There should be kvm_intel or kvm_amd. Use whatever is on your machine.
# Setup is done via `sudo modprobe kvm_intel` or `sudo modprobe kvm_amd`
.PHONY: check-virtualization-support
check-virtualization-support: ## Check if virtualization is supported on your machine
kvm-ok
# INFO: This should return "KVM acceleration can be used"
.PHONY: install-pkgs
install-pkgs: ## Install the mountain of packages needed to run qemu/kvm
sudo apt update
sudo apt install qemu qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager cpu-checker
.PHONY: install-ubuntu-image
install-ubuntu-image: ## Install ubuntu 23.04 server image
wget -P ~/.kvm https://releases.ubuntu.com/lunar/ubuntu-23.04-live-server-amd64.iso
qemu-img create ~/.kvm/ubuntu-lunar-k8s.img -f qcow2 12G
qemu-system-x86_64 -m 4000 -accel kvm -vga virtio -usb -drive file=~/.kvm/ubuntu-lunar-k8s.img,if=virtio -cpu host -cdrom ~/.kvm/ubuntu-23.04-live-server-amd64.iso
.PHONY: run-ubuntu-image
run-ubuntu-image: ## Run the ubuntu image
qemu-system-x86_64 -m 4000 -accel kvm -vga virtio -usb -drive file=~/.kvm/ubuntu-lunar-k8s.img,if=virtio -cpu host -netdev user,id=net0,hostfwd=tcp::8022-:22,hostfwd=tcp::8080-:80 -device virtio-net-pci,netdev=net0
.PHONY: setup-host-permissions
setup-host-permissions: ## Setup permissions for the current user so the vm works as you'd expect
sudo adduser $(whoami) libvirt
sudo adduser $(whoami) kvm
help: ## Show help
@echo Please specify a build target. The choices are:
@grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@kimdcottrell
Copy link
Author

kimdcottrell commented Oct 15, 2023

To get setup:

make check-virtualization-support
make install-pkgs
make setup-host-permissions
make check-kvm-modules

If all looks good:

make install-ubuntu-image

Do the install setup stuff in your VM. The post-install image will be saved in ~/.kvm/ubuntu-lunar-k8s.img. Exit out when done.

Now start the image with some network stuff included so you can ssh and visit websites hosted in your vm on your host machine's browser:

make run-ubuntu-image

SSH needs a bit of configuring. Run this:

netstat -tulpn | grep 8022

You should see an IP of either 0.0.0.0 or 127.0.0.1. Add that ip address to your /etc/hosts with a domain name. e.g.

0.0.0.0	host.local

Now you should be able to ssh like so:

ssh the-user-you-setup-in-the-vm@host.local -P 8022

Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment