Skip to content

Instantly share code, notes, and snippets.

@aduh95
Last active January 9, 2021 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aduh95/a7d90afb44688c1b606b58d7e5fd7747 to your computer and use it in GitHub Desktop.
Save aduh95/a7d90afb44688c1b606b58d7e5fd7747 to your computer and use it in GitHub Desktop.
Auto-mount a NTFS partition on a GNU/Linux system (Windows dualboot)

Dual-boot GNU/Linux↔Windows file sharing

You want to use the same files on different environment? Here is how to setup your Linux system.

It is strongly recommended to have a separate partition for every OS you want to have and another partition on which you want to store your data. If you are using UNIX-like OSes only, you can have a /home partition that every OS will share and you don't need this tutorial. However, if you want to use a Windows OS as well, here is what you need to do.

Note: If you have kept your documents on the same partition as your Windows installation (this is default), you can still use the Windows partition as the DATA partition, but that is far from ideal.

N.B.: This shared DATA partition MUST be formated on NTFS if you want to access it from a Windows OS.

Note: Because of NTFS limitation, your files must be on a NTFS partition. That means you'll have to give up on the file-specific permission system for example, every files in your NTFS will have the same permissions (0o777 for the directories and 0o755 for the other files).

Disclaimer

THIS AN IMPORTANT ACTION THAT CAN HARM YOUR SYSTEM FOR GOOD. Please don't do it alone if you don't understand what you are doing.

How to

First, get the UUID of the partition:

ls -lh /dev/disk/by-uuid

Pick the partition you want to have mounted automatically and copy its UUID and put it under a variable $UUID. If you don't know which one it is, you can spot NTFS partition by their shorter UUID (because they don't really have a UUID, they use a serial number instead).

Example:

$ ls -lh /dev/disk/by-uuid
total 0
lrwxrwxrwx 1 root root 11 Nov 16 22:03 C24C8AB84C8AA72F -> ../../xvda1

Here the UUID of my partition is C24C8AB84C8AA72F

UUID="C24C8AB84C8AA72F"

Choose a mount point you want and put it into a bash variable:

MOUNT_POINT="/mnt/DATA/"

Note: If the partition is already mounted, it doesn't matter if you specify a different mount point.

Then we need to modify the fstab file.

cp /etc/fstab ~/fstab.save # this is in case something wrong happens
echo -e "\n# Mount shared NTFS partition\nUUID=$UUID $MOUNT_POINT" \
    "ntfs-3g rw,auto,user,exec,fmask=0022,dmask=0000,x-gvfs-show,uid=1000" \
    "0 2" | sudo tee -a /etc/fstab
sudo mkdir -p $MOUNT_POINT
sudo mount -a

Or if you're connected as root:

cp /etc/fstab ~/fstab.save # this is in case something wrong happens
echo "" >> /etc/fstab
echo "# Mount shared NTFS partition" >> /etc/fstab
echo -n "UUID=$UUID $MOUNT_POINT ntfs-3g" >> /etc/fstab
echo -n "rw,auto,user,exec,fmask=0022,dmask=0000," >> /etc/fstab
echo "x-gvfs-show,uid=1000 0 2" >> /etc/fstab
mkdir -p $MOUNT_POINT
mount -a

Troubleshooting

If the last command fails, compare the ancient file saved in ~/fstab.save and the new one /etc/fstab to check if everything looks good.

DO NOT reboot until mount -a works.

Dynamic linking (optional)

After that, I can recommand to replace the original Documents and Downloads folders by the one from Windows. First you need to localize the full path of the Windows "home" folder (the one containing the Documents and Downloads folders). It should be something like that:

# replace `usermname` with your Windows account name in the following:
WINDOWS_HOME_DIR="$MOUNT_POINT/Users/username"

You can use the attached script to create the links for you:

curl -O https://gist.github.com/aduh95/a7d90afb44688c1b606b58d7e5fd7747/raw/05ab035bbcc9f0afbf168a172c15b2f6e7e320e6/symbolicLinkWithWindows.sh
sh symbolicLinkWithWindows.sh $WINDOWS_HOME_DIR Documents
sh symbolicLinkWithWindows.sh $WINDOWS_HOME_DIR Downloads
# You can link as many files or directories you'd like

Now, if you cd or ls into your Documents folder, you should always see the same content, on GNU/Linux as on Windows.

If ln fails, you can try to kill the graphical interface and execute the remote script from the CLI.

#!/bin/sh
WINDOWS_HOME_DIR=$(realpath "$1" | sed 's/\/$//')
TARGETED_FOLDER_NAME=$(echo "$2" | sed 's/\/$//')
[ -d "$HOME" ] || (\
echo "HOME variable is not defined in your environment or is not a directory" &&\
exit 1\
)
[ -e "$WINDOWS_HOME_DIR/$TARGETED_FOLDER_NAME" ] || (\
echo "'$WINDOWS_HOME_DIR/$TARGETED_FOLDER_NAME' does not exist, aborting!" &&\
exit 1\
)
echo "Deleting folder '$HOME/$TARGETED_FOLDER_NAME' (press y to confirm at each step)…"
rm -ri "$HOME/$TARGETED_FOLDER_NAME"
echo "Creating symbolic link to '$WINDOWS_HOME_DIR/$TARGETED_FOLDER_NAME'…"
ln -Ts "$WINDOWS_HOME_DIR/$TARGETED_FOLDER_NAME" "$HOME/$TARGETED_FOLDER_NAME"
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment