Ubuntu: Fixing “Read-Only File System” Error on WSL

By Samuel Molinski, 17 April, 2024
A digital illustration for a blog post titled 'WSL - Ubuntu mounted read-only disk issue'. The scene depicts a frustrated developer, a young South Asian woman, in a home office setting looking at her laptop screen displaying an error message about a read-only disk. She has long black hair tied in a ponytail and is wearing casual attire. The environment includes technical books, a plant, and Ubuntu and WSL logos visible in the background, emphasizing the software development context.

I have been working with DDEV and enjoying myself. One annoying issue is the mounted drive becoming read-only associated with my WSL Ubuntu installation. I believe it's related to my company laptop going into sleep/hibernation and causing havoc with Docker that is running inside of Ubuntu and inside of WSL— I know, there are too many layers of virtualization. Unfortunately, it's a necessary evil at this moment.

I threw together a Powershell script that does most of my work. I created a new script named reset-ubuntu.ps1 and added this code.

wsl.exe --shutdown
wsl.exe --set-default kali-linux
$ext4_volume = (Get-ChildItem -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | Where-Object { $_.GetValue("DistributionName") -eq 'Ubuntu' }).GetValue("BasePath") + "\ext4.vhdx"
wsl.exe --mount $ext4_volume --vhd --bare
wsl.exe sudo e2fsck -fy /dev/sdc
wsl.exe sudo fsck -fy /dev/sdc
wsl.exe --unmount
wsl --set-default ubuntu

Make sure you are in Powershell by running powershell, then run the script by running .\reset-ubuntu.ps1

Feel free to edit it to work with your own environment. The script assumes you have the broken Ubuntu instance named "ubuntu" and that you also have an instance of Kali Linux installed and called "kali-linux."

If you want to know more, here are some resources that explain the underlying issue and how to correct it. 

 

 

Comments