Scott Hanselman

WSL2 can now mount Linux ext4 disks directly

November 02, 2021 Comment on this post [3] Posted in Linux
Sponsored By

If you're on a version of Windows 11 that is build 22000 or greater, you can now use WSL to mount Linux disks directly. Run winver to see your Windows version. I'm on 22000.282 as of the time of this writing.

I can also run wsl --help and see the --mount instructions. If you don't have them, you're not on the latest, or you can try installing/update WSL from the Windows Store. Installing WSL from the Windows Store gets you updates faster.

--mount <Disk>
Attaches and mounts a physical disk in all WSL2 distributions.
Options:
--bare
Attach the disk to WSL2, but don't mount it.

--type <Type>
Filesystem to use when mounting a disk, if not specified defaults to ext4.

--options <Options>
Additional mount options.

--partition <Index>
Index of the partition to mount, if not specified defaults to the whole disk.

--unmount [Disk]
Unmounts and detaches a disk from all WSL2 distributions.
Unmounts and detaches all disks if called without argument.

You'll need to be an admin to mount a disk. You can first get a list of all the disks using this PowerShell query:

GET-CimInstance -query "SELECT * from Win32_DiskDrive"

The DeviceID is a path like \\.\SOMETHING and that's what matters.

Then you just wsl --mount \\.\SOMETHING".

The device will appear under /mnt/wsl/SOMETHING in your Linux instance. You can mount unpartitioned disks like this, or you can mount partitioned disks. Then you can run lsblk and see the partitions and they'll be under /dev/<Device><Partition>. Once you know the partition number you can go back and wsl --mount --\\.\SOMETHING --partition --type Filesystem. The filesystem parameter is for things like vfat, etc for filesystems that have kernel support.

WSL --mount for ext4 and Linux File Systems on WSL2

Today SD Cards and Flash Drives aren't working, but USB externals work and internal drives work, as well as VHDs.


Sponsor: Couchbase Capella DBaaS is flexible, full-featured and fully managed  with built-in access via K/V, SQL and full text search. It’s blazing fast, yet surprisingly affordable. Try Capella today for free.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

How to set the default user for a WSL distro that has been manually installed with wsl --import

October 28, 2021 Comment on this post [2] Posted in Linux | Win10 | Win11
Sponsored By

I've blogged before on how to easily move WSL distributions between Windows 10 machines with import and export. I recently did a full fresh install of Windows 11 and wanted to bring my existing highly customized Ubuntu installation along with me.

You can tar up (zip up) the user-mode parts of your WSL2 distributions like this:

wsl --export Ubuntu-20.04 c:\Temp\UbuntuBackup.tar

The part after --export is the distribution name that you can see from running wsl --list -v. The last argument is a full path and filename for the archive you want created.

Next, on the machine you've moved to, you'll do the reverse. Notice that I've changed the Distro name here, and you can if you want. Remember also that you can have as many Linux Distros installed as you want.

wsl --import Ubuntu c:\Linux c:\Temp\UbuntuBackup.tar

The Linux file system is stored in a VHDX (virtual hard drive), usually deep in AppData/Local/YadaYada, but this import is an opportunity for me to store it in C:\Linux which will also make it easier to do maintenance on like Compact-VHD which shrinks your WSL2 disks.

Here's the weird part. When you import a WSL2 distro manually, running that distro on the new machine will end up logging you in as root. It's forgotten that I'm "scott."

There's a lot of ways to fix this that involve the registry or passing in arguments to wsl, but I just want it to work when I run "wsl" or "wsl -d distroname."

Run your distro, and then edit /etc/wsl.conf and add a [user] section like this:

[user]
default=scott

This is the ideal way to set your WSL distro's default user for imported tars because it's stored inside the Linux file system and the setting will stick around when you export/import later on.

Linux on Windows

Hope this helps!


Sponsor: Lob APIs ensure your addresses are deliverable and everything you send arrives at the right place. Add address autocompletion and verification in minutes using React, Vue or Javascript - Try for free!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

How to fix Base System Device Driver issue in Windows 10 and Windows 11

October 26, 2021 Comment on this post [3] Posted in Win10 | Win11
Sponsored By

I had a hard drive die recently so I decided to do a full fresh reinstall of Windows, this time a fresh Windows 11 from a downloaded ISO burned to a USB stick.

Lots of Base System Devices in Device Manager

It was a solid install and everything worked out of the box. I used it for a few days and had no issues, but while poking around I noticed in the Device Manager that there were dozens of Base System Devices that were banged out. Like a TON.

I'd like to get that fixed, so I went to Windows Update but WU said everything was cool.

Since a lot of stuff moved and was redesigned in Windows 11 I went looking for "Windows Update Optional Updates" and it took me a while to find it, even though it's listed right there on Windows Update in Settings.

Windows Update optional updates in Windows 11

Click on Advanced Options. Here you can control things like your Windows Update active hours so it doesn't reboot when you don't want it, etc.

Here you'll see a bunch of Optional Updates. I had like 33 of them.

Lots of Optional Updates in Windows 11

Here's what it looks like when you have a bunch of updates pending. These are Chipset and Motherboard updates.

Optional Updates in Windows Update

I did have to select each of these checkboxes and select Install at the end, but once they were done, I had no banged out (yellow exclamation point) devices in Device Manager.

Hope this helps!


Sponsor: Lob APIs ensure your addresses are deliverable and everything you send arrives at the right place. Add address autocompletion and verification in minutes using React, Vue or Javascript - Try for free!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

Parallel.ForEachAsync in .NET 6

October 21, 2021 Comment on this post [6] Posted in DotNetCore | Open Source
Sponsored By

Great tweet from Oleg Kyrylchuk (follow him!) showing how cool Parallel.ForEachAsync is in .NET 6. It's new! Let's look at this clean bit of code in .NET 6 that calls the public GitHub API and retrieves n number of names and bios, given a list of GitHub users:

using System.Net.Http.Headers;
using System.Net.Http.Json;

var userHandlers = new []
{
"users/okyrylchuk",
"users/shanselman",
"users/jaredpar",
"users/davidfowl"
};

using HttpClient client = new()
{
BaseAddress = new Uri("https://api.github.com"),
};
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("DotNet", "6"));

ParallelOptions parallelOptions = new()
{
MaxDegreeOfParallelism = 3
};

await Parallel.ForEachAsync(userHandlers, parallelOptions, async (uri, token) =>
{
var user = await client.GetFromJsonAsync<GitHubUser>(uri, token);

Console.WriteLine($"Name: {user.Name}\nBio: {user.Bio}\n");
});

public class GitHubUser
{
public string Name { get; set; }
public string Bio { get; set; }
}

Let's note a few things in this sample Oleg shared. First, there's no Main() as that's not required (but you can have it if you want).

We also see just two usings, bringing other namespaces into scope. Here's what it would look like with explicit namespaces:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Threading.Tasks;

We've got an array of users to look up in userHandlers. We prep an HttpClient and setup some ParallelOptions, giving our future ForEach the OK to "fan out" to up to three degrees of parallelism - that's the max number of concurrent tasks we will enable in one call. If it's -1 there is no limit to the number of concurrently running operations.

The really good stuff is here. Tight and clean:

await Parallel.ForEachAsync(userHandlers, parallelOptions, async (uri, token) =>
{
var user = await client.GetFromJsonAsync<GitHubUser>(uri, token);

Console.WriteLine($"Name: {user.Name}\nBio: {user.Bio}");
});

"Take this array and naively fan out into parallel tasks and make a bunch of HTTP calls. You'll be getting JSON back that is shaped like the GitHubUser."

We could make it even syntactically shorter if we used a record vs a class with this syntax:

public record GitHubUser (string Name, string Bio);

This makes "naïve" parallelism really easy. By naïve we mean "without inter-dependencies." If you want to do something and you need to "fan out" this is super easy and clean.


Sponsor: Make login Auth0’s problem. Not yours. Provide the convenient login features your customers want, like social login, multi-factor authentication, single sign-on, passwordless, and more. Get started for free.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

Space Cadet Pinball for Windows 95 recompiled for Linux running on Windows 11 as a Linux app under WSLg

October 19, 2021 Comment on this post [16] Posted in Linux | Win11
Sponsored By

Award for longest blog post title ever? Andrey Muzychenko has a great github repository where they decompiled the 25 year old Space Cadet Pinball application from Windows 95/XP and then recompiled it for Linux (and really any platform now that it's portable code!).

NOTE: Because this is a decompilation/recompilation, it doesn't include the original data files. You'll need those from a Windows XP disk or ISO that you'll need to find yourself.

I recently did a YouTube where I showed that Windows 11 runs Graphical Linux Apps out of the box with WSLg.

Here, they've taken a Windows 95 32-bit app and decompiled it from the original EXE, done some nice cleanup, and now it can be recompiled to other targets like Linux.

So, could I go Windows 95 -> Linux -> Windows 11 -> WSL -> WSLg and run this new native Linux executable again on Windows?

If you don't think this is cool, that's a bummer. It's an example of how powerful (and fun) virtualization has become on modern systems!

Pinball under Linux under Windows 11

I just launched WSL (Ubuntu) and installed a few things to compile the code:

sudo apt-get install libsdl2-image-dev
sudo apt-get install libsdl2-mixer-dev
sudo apt install gcc clang build-essential cmake

Then I cloned the repo under WSL and built. It builds into bin and creates a Linux executable.

NOTE: Place compiled executable into a folder containing original game resources (not included).

I am a digital hoarder so I have digital copies of basically everything I've worked on for the last 30 years. I happened to have a Windows XP virtual disk drive from a VM from years ago that was saved on my Synology.

Virtual hard drive from Windows XP

I was able to open it and get all the original resources and wav files.

Pinball resources

Then I copy all the original resources minus the .exe and then run the newly built Linux version...and it magically pops out and runs on Windows...as a graphical Linux app.

3D Pinball for Windows

Amazing! Have fun!


Sponsor: Make login Auth0’s problem. Not yours. Provide the convenient login features your customers want, like social login, multi-factor authentication, single sign-on, passwordless, and more. Get started for free.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.