OS: Arch
To simplify and speed up the process of VMs deployment on local machine I will use cloud init image. We can find them all around the place. For my tasks I need to use ubuntu 20.04 server.
Source of the image can be found: cloud-images
Dependencies:
- libvirt
- qemu
- virt-manager
- bridge-utils
- cloud-utils
sudo pacman -Syy
sudo pacman -S qemu libvirt virt-manager virt-viewer bridge-utils cloud-utils curl
sudo usermod -aG libvirt $(whoami)
sudo systemctl enable --now libvirtd
The standard path for libvirt to store the images is /var/lib/libvirt/images
.
Downloading latest focal version of ubuntu.
sudo curl -L https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img -o /var/lib/libvirt/images/focal-server-cloudimg-amd64.img
While configuring cloud-init we will need to provide ssh public keys
ssh-keygen -t rsa -b 4096 -C "yourmail@mail.com"
Save this file in ~/.ssh/ci_ubuntu
, where ci is for cloud init and ubuntu for user name
-
Create a directory
cloud-init-config
at/var/lib/libvirt/images
-
Enter the directory and create 2 files:
- cloud-config
( echo "#cloud-config users: - name: ubuntu gecos: Ubuntu sudo: ALL=(ALL) NOPASSWD:ALL groups: users, admin shell: /bin/bash ssh-authorized-keys: - $(cat <PATH_TO_YOUR_PUB_KEY>.pub) # Enable password authentication with the SSH daemon ssh_pwauth: False write_files: - path: /etc/netplan/01-netcfg.yaml content: | network: version: 2 ethernets: all: match: name: \"en*\" dhcp4: true" ) > cloud-config
- user-data
touch user-data
-
The last one - use cloud-localds to create iso image. Run in
cloud-init-config
directorycloud-localds ubuntu-cloud-init.iso cloud-config user-data
Using virt-manager:
- Create new virtual machine -> Import existing disk image -> -> Set your resources -> Set check box "Customize configuration before install" and select network (NAT)
- Add hardware -> Device type: CDROM -> Select or create custom image:
/var/lib/libvirt/images/cloud-init-config/ubuntu-cloud-init.iso
- Boot options -> Select CDROM and move it on top
- Begin installation
- Get your template IP.
sudo virsh list --all
sudo virsh domifaddr <your_template_vm_name>
- SSH to your template using cloud init credentials:
ssh -i ~/.ssh/ci_ubuntu ubuntu@YOUR_VM_IP_ADDRESS
- Update, install qemu agent
sudo agt-get udpate && apt-get install qemu-guest-agent -y
sudo systemctl enable qemu-guest-agent --now
- Clean your image
sudo cloud-init clean
- Delete your machid-id data
sudo truncate -s 0 /etc/machine-id
sudo rm /var/lib/dbus/machine-id
sudo ln -s /etc/machine-id /var/lib/dbus/machine-id
- Shutdown
sudo shutdown now
While it is a template - you can still run it which will lead to mailfunction. To prevent this - we will set privilages to 400
sudo chmod 400 /var/lib/libvirt/images/focal-server-cloudimg-amd64.img