启明办公

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 92|回复: 1

通过windows wsl2 挂载 ext4 硬盘

[复制链接]

1

主题

2

帖子

3

积分

新手上路

Rank: 1

积分
3
发表于 2022-9-21 16:02:19 | 显示全部楼层 |阅读模式
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. 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.
If you are looking for guidance on how to connect a USB device (flash drive, SD card reader, etc), see Connect USB devices.
Note
Administrator access is required to attach a disk to WSL 2. The WSL 2 mount command does not support mounting a disk (or partitions that belong to the disk) that is currently in use. wsl --mount always attaches the entire disk even if only a partition is requested. You can't mount the Windows installation disk.
Prerequisites

You will need to be on Windows 11 Build 22000 or later to access this feature. You can join the Windows Insiders Program to get the latest preview builds.
Mounting an unpartitioned disk

In this simplest case, if you have a disk that doesn't have any partitions, you can mount it directly using the wsl --mount command. First you need to identify the disk.

  • Identify the disk - To list the available disks in Windows, run:
    PowerShellCopy
    GET-CimInstance -query "SELECT * from Win32_DiskDrive"
    The disks paths are available under the 'DeviceID' columns. Usually under the \\.\PHYSICALDRIVE* format.
  • Mount the disk - Using PowerShell, you can mount the disk using the Disk path discovered above, run:
    PowerShellCopy
    wsl --mount <DiskPath>



Mounting a partitioned disk

If you have a disk that you aren't sure what file format it is in, or what partitions it has, you can follow the steps below to mount it.

  • Identify the disk - To list the available disks in Windows, run:
    PowerShellCopy
    GET-CimInstance -query "SELECT * from Win32_DiskDrive"
    The disks paths are listed after 'DeviceID', usually in the \\.\PHYSICALDRIVE* format.
  • List and select the partitions to mount in WSL 2 - Once the disk is identified, run:
    PowerShellCopy
    wsl --mount <DiskPath> --bare
    This will make the disk available in WSL 2. (In the case of our example, the <DiskPath> is \\.\PHYSICALDRIVE*.
  • Once attached, the partition can be listed by running the following command inside WSL 2:
    BashCopy
    lsblk
    This will display the available block devices and their partitions.
Inside Linux, a block device is identified as /dev/<Device><Partition>. For example, /dev/sdb3, is the partition number 3 of disk sdb.
Example output:
BashCopy
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   0    1G  0 disk
├─sdb2   8:18   0   50M  0 part
├─sdb3   8:19   0  873M  0 part
└─sdb1   8:17   0  100M  0 part
sdc      8:32   0  256G  0 disk /
sda      8:0    0  256G  0 disk
Identifying the filesystem type

If you don't know the type of filesystem of a disk or partition, you can use this command:
BashCopy
blkid <BlockDevice>
This will output the detected filesystem type (under the TYPE="<Filesystem>" format).
Mount the selected partitions

Once you have identified the partitions you want to mount, run this command on each partition:
PowerShellCopy
wsl --mount <DiskPath> --partition <PartitionNumber> --type <Filesystem>
Note
If you wish to mount the entire disk as a single volume (i.e. if the disk isn't partitioned), --partition can be omitted.
If omitted, the default filesystem type is "ext4".
Access the disk content

Once mounted, the disk can be accessed under the path pointed to by the config value: automount.root. The default value is /mnt/wsl.
From Windows, the disk can be accessed from File Explorer by navigating to: \\wsl$\\<Distro>\\<Mountpoint> (pick any Linux distribution).
Unmount the disk

If you want to unmount and detach the disk from WSL 2, run:
PowerShellCopy
wsl --unmount <DiskPath>
Mount a VHD in WSL

Note
WSL from the Microsoft Store introduces a new argument to directly mount a VHD: wsl --mount --vhd <pathToVHD>
You can also mount virtual hard disk files (VHD) into WSL using wsl --mount. To do this, you first need to mount the VHD into Windows using the Mount-VHD command in Windows. Be sure to run this command with administrator privileges. Below is an example where we use this command, and also output the disk path. Be sure to replace <pathToVHD> with your actual VHD path.
PowerShellCopy
Write-Output "\\.\PhysicalDrive$((Mount-VHD -Path <pathToVHD> -PassThru | Get-Disk).Number)"
You can use the output above to obtain the disk path for this VHD and mount that into WSL following the instructions in the previous section.
You can also use this technique to mount and interact with the virtual hard disks of other WSL distros, as each WSL 2 distro is stored via a virtual hard disk file called: ext4.vhdx. By default the VHDs for WSL 2 distros are stored in this path: C:\Users\[user]\AppData\Local\Packages\[distro]\LocalState\[distroPackageName], please exercise caution accessing these system files, this is a power user workflow. Make sure to run wsl --shutdown before interacting with this disk to ensure the disk is not in use.



Command line reference

Mounting a specific filesystem

By default, WSL 2 will attempt to mount the device as ext4. To specify another filesystem, run:
PowerShellCopy
wsl --mount <DiskPath> -t <FileSystem>
For example, to mount a disk as fat, run:
Copy
wsl --mount <Diskpath> -t vfat Note
To list the available filesystems in WSL2, run: cat /proc/filesystems
When a disk has been mounted via WSL2 (Linux file system), it is no longer available to mount via an ext4 driver on the Windows file system.
Mounting a specific partition

By default, WSL 2 attempts to mount the entire disk. To mount a specific partition, run:
Copy
wsl --mount <Diskpath> -p <PartitionIndex>This only works if the disk is either MBR (Master Boot Record) or GPT (GUID Partition Table). Read about partition styles - MBR and GPT.
Specifying mount options

To specify mount options, run:
PowerShellCopy
wsl --mount <DiskPath> -o <MountOptions>
Example:
PowerShellCopy
wsl --mount <DiskPath> -o "data=ordered"
Note
Only filesystem specific options are supported at this time. Generic options such as ro, rw, noatime, ... are not supported.
Attaching the disk without mounting it

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:
PowerShellCopy
wsl --mount <DiskPath> --bare
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.
Specifying the mount name

Note
This option is only available with WSL from the Microsoft Store
By default the mountpoint name is generated based on the physical disk or VHD name. This can be overriden with --name. Example:
PowerShellCopy
wsl --mount <DiskPath> --name myDisk
Detaching a disk

To detach a disk from WSL 2, run:
PowerShellCopy
wsl --unmount [DiskPath]
If Diskpath is omitted, all attached disks are unmounted and detached.
Note
If one disk fails to unmount, WSL 2 can be forced to exit by running wsl --shutdown, which will detach the disk.
Limitations


  • 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.
  • 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.
  • Filesystems not directly supported by the kernel can be mounted via a --bare attach and then invoking the relevant FUSE driver.
回复

使用道具 举报

0

主题

5

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2022-9-21 16:03:08 | 显示全部楼层
If you're on a version of Windows 11 that is build 22000 or greater, you can now use WSL to mount Linux disks directly. Run winver to see your Windows version. I'm on 22000.282 as of the time of this writing.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|启明办公

Copyright © 2001-2013 Comsenz Inc.Template by Comsenz Inc.All Rights Reserved.

Powered by Discuz!X3.4

快速回复 返回顶部 返回列表