Setting up a Windows virtual machine using virsh, the command-line interface for managing virtual machines (VMs) with libvirt, involves several steps. Here’s a step-by-step guide to help you through the process.
Prerequisites
- Ensure that you have libvirt and QEMU/KVM installed.
- Make sure virtualization is enabled in your BIOS/UEFI settings.
- Download a Windows ISO file. You can obtain an official Windows ISO from Microsoft.
Step 1: Create a Virtual Machine
-
Create a Storage Volume: First, create a storage volume for the Windows VM. You can use the virsh vol-create-as command to create a disk image. Here's an example of creating a 50 GB disk image named windows_disk.qcow2 in the default storage pool:
virsh vol-create-as default windows_disk.qcow2 50G --format qcow2
Adjust the size and format as needed.
-
Create an XML Definition: Next, you need to create an XML file that defines the VM. You can create a text file, e.g., windows_vm.xml, with the following content, adjusting parameters as needed:
<domain type='kvm'>
<name>windows-vm</name>
<memory unit='KiB'>2097152</memory>
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-3.1'>hvm</type>
<boot dev='cdrom'/>
<boot dev='hd'/>
</os>
<devices>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/windows_disk.qcow2'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/path/to/your/windows.iso'/>
<target dev='hda' bus='ide'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</disk>
<interface type='network'>
<mac address='52:54:00:xx:xx:xx'/>
<source network='default'/>
<model type='virtio'/>
</interface>
</devices>
</domain>
Notes:
- Replace
/path/to/your/windows.iso with the actual path to your Windows ISO file.
- The
memory element is in KiB (e.g., 2 GB = 2097152 KiB).
- The
mac address should be a unique value.
-
Define the VM: After creating the XML file, you need to define the VM using the virsh define command:
virsh define windows_vm.xml
-
Start the Virtual Machine: Start the VM using:
virsh start windows-vm
-
Access the Virtual Machine Console: Use virsh console to access the VM's console:
virsh console windows-vm
Step 2: Install Windows
Once you access the virtual machine console, the Windows installer should start from the ISO. Follow the on-screen instructions to install Windows:
- Select the language and other preferences on the first screen.
- Proceed with the installation, and when you reach the screen to choose where to install Windows, select the disk (the storage volume you created).
- Complete the installation process by following the prompts.
Step 3: Finalize Configuration
After the installation is complete, you may want to remove the ISO from the VM’s configuration so it doesn’t boot from it again. You can edit the XML:
virsh edit windows-vm
Change the <boot dev='cdrom'/> line to exclude the cdrom or remove the CD-ROM device section entirely.
Step 4: Restart the VM
You can now restart your VM with the following command:
virsh reboot windows-vm
Additional Notes
- Drivers: If you plan to use Windows on a virtual machine for a long term, you should install virtio drivers for better I/O performance. The drivers can usually be found on the Fedora project site or similar sites and can often be loaded from a separate ISO.
- Network Configuration: Ensure your network device is configured as needed for your environment.
- Remote Management: Use tools like
virt-manager if you prefer a graphical interface for managing your VMs.
This guide provides a basic setup for a Windows VM using virsh. You may need to adjust configurations based on your specific setup and requirements.