Enabling Enhanced Session Mode for Arch Linux Guests in Hyper-V

Enabling Enhanced Session Mode for Arch Linux Guests in Hyper-V

Using virtual machines on Linux in Hyper-V out of the box is a somewhat less comfortable task than with guest Windows machines. The reason for this is that Hyper-V was not initially intended for desktop use; you cannot simply install the guest additions package and achieve functional graphics acceleration, clipboard sharing, shared directories, and other perks of life, as is the case with VirtualBox.

Hyper-V itself provides several integration services — for instance, guests can utilize the host's Volume Shadow Copy Service (VSS), signals can be sent to shut down guests, guests can synchronize their system time with the virtualization host, and file sharing with the virtual machine is possible from the host (Copy-VMFile in PowerShell). For certain guest operating systems, including Windows of course, Enhanced Session Mode is available in the Virtual Machine Connection application (vmconnect.exe) which works over the RDP protocol and allows for the passthrough of disk devices and printers to the virtual machine, as well as sharing the clipboard.

Enhanced Session Mode works out of the box in Windows in Hyper-V immediately after installation. For Linux guests, you need to set up an RDP server that supports vsock (a special virtual network address space in Linux designed for communication with the hypervisor). If for Ubuntu there is a specially prepared virtual machine template in the VMCreate application that comes with Hyper-V on desktop editions of Windows, where an RDP server functioning with vsock, XRDP is already pre-installed, with other distributions it is less straightforward — for example, the author of this post managed to enable ESM in Fedora. Here, we will activate Enhanced Session Mode for a virtual machine running Arch Linux.

Installing integration services

Here, everything is more or less simple; we just need to install the hyperv package from the community repository:

% sudo pacman -S hyperv

We will enable the VSS, metadata and file sharing services: % for i in {vss,fcopy,kvp}; do sudo systemctl enable hv_${i}_daemon.service; done

Installing XRDP

linux-vm-tools

Repository on GitHub provides scripts that automate the installation and configuration process for XRDP for Arch Linux and Ubuntu. We will install Git if it is not already installed, along with a compiler and other software for manual builds, after which we will clone the repository: % sudo pacman -S git base-devel % git clone https://github.com/microsoft/linux-vm-tools.git % cd linux-vm-tools/arch

% sudo pacman -S git base-devel
% git clone https://github.com/microsoft/linux-vm-tools.git
% cd linux-vm-tools/arch

At the time of writing this article, the latest version of XRDP that can be installed using the script makepkg.sh, available in the repository, is 0.9.11, which has broken parsing for vsock://-addresses, so you will need to install XRDP from Git and the Xorg driver from AUR manually. The patch for XRDP provided in AUR is also slightly outdated, so you'll need to manually edit the PKGBUILD and the patch.

Let's clone the repositories with the PKGBUILD files from AUR (usually this process is automated with programs like yay, but the author performed the entire process on a clean system):

% git clone https://aur.archlinux.org/xrdp-devel-git.git
% git clone https://aur.archlinux.org/xorgxrdp-devel-git.git

First, we'll install XRDP itself. We'll open the file PKGBUILD with any text editor.

Let's edit the build parameters. The PKGBUILD for building XRDP from Git does not include support for vsock during the build, so we will enable it ourselves:

 build() {
   cd $pkgname
   ./configure --prefix=/usr 
               --sysconfdir=/etc 
               --localstatedir=/var 
               --sbindir=/usr/bin 
               --with-systemdsystemdunitdir=/usr/lib/systemd/system 
               --enable-jpeg 
               --enable-tjpeg 
               --enable-fuse 
               --enable-opus 
               --enable-rfxcodec 
               --enable-mp3lame 
-              --enable-pixman
+              --enable-pixman 
+              --enable-vsock
   make V=0
 }

In the patch arch-config.diff, which modifies the units and startup scripts of XRDP to the paths used in Arch Linux, there is also a patch for the script instfiles/xrdp.sh, which, at the time of writing this article, had been removed from the XRDP package, so the patch will need to be edited manually:

  [Install]
  WantedBy=multi-user.target
-diff -up src/xrdp-devel-git/instfiles/xrdp.sh.orig src/xrdp-devel-git/instfiles/xrdp.sh
---- src/xrdp-devel-git/instfiles/xrdp.sh.orig  2017-08-30 00:27:28.000000000 -0600
-+++ src/xrdp-devel-git/instfiles/xrdp.sh   2017-08-30 00:28:00.000000000 -0600
-@@ -17,7 +17,7 @@
- # Description: starts xrdp
- ### END INIT INFO
- 
--SBINDIR=/usr/local/sbin
-+SBINDIR=/usr/bin
- LOG=/dev/null
- CFGDIR=/etc/xrdp
- 
 diff -up src/xrdp-devel-git/sesman/startwm.sh.orig src/xrdp-devel-git/sesman/startwm.sh
 --- src/xrdp-devel-git/sesman/startwm.sh.orig  2017-08-30 00:27:30.000000000 -0600

We will build and install the package by running the command % makepkg --skipchecksums -si (the key --skipchecksums is needed to disable checksum verification of the source files since we edited them manually).

Let's navigate to the directory xorgxrdp-devel-git, and then simply build the package with the command % makepkg -si.

Let's navigate to the directory linux-vm-tools/arch and run the script install-config.sh, which installs the settings for XRDP, PolicyKit, and PAM:

% sudo ./install-config.sh

The script installs the outdated setting use_vsock, which is ignored from version 0.9.11, so we will need to manually edit the configuration file: /etc/xrdp/xrdp.ini manually:

 ;   port=vsock://:
-port=3389
+port=vsock://-1:3389

 ; 'port' above should be connected with vsock instead of tcp
 ; use this only with number alone in port above
 ; prefer use vsock://: above
-use_vsock=true
+;use_vsock=true

 ; regulate if the listening socket uses the socket option tcp_nodelay

Let's add to the file ~/ .xinitrc launch your preferred window manager / desktop environment that will run when the X server starts:

% echo "exec i3" > ~/ .xinitrc

Shut down the virtual machine. Activate the vsock transport for the virtual machine by executing the following command in PowerShell as an administrator:

PS Admin > Set-VM -VMName MACHINE_NAME -EnhancedSessionTransportType HvSocket

Let's start the virtual machine again.

Connection

As soon as the XRDP service starts after the system boots, the vmconnect application will detect it and an item will become available in the menu View -> Enhanced Session. When selecting this item, we will be prompted to set the screen resolution, and in the tab Local Resources of the opened dialog, we can select the devices passed into the RDP session.

Enabling Enhanced Session Mode for Arch Linux Guests in Hyper-V
Enabling Enhanced Session Mode for Arch Linux Guests in Hyper-V

Let's connect. We will see the XRDP login window:

Enabling Enhanced Session Mode for Arch Linux Guests in Hyper-V

Let's enter our username and password.

Using

The profit from these manipulations is noticeable: the RDP session works much more responsively than when using a virtual display without Enhanced Session. The drives passed into the VM via RDP are available in the directory ${HOME}/shared-drives:

Enabling Enhanced Session Mode for Arch Linux Guests in Hyper-V

The clipboard works fine. It's not possible to pass printers in, not only is this not supported, but it also breaks the disk passthrough. Sound does not work either, but the author did not require it. To capture key combinations like Alt+Tab, you need to expand vmconnect to full screen.

If for any reason there is a desire to use the built-in Windows RDP client instead of the vmconnect application, or, for example, to connect to this machine from another machine, then it will be necessary to change to the file /etc/xrdp/xrdp.ini port to tcp://:3389. If the virtual machine is connected to the Default Switch and receives its network settings via DHCP, then you can connect to it from the host at the address machine_name.mshome.net. You can only access TTY from the vmconnect application, disabling Enhanced Mode.

References used:

  1. Hyper-V — Arch Wiki
  2. Bug reports on GitHub: 1, 2

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster