×

How to run a Windows virtual machine on Incus on Linux – Mi blog lah!

How to run a Windows virtual machine on Incus on Linux – Mi blog lah!


Incus is a manager for virtual machines and system containers.

A virtual machine (VM) is an instance of an operating system that runs on a computer, along with the main operating system. A virtual machine uses hardware virtualization features for the separation from the main operating system. With virtual machines, the full operating system boots up in them. Therefore, it is possible to run Windows in a virtual machine, even if your host is Linux.

A system container is an instance of an operating system that also runs on a computer, along with the main operating system. A system container, instead, uses security primitives of the Linux kernel for the separation from the main operating system. You can think of system containers as software virtual machines. The system containers reuse the running Linux kernel of the host, therefore you can only have Linux system containers, any Linux distribution.

In this post we are going to see how to create an Incus virtual machine with Windows.

Prerequisites

  1. You should have a system that runs Incus.
  2. A Windows ISO file or enough bandwidth to download one.

Cheat sheet

This is a summary of the commands. You are supposed to consult this section after you thoroughly read the post.

sudo snap install distrobuilder --classic
sudo distrobuilder repack-windows Win11_23H2_English_x64v2.iso Win11_23H2_English_x64v2.incus.iso
incus init win11vm --empty --vm
incus config device override win11vm root size=55GiB
incus config set win11vm limits.cpu=4 limits.memory=6GiB
incus config device add win11vm vtpm tpm path=/dev/tpm0
incus config device add win11vm install disk source=/home/myusername/Downloads/Win11_23H2_English_x64v2.incus.iso boot.priority=10
incus start win11vm --console=vga
incus console win11vm --type=vga
incus config device remove win11vm install

Downloading a Windows ISO file

You can download Windows ISO files from Microsoft, at https://www.microsoft.com/en-us/software-download/ In my case I am selecting Windows 11.

When you select the version of Windows, you are asked the type of file to download. Select the Windows Disk Image (ISO). Then, select your preferred product language. Finally, click to download the ISO file. It is about 6GB in size.

The file has been downloaded. In my case, the filename was Win11_23H2_English_x64v2.iso.

Preparing the Windows ISO file

The Windows ISO file needs to be prepared before it is used by Incus as a virtual DVD image to boot the virtual machine. When Incus starts up the empty virtual machine for the first time, it will find the ISO file attached as a virtual DVD disk, and boot it. By booting the virtual DVD disk, the installation process of Windows will start.

To prepare the original Windows ISO file, we use distrobuilder. First, we install it.

$ sudo snap install distrobuilder --classic
distrobuilder 3.0 from Stéphane Graber (stgraber) installed
$ 

Then, we can run distrobuilder to repack the ISO file in a special way that Windows ISO files require. The following procedure took about 2 minutes on my computer. The new, prepared, ISO file has been named Win11_23H2_English_x64v2.incus.iso and it is for use with Incus only.

$ sudo distrobuilder repack-windows Win11_23H2_English_x64v2.iso Win11_23H2_English_x64v2.incus.iso 
[sudo] password for myusername:  
INFO   [2024-01-30T02:23:01+02:00] Mounting Windows ISO                         
mount: /var/cache/distrobuilder.3138255837/source: WARNING: source write-protected, mounted read-only.
INFO   [2024-01-30T02:23:01+02:00] Downloading drivers ISO                      
INFO   [2024-01-30T02:24:45+02:00] Mounting driver ISO                          
...
$

The ISO file is prepared, we can now work on the Incus VM for Windows. The ISO preparation takes about 5 minutes.

Creating the Windows VM on Incus

We are going to create the VM through special steps.

  1. We initialize the VM. It is a VM because --vm, it is called win11vm in our case, and it is --empty for now.
  2. We override the default size of the disk of the VM to 60GB. The default size for the disk in Incus 10GB which is not enough for the installation of Windows. At the end of the installation, Windows use about 12GB of space. See the Troubleshooting section to figure out how much space you can afford to use or how to choose an older version of Windows because you do not have enough space for the VM.
  3. We allocate 4 CPU cores for the VM, and 8GB of RAM memory. Specify accordingly for your system. The minimum requirements for Windows 11 is 2 CPU cores and 4GB RAM.
$ incus init win11vm --empty --vm
Creating win11vm
$ incus config device override win11vm root size=55GiB
Device root overridden for win11vm
$ incus config set win11vm limits.cpu=4 limits.memory=6GiB
$ incus config device add win11vm vtpm tpm path=/dev/tpm0
Device vtpm added to win11vm
$ incus config device add win11vm install disk source=/home/myusername/Downloads/Win11_23H2_English_x64v2.incus.iso boot.priority=10
Device install added to win11vm
$ 

Now we are ready to boot the VM with the attached ISO image that will appear as a DVD disk. From there on, the installation of Windows will commence.

Booting the Windows virtual machine

Initially, the VM is in a stopped state. We are going to start it and at the same time request to open a window that shows the console of the VM. This helps us to perform the initial installation. If you get a message that the VM is already running, then see the next section on how to reconnect to the VM.

$ incus start win11vm --console=vga

Reconnecting to the Windows virtual machine

If you close the Windows virtual machine window (pun not intended), then the window closes but the VM continues to run. In that case, you can reconnect to the running Windows VM by running the incus console command with the parameter --type=vga. If you get a message that the VM is stopped, then see the previous section on how to start the VM.

$ incus console win11vm --type=vga

Removing the virtual Windows DVD ISO

The virtual Windows DVD ISO is attached to the Incus instance. When you boot the Windows Incus instance, you are asked to press any key to boot from the DVD and there’s a small delay before continuing. If you have already installed Windows, you can remove the virtual Windows DVD ISO from the Incus instance with the following command. We are using the incus config command, subcommand device remove, on the instance win11vm, for the device called install.

$ incus config device remove win11vm install
Device install removed from win11vm
$ 

Screenshots

When you first launch the Incus instance with Windows, you are booting from the attached virtual Windows DVD. As it is common with the booting process, you are asked to press any key in order to boot from the DVD. Otherwise, the system will boot from the VM hard disk, which is empty. Therefore, you need to click inside the window and press any key in order to get the attached virtual Windows DVD to boot. If you do not make it on time as in the screenshot, click on the menu Send Key, then Ctrl+Alt+Del to send the signal to reboot the VM.

When the VM is booting, the operating system shows an image that is provided by the UEFI environment. On your desktop when you start Windows, it would show the logo of the manufacturer of the motherboard.

In this case, Incus is showing the name of the company of the packager of Incus that I am using.

Launching the Incus instance, which is a VM running Windows 11.

Some other random screenshot.

Summary

We show how to

  1. how to download the ISO file from Microsoft,
  2. how to prepare the ISO file so that it can be used by Incus,
  3. setup an Incus VM to install Windows,
  4. how to start or reconnect to the VM.
  5. how to create an Incus profile with the configuration
  6. and below some troubleshooting issues.

Bonus material – Incus profile for Windows

When you run many Windows VMs, you might want to simplify the process of setting up the Incus instance. For example, the allocated amount of disk space, how many CPU cores and amount of RAM memory.

You can create Incus profiles to describe sets of configuration. When you create the Incus instance for Windows, specify the profile and you avoid repeating the same configuration steps.

To do so, we make a copy of the default profile into a new name, let’s call it windows. Then, we add configuration to that profile.

$ incus profile copy default windows
$ incus profile show test
config: {}
description: Default Incus profile
devices:
  eth0:
    name: eth0
    network: lxdbr0
    type: nic
  root:
    path: /
    pool: default
    type: disk
name: test
used_by: []
$ incus profile set windows limits.cpu=4
$ incus profile set windows limits.memory=6GiB
$ incus profile set windows raw.qemu="-device intel-hda -device hda-duplex"
$ incus profile device add windows vtpm tpm path=/dev/tpm0
$ incus profile device set windows root size=55GB
$ incus profile set windows --property description="Windows profile 4CPU, 6GB RAM, 55GB space"
$ incus profile show windows
config:
  limits.cpu: "4"
  limits.memory: 6GiB
  raw.qemu: -device intel-hda -device hda-duplex
description: Windows profile 4CPU, 6GB RAM, 55GB space
devices:
  eth0:
    name: eth0
    network: lxdbr0
    type: nic
  root:
    path: /
    pool: default
    size: 55GB
    type: disk
  vtpm:
    path: /dev/tpm0
    type: tpm
name: test
used_by: []
$ incus profile list
+---------+--------------------------------------------------------------------+---------+
|  NAME   |                            DESCRIPTION                             | USED BY |
+---------+--------------------------------------------------------------------+---------+
| default | Default Incus profile                                              | 42      |
+---------+--------------------------------------------------------------------+---------+
| gui     | GUI Wayland and xWayland profile with pulseaudio, shifting enabled | 3       |
+---------+--------------------------------------------------------------------+---------+
| windows | Windows profile 4CPU, 6GB RAM, 55GB space                         | 0       |
+---------+--------------------------------------------------------------------+---------+
$ 

Note that if you use many versions of Windows, you can create several profiles for each version. Windows 11 requires that there are available 52GB of disk space but Windows 10 can fit in much less space.

Troubleshooting

Error: This revision of snap “distrobuilder” was published using classic confinement…

You are trying to install the distrobuilder snap package. It does not install.

distrobuilder requires full access to your system because it uses loop devices, etc. Snap packages are confined. Therefore, by using the classic confinement, distrobuilder does not have any restrictions. To accept the classic confinement, you need to specify it when installing the package. Therefore, install distrobuilder by running the following command.

$ sudo snap install distrobuilder --classic
distrobuilder 3.0 from Stéphane Graber (stgraber) installed
$ 

Error: You must be root to run this tool

When you run the distrobuilder, you get the error message that you must be root to run the tool.

Indeed, you need elevated permissions to run distrobuilder. Run it either as root, or pre-pend sudo when running it.

Error: Failed add validation for device “install”: Missing source path for disk “install”

You are trying to attach the ISO image to an Incus VM and it fails.

Double-check the path to the ISO file. If you copy-pasted the command, you would need to update the path.

Error: The instance is already running

You are trying to start an Incus VM and you get the message that the instance is already running.

In that case, you should reconnect to the VM, using the following command.

$ incus console win11vm --type=vga

Error: Instance is not running

You are trying to reconnect to an Incus VM and you get the message that the instance is not running.

In that case, you should start the instance, using the following command.

$ incus start win11vm --console=vga

How much space is available in Incus to use for my VM?

First we find the name of the Incus storage pool. Then, we ask Incus to show the free space in that storage pool. In this case, 60GB is used by all instances and there’s still plenty of space to go.

$ incus storage list
+---------+--------+---------+-------------+---------+---------+
|  NAME   | DRIVER | SOURCE  | DESCRIPTION | USED BY |  STATE  |
+---------+--------+---------+-------------+---------+---------+
| default | zfs    | default |             | 65      | CREATED |
+---------+--------+---------+-------------+---------+---------+
$ incus storage info default
info:
  description: ""
  driver: zfs
  name: default
  space used: 98.65GiB
  total space: 512.22GiB
used by:
...
$ 

In this example, the total space is about 500GB and we have used only about 100GB. Plenty of free space for VMs.

Which version of Windows should I use in the Incus VM?

If you want a VM with Windows and you are not particularly interested in a specific version of Windows, I suggest to opt for Windows 10. Unlike Windows 10, Windows 11 23H2 require a disk partition of at least 52GB of space and they refuse to install if you do not provide enough space. In contrast, Windows 10 will happily install with less space, like 32GB of disk space.

When you install Windows 11, it takes up about 18GB of disk space. When you install Windows 10, it takes up about 12GB of disk space.



Source link