|
| 1 | +--- |
| 2 | +title: Get started mounting a linux disk in WSL 2 (preview) |
| 3 | +description: Learn how to set up a disk mount in WSL 2 and how to access it. |
| 4 | +keywords: wsl, windows, windowssubsystem, gnu, linux, bash, disk, ext4, filesystem, mount |
| 5 | +ms.date: 06/08/2020 |
| 6 | +ms.topic: article |
| 7 | +ms.localizationpriority: medium |
| 8 | +--- |
| 9 | + |
| 10 | +# Get started mounting a linux disk in WSL 2 (preview) |
| 11 | + |
| 12 | +If you want to access a Linux disk format that isn't supported by Windows, you can use WSL 2 to mount your disk and access its content. |
| 13 | + |
| 14 | +This tutorial will cover the steps to identify the disk and partition to attach to WSL2, how to mount them, and how to access them. |
| 15 | + |
| 16 | +> [!NOTE] |
| 17 | +> Administrator access is required to attach a disk to WSL 2. |
| 18 | +
|
| 19 | +## Identify the disk |
| 20 | + |
| 21 | +To list the available disks in Windows, run: |
| 22 | + |
| 23 | +```powershell |
| 24 | +wmic diskdrive list brief |
| 25 | +``` |
| 26 | + |
| 27 | +The disks paths are available under the 'DeviceID' columns. Usually under the `\\.\PHYSICALDRIVE*` format. |
| 28 | + |
| 29 | +## List and select the partitions to mount in WSL 2 |
| 30 | + |
| 31 | +Once the disk is identified, run: |
| 32 | + |
| 33 | +```powershell |
| 34 | +wsl --mount <DiskPath> --bare |
| 35 | +``` |
| 36 | + |
| 37 | +This will make the disk available in WSL 2. |
| 38 | + |
| 39 | +Once attached, the partition can be listed by running the following command inside WSL 2: |
| 40 | + |
| 41 | +```powershell |
| 42 | +lsblk |
| 43 | +``` |
| 44 | + |
| 45 | +This will display the available block devices and their partitions. |
| 46 | + |
| 47 | +Inside Linux, a block device is identified as `/dev/<Device><Partition>`. For example, /dev/sdb3, is the partition number 3 of disk `sdb`. |
| 48 | + |
| 49 | +Example output: |
| 50 | + |
| 51 | +```powershell |
| 52 | +NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT |
| 53 | +sdb 8:16 0 1G 0 disk |
| 54 | +├─sdb2 8:18 0 50M 0 part |
| 55 | +├─sdb3 8:19 0 873M 0 part |
| 56 | +└─sdb1 8:17 0 100M 0 part |
| 57 | +sdc 8:32 0 256G 0 disk / |
| 58 | +sda 8:0 0 256G 0 disk |
| 59 | +``` |
| 60 | + |
| 61 | +## Identifying the filesystem type |
| 62 | + |
| 63 | +If you don't know the type of filesystem of a disk or partition, you can use this command: |
| 64 | + |
| 65 | +```powershell |
| 66 | +blkid <BlockDevice> |
| 67 | +``` |
| 68 | + |
| 69 | +This will output the detected filesystem type (under the `TYPE="<Filesystem>"` format). |
| 70 | + |
| 71 | +## Mount the selected partitions |
| 72 | + |
| 73 | +Once you have identified the partitions you want to mount, run this command on each partition: |
| 74 | + |
| 75 | +```powershell |
| 76 | +wsl --mount <DiskPath> --partition <PartitionNumber> --type <Filesystem> |
| 77 | +``` |
| 78 | + |
| 79 | +> [!NOTE] |
| 80 | +> If you wish to mount the entire disk as a single volume (i.e. if the disk isn't partitioned), `--partition` can be omitted. |
| 81 | +> |
| 82 | +> If omitted, the default filesystem type is "ext4". |
| 83 | +
|
| 84 | +## Access the disk content |
| 85 | + |
| 86 | +Once mounted, the disk can be accessed under the path pointed to by the config value: `automount.root`. The default value is `/mnt/wsl`. |
| 87 | + |
| 88 | +From Windows, the disk can be accessed from File Explorer by navigating to: `\\wsl$\\<Distro>\\<Mountpoint>` (pick any Linux distribution). |
| 89 | + |
| 90 | +## Unmount the disk |
| 91 | + |
| 92 | +If you want to unmount and detach the disk from WSL 2, run: |
| 93 | + |
| 94 | +```powershell |
| 95 | +wsl --unmount <DiskPath> |
| 96 | +``` |
| 97 | + |
| 98 | +## Command line reference |
| 99 | + |
| 100 | +### Mouting a specific filesystem |
| 101 | + |
| 102 | +By default, WSL 2 will attempt to mount the device as ext4. To specify another filesystem, run: |
| 103 | + |
| 104 | +```powershell |
| 105 | +wsl --mount <DiskPath> -t <FileSystem> |
| 106 | +``` |
| 107 | + |
| 108 | +For example, to mount a disk as fat, run: |
| 109 | + |
| 110 | +``` |
| 111 | +wsl --mount <Diskpath> -t vfat |
| 112 | +``` |
| 113 | + |
| 114 | +> [!NOTE] |
| 115 | +> To list the available filesystems in WSL2, run: `cat /proc/filesystems` |
| 116 | +
|
| 117 | +### Mouting a specific partition |
| 118 | + |
| 119 | +By default, WSL 2 attempts to mount the entire disk. To mount a specific partition, run: |
| 120 | + |
| 121 | +``` |
| 122 | +wsl --mount <Diskpath> -p <PartitionIndex> |
| 123 | +``` |
| 124 | + |
| 125 | +This only works if the disk is either MBR (Master Boot Record) or GPT (GUID Partition Table). [Read about partition styles - MBR and GPT](https://docs.microsoft.com/windows-server/storage/disk-management/initialize-new-disks#about-partition-styles---gpt-and-mbr). |
| 126 | + |
| 127 | +### Specifying mount options |
| 128 | + |
| 129 | +To specify mount options, run: |
| 130 | + |
| 131 | +```powershell |
| 132 | +wsl --mount <DiskPath> -o <MountOptions> |
| 133 | +``` |
| 134 | + |
| 135 | +Example: |
| 136 | + |
| 137 | +```powershell |
| 138 | +wsl --mount <DiskPath> -o "data=ordered" |
| 139 | +``` |
| 140 | + |
| 141 | +> [!NOTE] |
| 142 | +> Only filesystem specific options are supported at this time. Generic options such as `ro, rw, noatime, ...` are not supported. |
| 143 | +
|
| 144 | +### Attaching the disk without mounting it |
| 145 | + |
| 146 | +If the disk scheme isn't supported by any of the above options, you can attach the disk to WSL 2 without mounting it by running: |
| 147 | + |
| 148 | +```powershell |
| 149 | +wsl --mount <DiskPath> --bare |
| 150 | +``` |
| 151 | + |
| 152 | +This will make the block device available inside WSL 2 so it can be mounted manually from there. Use `lsblk` to list the available block devices inside WSL 2. |
| 153 | + |
| 154 | +### Detaching a disk |
| 155 | + |
| 156 | +To detach a disk from WSL 2, run: |
| 157 | + |
| 158 | +```powershell |
| 159 | +wsl --unmount [DiskPath] |
| 160 | +``` |
| 161 | + |
| 162 | +If `Diskpath` is omitted, all attached disks are unmounted and detached. |
| 163 | + |
| 164 | +> [!NOTE] |
| 165 | +> If one disk fails to unmount, WSL 2 can be forced to exit by running `wsl --shutdown`, which will detach the disk. |
| 166 | +
|
| 167 | +## Limitations |
| 168 | + |
| 169 | +- At this time, only entire disks can be attached to WSL 2, meaning that it's not possible to attach only a partition. Concretely, this means that it's not possible to use `wsl --mount` to read a partition on the boot device, because that device can't be detached from Windows. |
| 170 | + |
| 171 | +- USB flash drives are not supported at this time and will fail to attach to WSL 2. USB disks are supported though. |
| 172 | + |
| 173 | +- Only filesystems that are natively supported in the kernel can be mounted by `wsl --mount`. This means that it's not possible to use installed filesystem drivers (such as ntfs-3g for example) by calling `wsl --mount`. |
0 commit comments