Moving a WSL virtual disk to another drive with automation
I have a couple disk drives in my experimental box and I want to move the WSL disks to one of the secondary, non-boot, drives. I want to use the bandwidth that exists on multiple drives on multiple buses and get better performance for my WSL instances. I do the same thing for VMS that I run in VMware Workstation or Fusion.
All the text on this page came from the video transcript of Moving a WSL virtual disk to another drive
WSL Distributions
WSL is managed as registered distributions. Each one of those distributions has an associated vhdx. Those are located on the C:\ drive so also I tend to run out of C:\ space because I do all kinds of other things there. We're going to need to do a couple tweaks to move them and make this work. There's no way to edit this easier to do what we want or make the change with just a config file change.
Migration Discussion
We're going to destroy the existing registration and recreate the registration with the vhd somewhere else.
- Terminate whatever distributions are running.
- Create a backup of the vhd and other info which results in a .tar file
- Unregister the instance which will wipe out this entire stack
- Import the .tar backup into WSL.
You can do all this by hand. There are plenty of sites out there to do this. Or you can look at my automation script and figure out what the script's doing. The script steps are
- Update WSL
- Terminate the distribution
- Create a tar backup of the vhd
- Unregister this with a WSL. That removes the registration and deletes the vhd.
- You got to create this backup before you do this unregistering thing.
- Run wsl.exe import. It creates a vhd from the backup and registers it with esl
- The last thing we can do is we can set the imported distribution as the default WSL if you want to so every time you can have a default distribution. I don't tend to worry about the default distribution because I target the WSL on the CLI with pretty much every command or every session.
We may need to restore the default Linux user to WSL if we don't want to default to root, UID 0. The default user which one you shell into by default is actually a registry entry that is lost in the move. The import doesn't let you set a non-root user. That's a defect in my opinion I'm sure there's an outstanding bug request for it
We have this set of scripts in https://github.com/freemansoft/wsl2-utils The README.md walks you through usage and gives examples of the sizing of the backups. I had Ubuntu and Kali on my machine. There are three backups for Kali Linux
The 5 backups in the snip:
- Fresh install 500KB default install
- Windows display integration installed, with one command, 3GB
- Full Kali max install 18GB
- Fresh Ubuntu default install 1.3GB
- Updated Ubuntu base install 1.5GB
I have plenty of space on my I. It is 800GB 800 so it was no big deal for me to back up these pretty big files. I have plenty of space so I just leave the tar files there in case I want to roll back to one of those versions
Script Discussion
The first thing it does is it checks to see if Kali Linux exists and terminates the program if not. Then it stops/terminates that running instance. It exports it to the tar file and it tells you the status of the export. It unregistered the existing instance of Kali Linux, logging that. Then it imports Kali Linux from the tar file and registers with the same distribution name. You can set the default. In my case the logs say non-destructive. There is a mode in the script that defines the last three steps as non-destructive so you can verify your settings prior to doing the actual move.
You don't have to
use my program right. I think it's cool because automation is repeatable. The code is at http://github.com/freemansoft/wsl2-utils
The only script in there at the time of this blog is move-distribution.ps1.
- The source distribution name, its WSL registration name
- The backup file location
- The backup file name. This one includes a date so you can take repeated backups without overwriting previous ones.
- Future todo: Create a backup recovery script that doesn't do an export
- The new location for the WSL vhd after the import
- The WSL registration name to use for the import. It defaults to the same name but could be something different.
- A flag that tells us if the imported WSL distribution should be set as the default.
- A test flag that runs creates the backup and then logs the destructive operations it would have taken, un-registration, import and setting the default distribution
- This flag is set to false, non-destructive. You have to change it to true to actually cause the unregister/import actions.
Comments
Post a Comment