canhanhan / wsl-virtualbox

VBoxManage wrapper to use Virtualbox from WSL for Packer
MIT License
16 stars 8 forks source link

Floppy disk issues with WSL2 #2

Open dot-mike opened 1 year ago

dot-mike commented 1 year ago

Hello, I'm using this and seeing floppy disk is failing to attach with WSL2.

Error log:

2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: stderr:
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: Copying floppy to temp location: /tmp/virtualbox1073853779/floppy.vfd
==> virtualbox-iso: Deleting any current floppy disk...
==> virtualbox-iso: Attaching floppy disk...
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: Executing VBoxManage: []string{"storagectl", "windows_7", "--name", "Floppy Controller", "--add", "floppy"}
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: stdout:
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: stderr:
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: Executing VBoxManage: []string{"storageattach", "windows_7", "--storagectl", "Floppy Controller", "--port", "0", "--device", "0", "--type", "fdd", "--medium", "/tmp/virtualbox1073853779/floppy.vfd"}
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: stdout:
==> virtualbox-iso: Cleaning up floppy disk...
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: stderr: VBoxManage.exe: error: Could not find file for the medium 'C:\Users\mike\AppData\Local\Docker\wsl\distro\\C\Users\mike\AppData\Local\Docker\wsl\dataC\Users\mike\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\rootfs\tmp\virtualbox1073853779\floppy.vfd' (VERR_PATH_NOT_FOUND)
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: VBoxManage.exe: error: Details: code VBOX_E_FILE_ERROR (0x80bb0004), component MediumWrap, interface IMedium, callee IUnknown
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: VBoxManage.exe: error: Context: "OpenMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType, enmAccessMode, fForceNewUuidOnOpen, pMedium.asOutParam())" at line 205 of file VBoxManageDisk.cpp
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: VBoxManage.exe: error: Invalid UUID or filename "C:\Users\mike\AppData\Local\Docker\wsl\distro\\C\Users\mike\AppData\Local\Docker\wsl\dataC\Users\mike\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\rootfs\tmp\virtualbox1073853779\floppy.vfd"
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: failed to unlock port lockfile: close tcp 127.0.0.1:5937: use of closed network connection
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: Executing VBoxManage: []string{"storageattach", "windows_7", "--storagectl", "IDE Controller", "--port", "0", "--device", "1", "--type", "dvddrive", "--medium", "none"}
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: stdout:
==> virtualbox-iso: Deregistering and deleting VM...
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: stderr:
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: Executing VBoxManage: []string{"unregistervm", "windows_7", "--delete"}
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: stdout:
==> virtualbox-iso: Deleting output directory...
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: stderr: 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Build 'virtualbox-iso' errored after 8 seconds 176 milliseconds: Error attaching floppy: VBoxManage error: VBoxManage.exe: error: Could not find file for the medium 'C:\Users\mike\AppData\Local\Docker\wsl\distro\\C\Users\mike\AppData\Local\Docker\wsl\dataC\Users\mike\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\rootfs\tmp\virtualbox1073853779\floppy.vfd' (VERR_PATH_NOT_FOUND)
2022/12/31 21:36:27 packer-builder-virtualbox-iso plugin: Deleting floppy disk: /tmp/packer4001797295
VBoxManage.exe: error: Details: code VBOX_E_FILE_ERROR (0x80bb0004), component MediumWrap, interface IMedium, callee IUnknown
2022/12/31 21:36:27 [INFO] (telemetry) ending virtualbox-iso
VBoxManage.exe: error: Context: "OpenMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType, enmAccessMode, fForceNewUuidOnOpen, pMedium.asOutParam())" at line 205 of file VBoxManageDisk.cpp
VBoxManage.exe: error: Details: code VBOX_E_FILE_ERROR (0x80bb0004), component MediumWrap, interface IMedium, callee IUnknown
VBoxManage.exe: error: Invalid UUID or filename "C:\Users\mike\AppData\Local\Docker\wsl\distro\\C\Users\mike\AppData\Local\Docker\wsl\dataC\Users\mike\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\rootfs\tmp\virtualbox1073853779\floppy.vfd"
VBoxManage.exe: error: Context: "OpenMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType, enmAccessMode, fForceNewUuidOnOpen, pMedium.asOutParam())" at line 205 of file VBoxManageDisk.cpp

VBoxManage.exe: error: Invalid UUID or filename "C:\Users\mike\AppData\Local\Docker\wsl\distro\\C\Users\mike\AppData\Local\Docker\wsl\dataC\Users\mike\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\rootfs\tmp\virtualbox1073853779\floppy.vfd"
==> Wait completed after 8 seconds 176 milliseconds
==> Wait completed after 8 seconds 176 milliseconds

2022/12/31 21:36:27 machine readable: error-count []string{"1"}
==> Some builds didn't complete successfully and had errors:
==> Some builds didn't complete successfully and had errors:
--> virtualbox-iso: Error attaching floppy: VBoxManage error: VBoxManage.exe: error: Could not find file for the medium 'C:\Users\mike\AppData\Local\Docker\wsl\distro\\C\Users\mike\AppData\Local\Docker\wsl\dataC\Users\mike\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\rootfs\tmp\virtualbox1073853779\floppy.vfd' (VERR_PATH_NOT_FOUND)
2022/12/31 21:36:27 machine readable: virtualbox-iso,error []string{"Error attaching floppy: VBoxManage error: VBoxManage.exe: error: Could not find file for the medium 'C:\\Users\\mike\\AppData\\Local\\Docker\\wsl\\distro\\\uf03f\\C\uf03a\\Users\\mike\\AppData\\Local\\Docker\\wsl\\dataC\uf03a\\Users\\mike\\AppData\\Local\\Packages\\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\\LocalState\\rootfs\\tmp\\virtualbox1073853779\\floppy.vfd' (VERR_PATH_NOT_FOUND)\r\nVBoxManage.exe: error: Details: code VBOX_E_FILE_ERROR (0x80bb0004), component MediumWrap, interface IMedium, callee IUnknown\r\nVBoxManage.exe: error: Context: \"OpenMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType, enmAccessMode, fForceNewUuidOnOpen, pMedium.asOutParam())\" at line 205 of file VBoxManageDisk.cpp\r\nVBoxManage.exe: error: Invalid UUID or filename \"C:\\Users\\mike\\AppData\\Local\\Docker\\wsl\\distro\\\uf03f\\C\uf03a\\Users\\mike\\AppData\\Local\\Docker\\wsl\\dataC\uf03a\\Users\\mike\\AppData\\Local\\Packages\\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\\LocalState\\rootfs\\tmp\\virtualbox1073853779\\floppy.vfd\""}
VBoxManage.exe: error: Details: code VBOX_E_FILE_ERROR (0x80bb0004), component MediumWrap, interface IMedium, callee IUnknown
VBoxManage.exe: error: Details: code VBOX_E_FILE_ERROR (0x80bb0004), component MediumWrap, interface IMedium, callee IUnknown
VBoxManage.exe: error: Context: "OpenMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType, enmAccessMode, fForceNewUuidOnOpen, pMedium.asOutParam())" at line 205 of file VBoxManageDisk.cpp
VBoxManage.exe: error: Context: "OpenMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType, enmAccessMode, fForceNewUuidOnOpen, pMedium.asOutParam())" at line 205 of file VBoxManageDisk.cpp
VBoxManage.exe: error: Invalid UUID or filename "C:\Users\mike\AppData\Local\Docker\wsl\distro\\C\Users\mike\AppData\Local\Docker\wsl\dataC\Users\mike\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\rootfs\tmp\virtualbox1073853779\floppy.vfd"
VBoxManage.exe: error: Invalid UUID or filename "C:\Users\mike\AppData\Local\Docker\wsl\distro\\C\Users\mike\AppData\Local\Docker\wsl\dataC\Users\mike\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\rootfs\tmp\virtualbox1073853779\floppy.vfd"

==> Builds finished but no artifacts were created.
==> Builds finished but no artifacts were created.
2022/12/31 21:36:27 [INFO] (telemetry) Finalizing.
2022/12/31 21:36:28 waiting for all plugin processes to complete...

Version info:

C:\Users\mike>wsl -v
WSL-versjon: 1.0.3.0
Kjerneversjon: 5.15.79.1
WSLg-versjon: 1.0.47
MSRDC-versjon: 1.2.3575
Direct3D-versjon: 1.606.4
DXCore-versjon: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows: 10.0.22000.1335

C:\Users\mike>wsl --list --verbose
  NAME                   STATE           VERSION
* Ubuntu-22.04           Running         2
  docker-desktop         Running         2
  docker-desktop-data    Stopped         2
dot-mike commented 1 year ago

Turns out I had more than one WSL distribution installed as shown below and the fact that rootfs is no longer available in WSL2 is causing issues. (see issue https://github.com/microsoft/WSL/issues/6221)

To fix script not detecting the correct WSL distro

To fix this I updated the script to first find the registry path for the current distro and then query the BasePath for the distro GUID using the newly found registry path 😄

Update line 1-5 as follows:

#!/bin/bash

# Get the Windows registry path for the current WSL distro
wsl_distro_regpath=$(reg.exe query "HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Lxss" /s /f DistributionName 2>&1 | sed -e 's|\r||g' | grep -B1 -e "$WSL_DISTRO_NAME$" | head -n1)

# Get path for WSL storage
wslroot=$(wslpath $(reg.exe query $wsl_distro_regpath /s /v BasePath | awk 'BEGIN { FS = "[ \t]+" } ; /BasePath/{print $4}' | tr -d "[:cntrl:]"))

To fix WSL2 issue where rootfs is not a vailid path

Comment out the if-clause for the floopy-disk check so it becomes:

    # Packer tries to create floppy in the linux /tmp folder which is not representable in Windows. Replace it with direct storage path
    #if [[ $argument == /tmp/* ]]; then
      #argument="$wslroot/rootfs$argument"
    #fi

Issue showing multiple WSL distros:

$ reg.exe query "HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Lxss" /s /f DistributionName

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\{2917863d-fe64-4014-ae04-f3121232474d}
    DistributionName    REG_SZ    docker-desktop

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\{ef66b4e7-1c22-4297-b9e9-5f84bd69a473}
    DistributionName    REG_SZ    docker-desktop-data

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\{fe915138-9e0c-44a0-bc0c-165279ae48ca}
    DistributionName    REG_SZ    Ubuntu-22.04

Full bash script after edits:

#!/bin/bash

# Get the Windows registry path for the current WSL distro
wsl_distro_regpath=$(reg.exe query "HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Lxss" /s /f DistributionName 2>&1 | sed -e 's|\r||g' | grep -B1 -e "$WSL_DISTRO_NAME$" | head -n1)

# Get path for WSL storage
wslroot=$(wslpath $(reg.exe query $wsl_distro_regpath /s /v BasePath | awk 'BEGIN { FS = "[ \t]+" } ; /BasePath/{print $4}' | tr -d "[:cntrl:]"))

# Initialize defaults
is_next_path=0
args=()

for argument; do
  # If the current argument is --medium expect path of the medium in next argument
  if [ "$argument" == 'none' ]; then
    is_next_path=0
  elif [ "$argument" = '--medium' ]; then
    is_next_path=1
  elif [ $is_next_path = 1 ]; then
    # Packer tries to create floppy in the linux /tmp folder which is not representable in Windows. Replace it with direct storage path
    #if [[ $argument == /tmp/* ]]; then
      #argument="$wslroot/rootfs$argument"
    #fi
    # Convert WSL paths to Windows path
    argument=$(wslpath -w "$argument")
    is_next_path=0
  fi

  args+=("\"$argument\"")
done

# Redirect to Windows VBoxManage and convert Windows paths back to WSL paths
echo "${args[@]}" | xargs /mnt/c/Program\ Files/Oracle/VirtualBox/VBoxManage.exe | sed -r '/[A-Za-z]:\\.*$/{h; s/(.*)([A-Za-z]:\\.*)$/\2/; s/(.+)/wslpath "\1"/e; H; x; s/(([A-Za-z]:\\.*)\n(.+))/\3/ }'

# /[A-Za-z]:\\.*$/ # Find lines that end with Windows paths
#  { # For each line
#    h; # Copy to hold storage
#    s/(.*)([A-Za-z]:\\.*)$/\2/; #Remove everything except the path
#    s/(.+)/wslpath "\1"/e; #Convert the path to WSL path
#    H; # Append to the hold storage
#    x; # Swap pattern storage with hold storage
#       # Pattern storage have 2 lines now:
#       #   SAMPLE C:\Windows\Temp
        #   /mnt/c/Windows/Temp
#    s/(([A-Za-z]:\\.*)\n(.+))/\3/ # Replace Windows path in the first line with the WSL path in the second line
#  

This is how the path for the floppy-disk looks like to VirtualBox: This is a valid UNC path (https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats)

VirtualBoxVM_2022-12-31_22-16-10