Install Arch Linux in Virtual Machine (KVM/QEMU)

Student
Search for a command to run...

Student
No comments yet. Be the first to comment.
If you've ever faced the challenge of running graphical user interface (GUI) programs within a container, fear not! While it may seem like a daunting task at first, with this comprehensive guide, we'll walk through the process using Incus Container. ...

When working with containers, managing file sharing between the host system and container can often pose a challenge. However, there is an easy-to-use solution that simplifies this process using just one line of code! This blog post will guide you on...

Incus, a manager and hypervisor for system containers (LXC) and virtual machines (QEMU), is an excellent tool for managing and orchestrating your applications and services. It is a fork of LXD by the original maintainers. I found the documentation re...

Using ble.sh and Starship. Bash by itself is pretty basic. Syntax highlighting, auto-suggestion and history matching are pretty helpful features in any shell. Users might be worried about the bloat these features bring and add delay to the startup of...

The ability to run virtual machines (VMs) and docker containers is an integral part of today’s modern computing landscape. However, issues with internet connectivity inside these environments often cause significant inconvenience for users who depend...

Installing Arch Linux on a KVM/QEMU virtual machine is a fairly startightforward process. We will use command line approach in this post.
Here we use virt-install to create a virtual machine. Run the following as root:
virt-install -n arch \
--os-type=linux \
--os-variant=archlinux \
--ram=2048 \
--vcpus=4 \
--disk path=/var/lib/libvirt/images/arch.qcow2,bus=virtio,size=32 \
--graphics=spice \
--cdrom /path/to/image.iso \
--network default
Here we specify name for the virtual machine, which is arch.
Next we specify type and variant.
The amount of RAM is specified in megabytes.
We assign four virtual CPUs to the VM.
In disk path we have given the default location where the virtual machine image is stored.
Size is in gigabyes.
We have used spice for graphics. Alternatively, VNC can be used.
In cdrom we have provided the location of the boot media.
Finally we asked virt-install to use the default network profile.
Pressing Enter will start the creation of the virtual machine. When done, it will start virt-viewer with live media running.
This part is the same as any Arch Linux installation. We partition disks, run bootstrap script, generate locales, set timezone, hostname, install bootloader and optionally a desktop environment.
The virtual storage goes by /dev/sda. We partition disks using cfdisk. Select dos partition tabke since we are not using UEFI. Create just one partition and use it as root. Write then quit the program.
Now we format the partition.
mkfs.ext4 /dev/vda1
Now we are ready to install Arch Linux. Mount root partition on /mnt with mount /dev/vda1 /mnt. Then we run the bootstrap script that will create the necessary files and install the specified packages.
pacstrap /mnt base base-devel linux vim dhcpcd
Here we asked pacstrap to install Arch Linux on /mnt.
Then we specified the packages to be installed.
The meta package base contains a minimal package set that defines a basic Arch Linux installation. It includes basic utilities like glibc, bash, coreutils, networking tools, systemd, etc.
Next is the base-devel group. It contains tools to build many packages like gcc, make, binutils, etc.
linux is the kernel.
We require vim to configure various things. Any text editor will do.
dhcpcd will allow us to connect to the internet. Alternatively, networkmanager can be used.
This file tells which partitions to mount during boot. Instead of writing by hand, we use a tool to simplify things for us. Running the following
genfstab -U /mnt >> /mnt/etc/fstab
will generate a fstab file. We asked it to use /mnt partition. Any partition under /mnt will also be included, so if you created separate /home and /var partitions, they will also be included. The -U parameter tells genfstab to use UUID.
In this section we set timezones, locale and hostname.
Chroot into new root
arch-chroot /mnt
List timezones by running timedatectl list-timezones. Set timezone with
timedatectl set-timezone Asia/Kolkata
Replace Asia/Kolkata with your timezone.
Uncomment the desired locales in locale.gen
vim /etc/locale.gen
Save and exit then generate them by running
locale-gen
And set locale config
echo LANG=en_US.UTF-8 > /etc/locale.conf
export LANG=en_US.UTF-8
Replace en_US.UTF-8 with your locale.
Your virtual machine will be identified by this name on the network
echo arch-vm > /etc/hostname
Your computer will look here for resolving domain names before looking out on the internet. Put the following information in /etc/hosts:
127.0.0.1 localhost
::1 localhost
127.0.1.1 arch-vm
This will enable the virtual machine to connect to the internet
systemctl enable dhcpcd.service
Before we create a regular user, it becomes necessary to set a root password. Set it by running
passwd
Create a new user with
useradd -m -G wheel,storage,power -s /bin/bash <username>
-m tells useradd ro create a home directory for this user.
-G tells which groups this user will belong to.
-s specifies the login shell for this user.
Replace <username> with a username of your choice.
Set a password for this user with
passwd <username>
If working on command line is all you have to do, then working via SSH is more convenient than using Spice client (or VNC if you chose that). Install it
pacman -S openssh
and enable it
systemctl enable sshd.service
The instructions thus far will provide us a working bare minimum Arch Linux environment. Now it is time to install a bootloadet and finish the installation. We will go with grub.
pacman -S grub
grub-install /dev/vda
grub-mkconfig -o /boot/grub/grub.cfg
First we install grub package.
Then we install it on the device /dev/vda.
Next we create a configuration file. It can be modified at /etc/default/grub. Every time we modify the configuration, it must be generated with the last command for the changes to take place.
The installation is complete. Next time we boot, we will be dropped into a working Ach Linux environment.
Exit chroot.
exit
Unmount partition
umoount /mnt
And reboot
reboot
Wait for a minute and the virtual machine will be up. FInd the virtual machine's IP address on host machine (assuming arch is the name of your virtual machine)
virsh domifaddr arch
Save the IP address in your host's /etc/hosts
printf "<ip-address>\t<guest hostname>\n" | sudo tee -a /etc/hosts
Replace <ip-address> with the IP address of the virtual machine and <guest hostname> with the hostname you set while installing the virtual machine. It wuold be something like
printf "192.168.122.43\tarch-vm\n" | sudo tee -a /etc/hosts
While not necessary on a local network, it is generally a good habit to use key pair instead of password authentication.
Go to ~/.ssh and create a key pair.
cd ~/.ssh
ssh-keygen -t ed25519 -f arch-vm -C "arch linux virtual machine"
ssh-keygen generates keys.
-t specifies the type of the key. We chose Ed25519. Other popular types are EdDSA, RSA, DSA, etc.
-f specifies the filename of this key pair.
-C is used to put comment in the key.
This process will ask for a passphrase and generates a private key and a public key. The public key will have extension .pub.
Create a config file. This will tell SSH which key to use with respect to host. The file is named config. Put the following in the file
Host arch-vm
IdentityFile ~/.ssh/arch-vm
The private key is used to identify you. Anybody with your private key will be impersonate you so keep it safe.
Now copy the public key over to your virtual machine. It can be achieved by programs like scp or rsync. However, we will use ssh-copy-id to conveniently put our key while abstracting most parts.
ssh-copy-id -f -i ~/.ssh/arch arch@arch-vm
It is assuming arch is the username of the normal user of the virtual machine.
Now log in by running
ssh arch@arch-vm
Modify /etc/ssh/sshd_config to only use public key for authentication.
Change PubkeyAuthentication to yes
Change PasswordAuthentication to no
Change X11Forwarding to yes
Now restart sshd for changes to take place.
systemctl restart sshd.config
Exit and try logging in again. This time it should ask for passphrase.
This part is entirely optional if you wish to use GUI. In this post, we will install MATE desktop environment.
pacman -S mate mate-extra
The desktop environment can be started by running exec mate-session. If you want to use xinit then put the command in ~/.xinitrc. Then install xorg-xinit package. Start it with startx.
Alternatively you can use a display manager like GDM, LightDM, SDDM. etc. Install sddm, enable and start it
systemctl enable sddm.service
systemctl start sddm.service
If you wish to run GUI programs over X11 forwarding, a desktop environment might be overkill. In that case just install xorg, xorg-server. To use X11 forwarding, log in via ssh with
ssh -Y arch@arch-vm