Writable NTFS

This page will explain how to obtain read/write support with NTFS. With Linux this is performed using either the ntfs-3g or ntfs3 driver.

  1. Install USB support.
  2. Connect your storage device, the device and its partitions are available as Device files under /dev, e.g. /dev/sda1.

NTFS is the primary Windows filesystem and is available via the ntfs-3g driver. With kernel 5.15 onward the new ntfs3 aims to provide improved performance as an in-kernel driver similar to other Linux filesystems. Either driver may be used however below is written for ntfs-3g due to its proven reliability.

  • ntfs-3g
  • fdisk optional Required to autodetect the filesystem type when using the hotplug script.

Mount a partition to an existing directory. You can create one with mkdir, for example mkdir -p /mnt/sda1.

To mount a partition to above directory:

ntfs-3g /dev/sda1 /mnt/sda1 -o rw,big_writes

To auto mount a partition at startup (with drive plugged in) edit /etc/rc.local:

sleep 1

ntfs-3g /dev/sda1 /mnt/sda1 -o rw,lazytime,noatime,big_writes

exit 0

To unmount:

umount /dev/sda1

To be able to mount it automatically:

ln -s /usr/bin/ntfs-3g /sbin/mount.ntfs

For details about mounting options used above see man page.

In 2021 a new NTFS implementation was merged into Linux kernel 5.15 called ntfs3. There are numerous discussions comparing this new driver to NTFS-3G which is external to the kernel source but used for decades. See more information about ntfs3 in the FAQ. You can use either driver, provided you are running kernel 5.15 or later.

Given that ntfs3 runs in kernel space it should eventually be more performant than using ntfs-3g. In the short-term some users found more performance with ntfs-3g. Your mileage may vary.

If you want a recommendation, for now it may be better to use the long proven ntfs-3g driver so this page is written as such.

Now that you can get your volume to mount on command, the next step is mounting it when it's plugged in automatically.

To get our drive to mount on plugin, we utilize the hotplug system. Create the following files as /etc/hotplug.d/block/10-mount.

#!/bin/sh
# Copyright (C) 2011 OpenWrt.org
sleep 10 #more apps installed, need more time to load kernel modules!
blkdev=`dirname $DEVPATH`
if [ `basename $blkdev` != "block" ]; then
	device=`basename $DEVPATH`
	case "$ACTION" in
		add)
			mkdir -p /mnt/$device
			# vfat & ntfs-3g check
			if [ `which fdisk` ]; then
				isntfs=`fdisk -l | grep $device | grep NTFS`
				isvfat=`fdisk -l | grep $device | grep FAT`
				isfuse=`lsmod | grep fuse`
				isntfs3g=`which ntfs-3g`
			else
				isntfs=""
				isvfat=""
			fi

			# mount with ntfs-3g if possible, else with default mount
			if [ "$isntfs" -a "$isfuse" -a "$isntfs3g" ]; then
				ntfs-3g /dev/$device /mnt/$device
			elif [ "$isvfat" ]; then
				mount -o iocharset=utf8 /dev/$device /mnt/$device
			else
				mount /dev/$device /mnt/$device
			fi
		;;
		remove)
			umount -l /dev/$device
		;;
	esac
fi

(The script above comes from this blog post)

Now, whenever you plug in an NTFS USB disk, it should automatically mount. (Note that this will be a different path than /mnt/usb-ntfs)

Below is a modified version of the script which should work fine even if you are using root file system using extroot. The original script tries to re-mount all the filesystems, including mtdblock* filesystems at boot which is not what we want.

Code is added to dismiss disks which can be managed by block_mount. It should be preferred if it supports the file system you are using but it has limited filesystem type support.

The mount binary seem to figure out what filesystem type it is trying to mount, therefore the code sections for checking this is removed. The script should be able to mount any supported filesystem. (so should it be in ntfs section?)

Some sensible mount options which should be suitable for both solid state and normal drives is also added. In addition, hdparm is called to set drive APM setting so drive can go to standby if not used (the correct setting may be drive dependent). The hd-idle does not seem to work on USB drives properly, but the APM setting is able to make the drive to use its internal logic if supported by the drive.

One other problem with the original script was related to unmounting. Once a drive is disconnected, it disappears from /dev therefore it can not be unmounted by giving the original /dev path (you would just get 'No such file or directory' error). Therefore the script finds where the drive was mounted and uses the mount point for unmounting.

The script was tested on OpenWrt 21.02.1 and requires hdparm to be installed. Modify the script to suit your needs.

#!/bin/sh

# Copyright (C) 2021 OpenWrt.org

blkdev=`dirname $DEVPATH`
basename=`basename $blkdev`
device=`basename $DEVPATH`
path=$DEVPATH

if [ $basename != "block" ] && [ -z "${device##sd*}" ]; then
        islabel=`block info /dev/$device | grep -q LABEL ; echo $?`
        if [ $islabel -eq 0 ] ; then
                mntpnt=`block info /dev/$device |sed 's/.*LABEL="\([^"]*\)".*/\1/'`
        else
                mntpnt=$device
        fi

        # Do not tolerate spaces in mount points -- the remove case mountpoint determination fails
        if echo "$mntpnt" |grep -q ' ' ; then
                exit 0
        fi

        case "$ACTION" in
                add)
                        mkdir -p "/mnt/$mntpnt"
                        # Set APM value for automatic spin down
                        /sbin/hdparm -B 127 /dev/$device
                        # Try to be gentle on solid state devices
                        mount -o noatime,discard /dev/$device "/mnt/$mntpnt"
                ;;
                remove)
                        # Once the device is removed, the /dev entry disappear. We need mountpoint
                        mountpoint=`mount |grep /dev/$device | sed 's/.* on \(.*\) type.*/\1/'`
                        umount -l $mountpoint
                        rmdir $mountpoint
                ;;
        esac
fi
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
  • Last modified: 2024/03/12 13:43
  • by palebloodsky