Install Arch Linux on Virtual Machine

cd .. || cd

March 2, 2025 · 5 mins · Robertus Chris

Table of Contents

A Brief Intro

In this post, we’ll install arch linux on virtual machine with QEMU and add a shared directory between host machine and virtual machine.

Before we start, make sure to install QEMU on your system. If your system using arch linux too, we can install qemu-base and tigervnc package. tigervnc package for viewing the virtual machine from QEMU. We will be using SeaBIOS, which is legacy BIOS implementation and the default BIOS on QEMU (at the time of writing this post).

This post assume that you use ext4 filesystem format. If you use another file format, like btrfs, you might need to use different configuration.

QEMU Disk Image

The first thing we need to do is to create a disk image. We can do that with this command:

qemu-img create -f qcow2 <image-path> 15g

Installation

The first thing we need to do is download the arch linux ISO on the download page .

After that, we can use this command:

qemu-system-x86_64 -enable-kvm -cdrom <path-to-arch-linux-ISO>.iso -boot
order=d -drive file=<image-path> -m 2g

Mostly we can follow the arch linux installation guide , but be careful of the partition. Because we will use the default QEMU BIOS, which is a legacy BIOS, we need to partition the drive with legacy BIOS in mind. We can check on the partition example layout on arch wiki . For this post, we’ll use BIOS/GPT layout to not deviate too much from the UEFI/GPT layout.

If after running the command above, the virtual machine is not automatically appear, we can use command:

vncviewer :<port>

Make sure to install tigervnc or something similar before running the command. We can get the port from the QEMU command above. For example:

VNC server running on ::1:5900

5900 is the port we want.

After inside the virtual machine, you can follow the usual arch linux installation guide. To be able to use host machine internet connection, we might want to install dhcpcd in the pacstrap step, like this:

pacstrap -K /mnt base linux linux-firmware dhcpcd

and enable it after arch-chroot like this:

systemctl enable dhcpcd

The important part is the drive partition, so we’ll skip into the partition part.

Personally, i don’t really need swap on my virtual machine so in this post we’ll only make 2 partition:

We can use cfdisk /dev/<drive> to partition our drive. To look what our drive name is, we can use lsblk command. For example:

cfdisk /dev/sda

After the partition, we need to format the root partition to ext4 with this command:

mkfs.ext4 /dev/<root-partition>

for example:

mkfs.ext4 /dev/sda2

After that, we can mount the root partition with this command:

mount /dev/<root-partition> /mnt

for example:

mount /dev/sda2 /mnt

After that we can follow the rest of the arch linux installation guide until we need to install the bootloader.

For this post, we’ll use grub2 as our bootloader. First, we need to install grub with this command (after running arch-chroot):

pacman -S grub

After grub installed, we can use this command to install the bootloader:

grub-install --target=i386-pc /dev/<drive-name>

keep in mind that we need to provide the drive name, not the partition name. For example:

grub-install --target=i386-pc /dev/sda

After that, we need to generate the grub config with this command:

grub-mkconfig -o /boot/grub/grub.cfg

To run the virtual machine, we can use this command:

qemu-system-x86_64 -enable-kvm -nic user,hostfwd=tcp::2222-:22 -m 2g -smp cores=4,cpus=4 <image-path>

If we want to add shared directory between virtual machine and host machine, we can use -fsdev and -device like this:

qemu-system-x86_64 -enable-kvm -nic user,hostfwd=tcp::2222-:22 -fsdev local,id=fs1,path=<host-shared-directory-path>,security_model=none -device virtio-9p-pci,fsdev=fs1,mount_tag=<mount_tag> -m 2g -smp cores=4,cpus=4 <image-path>

and then add to /etc/fstab like this:

...
<mount_tag> <mounting-path-on-virtual-machine> 9p uid=xxxx,gid=xxxx,trans=virtio 0 0

Let’s say we have <mount_tag> as shared_directory and we want to mount it on /home/user/shared. Also, to get the uid and gid we can use id <user>, for example id bruhtus. Let’s assume the uid and gid of the user is 1000. We can use the command like this:

qemu-system-x86_64 -enable-kvm -nic user,hostfwd=tcp::2222-:22 -fsdev local,id=fs1,path=<host-shared-directory-path>,security_model=none -device virtio-9p-pci,fsdev=fs1,mount_tag=shared_directory -m 2g -smp cores=4,cpus=4 <image-path>

and in /etc/fstab like this:

shared_directory /home/user/shared 9p uid=1000,gid=1000,trans=virtio 0 0

Alright, that’s it. See you next time!

Side Note

If we want to use shutdown or reboot command from non-root user, we can install polkit package, like this:

pacman -S polkit

We can also ssh into the virtual machine from host machine. First we need to install openssh on virtual machine and host machine, don’t forget to start sshd service. And then, we create ssh key with this command:

ssh-keygen -t rsa -f ~/.ssh/qemu

(we can change the name qemu with something else).

After that, we can use this command to copy the ssh key into the virtual machine:

ssh-copy-id -i ~/.ssh/qemu.pub -p 2222 $USER@127.0.0.1

please keep in mind that we need to have the same username on the host machine and virtual machine. If you use different username on your virtual machine, you can change $USER with the username on your virtual machine.

To delete the hashed key of virtual machine from ssh known_hosts file, we can use this command:

ssh-keygen -R '[127.0.0.1]:2222'

References