Accessing removable media in Bash on Windows

64,692

Solution 1

Good news, it is now possible to mount USB media (including formatted as FAT) and network shares with drvfs on Windows 10:

Mount removable media: (e.g. D:)

$ sudo mkdir /mnt/d
$ sudo mount -t drvfs D: /mnt/d

To safely unmount

$ sudo umount /mnt/d

You can also mount network shares without smbfs:

$ sudo mount -t drvfs '\\server\share' /mnt/share

You need at least Build 16176 so you might have to opt-in to the Windows Insider programm and then update Windows. Source: https://blogs.msdn.microsoft.com/wsl/2017/04/18/file-system-improvements-to-the-windows-subsystem-for-linux/

Solution 2

Is there a way to access removable media from within Bash on Windows?

Update:

Apparently it is now possible starting from Windows 10 Build 16176.

See https://superuser.com/a/1209701/337631.


No.

At the moment there are limitations on what drives are mounted:

In order for a drive to show up under /mnt/ it must meet the following criteria:

  1. The drive must be a fixed drive
  2. The drive must be formatted to NTFS.

This has been raised as an issue: Drives other than C: are not mounted in /mnt #1079. It is still marked as "Open".

To facilitate interoperability with Windows, WSL uses the DrvFs file system. WSL automatically mounts all fixed drives with supported file systems under /mnt, such as /mnt/c, /mnt/d, etc. Currently, only NTFS and ReFS volumes are supported.

Source WSL File System Support


Further Reading

Solution 3

I am finally able to configure WSL2 to automount my SDcard as non-root user and preserve owner:group and rwx permissions on SDCard. My SDcard is NTFS in format.

Step1. enable automount. Create /etc/wsl.conf with this content

$cat /etc/wsl.conf
[automount]
enable = true
options = "defaults,user"
mountFsTab = true

Step2: create mnt/d where we want to mount our drive

mkdir /mnt/d

Step3: Enable fstab entry for SDCard to be mounted at /mtn/d. In my case, D: drive (on surface pro 7)

$ cat /etc/fstab
LABEL=cloudimg-rootfs   /        ext4   defaults        0 1
D:      /mnt/d  drvfs   defaults,user,metadata,exec  0       0

Note:

  1. there are two lines, first one was already there. I added the second line.
  2. options user is requires, else it mounts as root user. and option metadata is required, else it wont be able preserve user, group etc permissions.
  3. exec is needed, else you may not be able to execute files in mounted file system. (I was not able to execute file in windows 11)

Step 4: restart wsl

#exit bash or linux, go to dos/windows prompt
wsl --shutdown
wsl # or bash 

Solution 4

To add on to the existing answers: if you install an ext driver for windows (e.g. Ext2Fsd) you can mount and ext flesystem like you would an NTFS filesystem. This can be useful if you want to mount Raspberry Pi SDCards.

Share:
64,692

Related videos on Youtube

Aaron Campbell
Author by

Aaron Campbell

Updated on September 18, 2022

Comments

  • Aaron Campbell
    Aaron Campbell over 1 year

    Is there a way to access removable media (e.g. thumb drives or SD cards) from within Bash on Windows?

    Bash on Windows uses DriveFs to mount fixed drives in the /mnt folder, but it doesn't automatically mount removable media. And it doesn't look like it puts them in /dev either:

    Aaron@MYPC:/$ ls /dev
    block  fd  kmsg  lxss  null  ptmx  pts  random  shm  stderr  stdin  stdout  tty  tty0  tty1  urandom  zero
    

    Is there a way to mount removable drives? Is there a way to access the underlying block device?

    • CodeIt
      CodeIt almost 8 years
    • strobelight
      strobelight over 7 years
      type mount and see if that gives you clues. If using cygwin bash, the path is /cygdrive/.
    • Aaron Campbell
      Aaron Campbell over 7 years
      Nope: Aaron@MYPC:~$ mount rootfs on / type rootfs (rw,relatime) sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime) proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000) tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=204320k,mode=755) [etc]
    • DavidPostill
      DavidPostill over 7 years
    • Aaron Campbell
      Aaron Campbell over 7 years
      Similar, although this question specifically mentions removable media and that one refers to fixed media. The highest rated answer on that post is only partially applicable to this question.
    • Nic
      Nic about 5 years
      @AaronCampbell And yet you accepted it as an answer...?
    • Aaron Campbell
      Aaron Campbell about 5 years
      I was referring to the highest rated answer to the question that @DavidPostill marked as a possible duplicate, not on this thread.
  • DavidPostill
    DavidPostill over 7 years
    mounting removable drives is not yet supported. "WSL automatically mounts all fixed drives with supported file systems under /mnt, such as /mnt/c, /mnt/d, etc. Currently, only NTFS and ReFS volumes are supported." See blogs.msdn.microsoft.com/wsl/2016/06/15/wsl-file-system-supp‌​ort
  • DavidPostill
    DavidPostill over 7 years
    lol. I already did it some time ago :) See my answer here Manipulate windows files with linux bash shell in windows 10
  • user643011
    user643011 about 7 years
    Now there is a solution: superuser.com/a/1209701/182880
  • DavidPostill
    DavidPostill about 7 years
    Please do not post the same answer to multiple questions. If the same information really answers both questions, then one question (usually the newer one) should be closed as a duplicate of the other. You can indicate this by voting to close it as a duplicate or, if you don't have enough reputation for that, raise a flag to indicate that it's a duplicate. Otherwise tailor your answer to this question and don't just paste the same answer in multiple places.
  • user643011
    user643011 about 7 years
    Dear David. Feel free to raise the flag for duplication yourself. This answer is tailored to the question and you are more than welcome to make suggestions for improvement.
  • Sz.
    Sz. almost 6 years
    Dear "user", I upvoted your comment here assuming that what you said was, well, true... Now I realize I was wrong, but can't seem to undo my comment upvote, so I have to ask explicitly: how exactly is this "tailored to the question", while still being the exact "bitwise" copy of your other answer?
  • user643011
    user643011 almost 6 years
    Hey Sz! Because it's true? It answers exactly: Is there a way to access removable media from within Bash on Windows. If you have a better answer then feel free to answer the question better or suggest an edit to this one. Thanks!
  • Nic
    Nic about 5 years
    wrong fs type, bad option, bad superblock on G:, missing codepage or helper program, or other error -- also, please read this comment; don't answer duplicates.
  • netpoetica
    netpoetica about 5 years
    What is "drvfs" supposed to represent in this case? If I do which drvfs there is no such program. Documentation is a bit sparse on this on the Googles.
  • TNT
    TNT over 4 years
    Is it possible without sudo?
  • user643011
    user643011 over 4 years
    @netpoetica drvfs is the vfstype (type of the filesystem). See man mount, e.g. linux.die.net/man/8/mount
  • user643011
    user643011 over 4 years
    @TNT: mount is a powerful (privileged) command that needs root permissions. You should have no problem to execute sudo, because as a developer you should have sudo access on your Windows Subsystem for Linux. If not, try resetting your password or reinstalling it. winaero.com/blog/reset-password-wsl-linux-distro-windows-10 Google it or open a new question if you still have problems.
  • TNT
    TNT over 4 years
    @user643011: I've got no problems with lost sudo passoword. I'm just wondering, because Linux GUI and Windows auto mount/dismount without asking for elevated permissions. I'm trying to make a "user-proof" backup script, it would be better if not ask for a password.
  • ihavenokia
    ihavenokia about 4 years
    can we dd this mounted device?
  • R.D. Alkire
    R.D. Alkire over 2 years
    This is now the best solution since it's more up-to-date, and the mount is persistent