The last ISO installer version of Archlinux that supports 32-bit is [2017.02.01](https://www.archlinux.org/releng/releases/2017.02.01/). Later versions of the archisos don't work on the v86 emulator because the installer only supports x86_64, not x86 anymore. For existing Archlinux installations, updates and patches will be done until somewhere around 2018.
In the future the community might come up with an alternative distribution based on Archlinux to maintain support for x86. At this point in time [archlinux32](https://mirror.archlinux32.org) seems to work.
Installing the ISO by hand takes a long time if you intend to recreate the image many times. There are various reasons why you might want to do this more than once. For example: because the emulator is slow you might want to compile any new software release in QEMU which is much faster and then use the resulting image in v86 instead of making the emulator compile the software. Another reason is that the build progress potentially takes long and if you want to do automated builds in parallel to find out what configurations do and don't work you can just throw more computing power at the problem in order to solve it. This example requires that you have `packer`, `qemu` and `kpartx` installed.
### Creating a packer template
[Packer](https://www.packer.io/docs/builders/qemu.html) is a tool that lets you boot an ISO in any of multiple emulators (so QEMU in our case) and send pre-scripted keystrokes to bootstrap and SSH server. Once the SSH connection is established a script can be started for further provisioning.
Create a template for automating the base installation
You can tweak the options a bit to match your situation. For debugging you can set `headless` to `false`. That will show you the vnc instead of running the `boot_command` in the background. For a `base` pacstrap using a 1.5G disk should be sufficient. The `raw` disk format is important. v86 does not read qcow2 images, only raw disk images. If your system does not support kvm (the default accelerator), you can add `"accelerator": "none"` to the settings. Other accelerator options can be found [here](https://www.packer.io/docs/builders/qemu.html#accelerator).
After gaining SSH connectivity to the VM, packer will run the `scripts/provisioning.sh` script in the guest.
### Creating the Archlinux installation script
Create a script for your Archlinux installation. This runs in the ISO booted Archlinux environment, so you need to partition, pacstrap and install a bootloader.
```sh
mkdir -p packer/scripts
### Write your own or copy paste the example below
vim packer/scripts/provision.sh
```
An example script to install Archlinux with the root mounted using the 9p network filesystem:
```sh
#!/bin/bash
echo "Creating a GPT partition on /dev/sda1"
echo -e "g\nn\n\n\n\nw" | fdisk /dev/sda
# In case you might want to create a DOS partition instead. It doesn't really matter.
#echo "Creating a DOS partition on /dev/sda1"
#echo -e "o\nn\np\n1\n\n\nw" | fdisk /dev/sda
echo "Formatting /dev/sda1 to ext4"
mkfs -t ext4 /dev/sda1
echo "Mounting new filesystem"
mount -t ext4 /dev/sda1 /mnt
echo "Create pacman package cache dir"
mkdir -p /mnt/var/cache/pacman/pkg
# We don't want the pacman cache to fill up the image. After reboot whatever tarballs pacman has cached are gone.
echo "Mount the package cache dir in memory so it doesn't fill up the image"
mount -t tmpfs none /mnt/var/cache/pacman/pkg
# Install the Archlinux base system
echo "Performing pacstrap"
pacstrap -i /mnt base --noconfirm
echo "Writing fstab"
genfstab -p /mnt >> /mnt/etc/fstab
# When the Linux boots we want it to automatically log in on tty1 as root
echo "Chrooting and bootstrapping the installation"
arch-chroot /mnt bash bootstrap.sh
umount -R /mnt
```
With the packer template and the script you have enough to create an image that can be booted by v86. But because this example script installs an Archlinux that wants to mount root over the network with 9p, we need to host that filesystem first. If you do not want to use 9p, you can just run `(cd packer && packer build -force template.json)` to build the image.
### Creating the 9p filesystem
Now that we have an image that contains a filesystem, we can convert that filesystem into something we can host on the webserver together with the v86 library.
To do so, we need to mount the image once and create a json mapping of the filesystem. This can be done with the [fs2json](https://github.com/copy/fs2json) python script.
An example repository with these scripts can be found [here](https://github.com/vdloo/archlinux-v86-builder)
### Using the created artifacts in v86
Now that we have everything we need to host a server that serves an Archlinux environment over the network.
Create a checkout of v86 and run `make build/libv86.js`. Then edit `examples/arch.html` and replace the `hda` and `filesystem` section with something like this:
```sh
hda: {
url: "http://localhost:8000/images/arch.img",
async: true, # For this option we need to run a webserver that supports the Range header
size: 1.5 * 1024 * 1024 * 1024, # This needs to be the size of the raw disk.
# See the `disk_size` item in the packer template.
},
filesystem: {
baseurl: "http://localhost:8000/arch/",
basefs: "http://localhost:8000/images/fs.json",
},
```
Then copy the created image, filesystem and json file to the v86 directory:
Next, we need a webserver that supports the Range header. For example [this extension of the SimpleHTTPServer](https://github.com/smgoller/rangehttpserver).
Now that the webserver is running, point your browser to `http://localhost:8000/examples/arch.html`. Wait for the Linux to boot. When the system is up, click 'Save state to file'. Your browser will download a `v86state.bin` file. Copy that file to `/your/v86/dir/images`. You can then edit `examples/arch.html` again and add a 'state' key to the `V86Starter` array.
If you refresh `http://localhost:8000/examples/arch.html` you will see that the state is restored instantly and all required files are loaded over the network on the fly.