WSL2 (Windows Subsystem for Linux) is a powerful tool that allows you to run a Linux distribution on your Windows machine. It provides a seamless integration between the two operating systems, allowing you to use Linux command-line tools and utilities within the Windows environment. One of the great features of WSL2 is the ability to mount external drives, such as USB drives or network shares, within the Linux environment. However, by default, these mounted drives are not persistent and will disappear once you close the WSL window. In this article, we will show you how to keep a WSL2-mounted ext4 drive persistent and available to Windows even after closing the WSL window.
Step 1: Install WSL2 and set up a Linux distribution
The first step is to install WSL2 and set up a Linux distribution on your Windows machine. You can follow the official Microsoft documentation for detailed instructions on how to do this. Once you have WSL2 up and running, open the WSL window and make sure you have a Linux distribution installed and set as the default distribution.
Step 2: Mount the ext4 drive in WSL2
Once you have your Linux distribution set up, you can proceed to mount the ext4 drive within the WSL2 environment. To do this, follow these steps:
- Connect the ext4 drive to your Windows machine.
- Open the WSL window and navigate to the desired mount point. This is the directory where you want the ext4 drive to be mounted.
- Run the following command to create a mount point directory:
sudo mkdir /mnt/ext4drive - Next, you need to find the device name of your ext4 drive. Run the following command to list all the connected devices:
sudo fdisk -l - Look for the entry that corresponds to your ext4 drive. It should be something like
/dev/sdX, where X is a letter representing the drive. - Run the following command to mount the ext4 drive:
sudo mount -t ext4 /dev/sdX /mnt/ext4drive
Now, your ext4 drive is mounted within the WSL2 environment and you can access its contents from the Linux command line.
Step 3: Make the ext4 drive persistent
To make the ext4 drive persistent and available to Windows even after closing the WSL window, you need to add an entry to the /etc/fstab file. The /etc/fstab file contains information about the file systems and devices that should be mounted at boot time. Here's how you can do it:
- Open the
/etc/fstabfile in a text editor using the following command:
sudo nano /etc/fstab - Add the following line to the end of the file:
/dev/sdX /mnt/ext4drive ext4 defaults 0 0 - Replace
/dev/sdXwith the device name of your ext4 drive. - Save the file and exit the text editor.
Now, the ext4 drive will be automatically mounted at boot time, and it will remain persistent and available to Windows even after closing the WSL window.
Conclusion
By following the steps outlined in this article, you can keep a WSL2-mounted ext4 drive persistent and available to Windows even after closing the WSL window. This allows you to easily access and work with the contents of the ext4 drive from both the Linux command line and the Windows environment. Enjoy the seamless integration between Windows and Linux with WSL2!
References
| Reference | Link |
|---|---|
| Microsoft WSL2 Documentation | https://docs.microsoft.com/en-us/windows/wsl/ |