Move your WSL to cloud and save storage space
I decided to move my WSL2 Ubuntu installation to a folder inside my OneDrive. Goal was to save storage space. I study and work remotely so I carry light. My laptop for study is light and humble: 256GB storage space. Other configuration details are unnecessary for the purpose of the setup discussed in this article.
Before you proceed:
The setup would work only with network connection i.e., you need internet access to use your WSL once you make the move.
My setup assumes OneDrive as my cloud provider and I have the OneDrive folders mounted to my computer. You need to mount your folders from your cloud (OneDrive, DropBox, GoogleDrive etc.,) to your computer drives.
Steps
Start powershell (preferably as adminstrator)
Shutdown your wsl. This is not a necessary step but I suggest you shut your WSL down before you make the move. Things do not go according to the plan sometimes and you want to be cautious.
wsl -—shutdown
Export your WSL distribution as below
wsl --export Ubuntu C:\Users\saisu\OneDrive\Documents\Research\wsl\ubuntu.tar
“Ubuntu” is the name of my installed Linux distribution. If you are not sure of your installed distribution, then open “wsl” and find out the distribution name/version using “lsb_release -a”
The full qualified path in the export command is the destination folder for the compressed WSL image. Clearly, my destination folder is on my OneDrive as well. I really want to save space for the exported file too, which is an intermediate object in this process.
Unregister your WSL distribution
wsl —-unregister Ubuntu
Import your WSL distribution to the desired folder
wsl.exe --import Ubuntu . ubuntu.tar
“.” is the current folder, which in my case is “C:\Users\saisu\OneDrive\Documents\Research\wsl\ubuntu.tar”. Replace “.” with the fully qualified or relative path of folder of your choice on your cloud.
The process takes a few minutes and once it finishes, you are ready to use your WSL as before but from the cloud.