arm linux:添加对挂载ntfs和exfat格式u盘的支持(petalinux)

  • Linux内核(arm64 4.19.0)配置
    –>File systems
    <*> FUSE (Filesystem in Userspace) support
  • 添加exfat支持
    因为是用petalinux构建的linux环境,这里也用它添加exfat支持。(也可直接将驱动加到linux源码fs文件夹下,再修改对应目录的Makefile和Kconfig文件即可)
  1. 下载exfat驱动源码
  2. 添加exfat驱动到petalinux工程
petalinux-create -t modules --name exfat --enable
  1. 将驱动源码拷贝到添加的驱动文件夹
  2. 修改exfat.bb文件如下:
SUMMARY = "Recipe for  build an external exfat Linux kernel module"
SECTION = "PETALINUX/modules"
LICENSE = "GPLv2"
LIC_FILES_CHKSUM = "file://COPYING;md5=12f884d2ae1ff87c09e5b7ccc2c4ca7e"

inherit module

SRC_URI = "file://Makefile \
           file://dkms.conf \
           file://exfat_api.c \
           file://exfat_api.h \
           file://exfat_bitmap.c \
           file://exfat_bitmap.h \
           file://exfat_blkdev.c \
           file://exfat_blkdev.h \
           file://exfat_cache.c \
           file://exfat_cache.h \
           file://exfat_config.h \
           file://exfat_core.c \
           file://exfat_core.h \
           file://exfat_data.c \
           file://exfat_data.h \
           file://exfat-km.mk \
           file://exfat_oal.c \
           file://exfat_oal.h \
           file://exfat_super.c \
           file://exfat_super.h \
           file://exfat_upcase.c \
           file://exfat_version.h \
           file://exfat_nls.c \
           file://exfat_nls.h \
           file://Kconfig \
           file://LICENSE \
	   file://COPYING\
          "
S = "${WORKDIR}"
# The inherit of module.bbclass will automatically name module packages with
# "kernel-module-" prefix as required by the oe-core build environment.
  1. petalinux-build
  • ntfs
    因为linux内核源码中对ntfs的挂载是只读的,所以需要下载ntfs-3g工具进行交叉编译,再移植到板子
  1. 下载ntfs-3g源码
  2. 解压后,进入源码文件夹,进行交叉编译
./configure --build=“编译主机,如i386“ --host=“交叉编译工具链” --prefix=“编译生成文件路径” --exec-prefix=“编译执行文件路径”
make 
make install
  1. 将编译生成ntfs-3g文件(在执行configure 时,“exec-prefix”/bin 路径下),拷贝到板子的/bin文件夹下。(编译时make成功了,但make install失败,“exec-prefix”/bin 路径下没有ntfs-3g文件,搜索后在 ”‘源码路径’/src/.libs 文件夹下找到)。
  2. 将生成的库文件(在执行configure 时,“exec-prefix”/lib 路径下),拷贝到板子的/lib文件夹下
  3. 在板子上测试执行ntfs-3g输出如下,移植成功
root@root:~# ntfs-3g
ntfs-3g: No device is specified.

ntfs-3g 2017.3.23 integrated FUSE 27 - Third Generation NTFS Driver
                Configuration type 1, XATTRS are on, POSIX ACLS are off

Copyright (C) 2005-2007 Yura Pakhuchiy
Copyright (C) 2006-2009 Szabolcs Szakacsits
Copyright (C) 2007-2017 Jean-Pierre Andre
Copyright (C) 2009 Erik Larsson

Usage:    ntfs-3g [-o option[,...]] <device|image_file> <mount_point>

Options:  ro (read-only mount), windows_names, uid=, gid=,
          umask=, fmask=, dmask=, streams_interface=.
          Please see the details in the manual (type: man ntfs-3g).

Example: ntfs-3g /dev/sda1 /mnt/windows

News, support and information:  http://tuxera.com
  • 自动挂载
    exfat加入后会直接自动挂载,不需要再做修改。后面主要是实现ntfs格式u盘的自动挂载。
    板子环境:linux kernel 4.19.0,rootfs通过busybox1.29.2创建
  1. 当前系统采用udev实现U盘的自动挂载,所以可以通过修改mount.sh来添加ntfs格式u盘的特殊处理
  2. 搜索找到mount.sh文件路径为/etc/udev/scripts/mount.sh
  3. 在mount.sh加入ntfs格式判断,判断成功后执行ntfs-3g,而非mount指令。
  4. 修改后mount.sh如下
#!/bin/sh
#
# Called from udev
#
# Attempt to mount any added block devices and umount any removed devices

BASE_INIT="`readlink -f "/sbin/init"`"
INIT_SYSTEMD="/lib/systemd/systemd"
NTFS="/bin/ntfs-3g"

if [ "x$BASE_INIT" = "x$INIT_SYSTEMD" ];then
    # systemd as init uses systemd-mount to mount block devices
    MOUNT="/usr/bin/systemd-mount"
    UMOUNT="/usr/bin/systemd-umount"

    if [ -x $MOUNT ] && [ -x $UMOUNT ];
    then
        logger "Using systemd-mount to finish mount"
    else
        logger "Linux init is using systemd, so please install systemd-mount to finish mount"
        exit 1
    fi
else
    MOUNT="/bin/mount"
    UMOUNT="/bin/umount"
fi

PMOUNT="/usr/bin/pmount"

for line in `grep -h -v ^# /etc/udev/mount.blacklist /etc/udev/mount.blacklist.d/*`
do
	if [ ` expr match "$DEVNAME" "$line" ` -gt 0 ];
	then
		logger "udev/mount.sh" "[$DEVNAME] is blacklisted, ignoring"
		exit 0
	fi
done

automount_systemd() {
    name="`basename "$DEVNAME"`"

    # Skip the partition which are already in /etc/fstab
    grep "^[[:space:]]*$DEVNAME" /etc/fstab && return
    for n in LABEL PARTLABEL UUID PARTUUID; do
        tmp="$(lsblk -o $n $DEVNAME | sed -e '1d')"
        test -z "$tmp" && continue
        tmp="$n=$tmp"
        grep "^[[:space:]]*$tmp" /etc/fstab && return
    done

    [ -d "/run/media/$name" ] || mkdir -p "/run/media/$name"

    MOUNT="$MOUNT -o silent"

    # If filesystemtype is vfat, change the ownership group to 'disk', and
    # grant it with  w/r/x permissions.
    case $ID_FS_TYPE in
    vfat|fat)
        MOUNT="$MOUNT -o umask=007,gid=`awk -F':' '/^disk/{print $3}' /etc/group`"
        ;;
	ntfs)
	# 在此处加入对ntfs格式设备的特殊处理,不用系统的mount命令,改为ntfs-3g,完成后,直接退出当前函数
		MOUNT="$NTFS"
		if ! $MOUNT $DEVNAME "/run/media/$name"
		then
			#logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/run/media/$name\" failed!"
			rm_dir "/run/media/$name"
		else
			logger "mount.sh/automount" "Auto-mount of [/run/media/$name] successful"
			touch "/tmp/.automount-$name"
		fi
		return
	;;
    # TODO
    *)
        ;;
    esac

    if ! $MOUNT --no-block -t auto $DEVNAME "/run/media/$name"
    then
        #logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/run/media/$name\" failed!"
        rm_dir "/run/media/$name"
    else
        logger "mount.sh/automount" "Auto-mount of [/run/media/$name] successful"
        touch "/tmp/.automount-$name"
    fi
}

automount() {
	name="`basename "$DEVNAME"`"

	if [ -x "$PMOUNT" ]; then
		$PMOUNT $DEVNAME 2> /dev/null
	elif [ -x $MOUNT ]; then
		$MOUNT $DEVNAME 2> /dev/null
	fi

	# If the device isn't mounted at this point, it isn't
	# configured in fstab
	grep -q "^$DEVNAME " /proc/mounts && return

	! test -d "/run/media/$name" && mkdir -p "/run/media/$name"
	# Silent util-linux's version of mounting auto
	if [ "x`readlink $MOUNT`" = "x/bin/mount.util-linux" ] ;
	then
		MOUNT="$MOUNT -o silent"
	fi

	# If filesystem type is vfat, change the ownership group to 'disk', and
	# grant it with  w/r/x permissions.
	case $ID_FS_TYPE in
	vfat|fat)
		MOUNT="$MOUNT -o umask=007,gid=`awk -F':' '/^disk/{print $3}' /etc/group`"
		;;	
	ntfs)
	# 在此处加入对ntfs格式设备的特殊处理,不用系统的mount命令,改为ntfs-3g,完成后,直接退出当前函数
		MOUNT="$NTFS"
		if ! $MOUNT $DEVNAME "/run/media/$name"
		then
			#logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/run/media/$name\" failed!"
			rm_dir "/run/media/$name"
		else
			logger "mount.sh/automount" "Auto-mount of [/run/media/$name] successful"
			touch "/tmp/.automount-$name"
		fi
		return
	;;
	# TODO
	*)
		;;
	esac
	
	if ! $MOUNT -t auto $DEVNAME "/run/media/$name"
	then
		#logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/run/media/$name\" failed!"
		rm_dir "/run/media/$name"
	else
		logger "mount.sh/automount" "Auto-mount of [/run/media/$name] successful"
		touch "/tmp/.automount-$name"
	fi
}
	
rm_dir() {
	# We do not want to rm -r populated directories
	if test "`find "$1" | wc -l | tr -d " "`" -lt 2 -a -d "$1"
	then
		! test -z "$1" && rm -r "$1"
	else
		logger "mount.sh/automount" "Not removing non-empty directory [$1]"
	fi
}

# No ID_FS_TYPE for cdrom device, yet it should be mounted
name="`basename "$DEVNAME"`"
[ -e /sys/block/$name/device/media ] && media_type=`cat /sys/block/$name/device/media`

if [ "$ACTION" = "add" ] && [ -n "$DEVNAME" ] && [ -n "$ID_FS_TYPE" -o "$media_type" = "cdrom" ]; then
    # Note the root filesystem can show up as /dev/root in /proc/mounts,
    # so check the device number too
    if expr $MAJOR "*" 256 + $MINOR != `stat -c %d /`; then
        if [ "`basename $MOUNT`" = "systemd-mount" ];then
            automount_systemd
        else
            automount
        fi
    fi
fi

if [ "$ACTION" = "remove" ] || [ "$ACTION" = "change" ] && [ -x "$UMOUNT" ] && [ -n "$DEVNAME" ]; then
    for mnt in `cat /proc/mounts | grep "$DEVNAME" | cut -f 2 -d " " `
    do
        $UMOUNT $mnt
    done

    # Remove empty directories from auto-mounter
    name="`basename "$DEVNAME"`"
    test -e "/tmp/.automount-$name" && rm_dir "/run/media/$name"
fi

版权声明:本文为anyinxi原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。