安装驱动器的脚本 -mount-menu.sh

安装驱动器的脚本 -mount-menu.sh

如何从命令行触发自动挂载?我说的“自动挂载”不是指全自动挂载,而是指获取可用设备列表,然后选择一个并最终将其作为/media/{user}/{diskid}。例如,Nautilus 或 Thunar 提供了此功能,但我似乎找不到命令行工具来触发这种半自动挂载。

pmount是我发现的最接近的,但似乎通过完全不同的底层机制工作,并使设备显示为/media/sdf或类似的东西。

答案1

您可以使用:

udisksctl 安装-b设备名称

在哪里device_name是存储设备的名称,看起来应该类似于/dev/sdb1

使用lsblksudo fdisk -l命令您可以找出连接到系统的所有存储设备。

答案2

gio mount

財產協會现已被列为弃用(2018 年),建议您使用“gio”,它是 Gnome In Out 和 Glib 的一部分。请参阅维基百科

例如,要自动挂载第二个驱动器分区;创建一个具有可执行权限的 bash 脚本,以便在启动时运行以下命令:

gio mount -d /dev/sda2

如果您是该分区的所有者(请参阅chown),则不需要 sudo。

要挂载位于以下位置的 ISO 文件~/ISOs

gio mount "archive://file%3A%2F%2F%2Fhome%2Fpablo%2FISOs%2Fubuntu-18.04-desktop-amd64.iso"

你可以URL 编码使用 Python 3 的路径并realpath(连接到archive://

python -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1], \"\"))" "file://$(realpath ubuntu-18.04-desktop-amd64.iso)"

这将安装在 上/run/user/$(id -u)/gvfs/

作为替代方案gnome-disk-image-mounter将安装在 上/media/$USER/

要卸载,请使用gio mount -u /run/user/$(id -u)/gvfs/archive*(或/media/$USER/,取决于您的安装方式)。

udisksctl

列出可用的设备:

udisksctl status

安装通过以下方式完成:

udisksctl mount -b /dev/sdf

或者

udisksctl mount -p block_devices/sdf

卸载通过以下方式完成:

udisksctl unmount -b /dev/sdf

或者

udisksctl unmount -p block_devices/sdf

可以object-path通过以下方式找到:

udisksctl dump

类型的对象org.freedesktop.UDisks2.Block似乎是有效的object-patch/org/freedesktop/UDisks2/必须从路径中剪切前缀以便 udisksctl 接受它们。

gvfs-mount

可以使用以下方法列出可用的设备:

gvfs-mount --list

可以使用以下方法安装它们:

gvfs-mount -d /dev/sdf

可以通过以下方式卸载:

gvfs-mount --unmount /media/user/01234567890

剩下的一个问题是我不知道如何gvfs-mount --list在 mount 命令中使用输出,因为--list不会显示块设备名称,并且尝试在 mount 中使用其打印的设备名称将导致:

Error mounting location: volume doesn't implement mount

结论

虽然和都gvfs-mount可以udisksctl完成这些任务,但它们的界面不切实际,因为它们不提供可用磁盘的可读状态,而只是提供过于冗长的信息转储。

答案3

一个简单的解决方案,可以按要求工作(挂载到 /media/{user}/{diskid}),但它不能列出设备,但需要给出准确的、区分大小写的卷标作为参数 $1

安装

DEVICE=$(findfs LABEL=$1) && udisksctl mount -b $DEVICE

卸载

DEVICE=$(findfs LABEL=$1) && udisksctl unmount -b $DEVICE

答案4

安装驱动器的脚本 -mount-menu.sh

mount-menu.sh脚本允许您选择未安装的驱动器/分区进行安装。要调用该脚本,请使用:sudo mount-menu.sh。此屏幕根据您的独特机器环境定制:

安装菜单 1.png

  • 使用箭头键选择分区并按Enter

菜单将清除并将此信息保留在您的终端中:

=====================================================================
Mount Device:  /dev/nvme0n1p10
Mount Name:    /mnt/mount-menu.FPRAW
File System:   ext4
ID:            Ubuntu
RELEASE:       18.04
CODENAME:      bionic
DESCRIPTION:   Ubuntu 18.04.1 LTS
 Size  Used Avail Use%
  27G  7.9G   18G  32%

现在您可以使用:cd /mnt/mount-menu.FPRAW访问外部驱动器的分区。

然后,您可以使用,cd home/YOUR_NAME但要小心不要/在 前面放home。如果您使用 ,cd /home它会带您进入启动驱动器并退出外部驱动器。

mount-menu.sh脚本内容

要创建脚本,请打开终端并输入:

sudo -H gedit /usr/local/bin/mount-menu.sh

然后复制下面的代码并粘贴到gedit。保存文件并退出gedit

现在使用以下命令将文件标记为可执行文件:

sudo chmod a+x /usr/local/bin/mount-menu.sh

这是要复制的脚本:

#!/bin/bash

# NAME: mount-menu.sh
# PATH: /usr/local/bin
# DESC: Select unmounted partition for mounting
# DATE: May 9, 2018. Modified May 11, 2018.

# $TERM variable may be missing when called via desktop shortcut
CurrentTERM=$(env | grep TERM)
if [[ $CurrentTERM == "" ]] ; then
    notify-send --urgency=critical \ 
                "$0 cannot be run from GUI without TERM environment variable."
    exit 1
fi

# Must run as root
if [[ $(id -u) -ne 0 ]] ; then echo "Usage: sudo $0" ; exit 1 ; fi

#
# Create unqique temporary file names
#

tmpMenu=$(mktemp /tmp/mount-menu.XXXXX)     # Menu list
tmpInfo=$(mktemp /tmp/mount-menu.XXXXX)     # Mount Parition Info
tmpWork=$(mktemp /tmp/mount-menu.XXXXX)     # Work file
MountName=$(mktemp -d /mnt/mount-menu.XXXXX)  # Mount directory name

#
# Function Cleanup () Removes temporary files
#

CleanUp () {
    [[ -f $tmpMenu ]] && rm -f $tmpMenu     # If temporary files created
    [[ -f $tmpInfo ]] && rm -f $tmpInfo     #  at various program stages
    [[ -f $tmpWork ]] && rm -f $tmpWork     #  remove them before exiting.
}


#
# Mainline
#

lsblk -o NAME,FSTYPE,LABEL,SIZE,MOUNTPOINT > $tmpMenu

i=0
SPACES='                                                                     '
DoHeading=true
AllPartsArr=()      # All partitions.

# Build whiptail menu tags ($i) and text ($Line) into array

while read -r Line; do
    if [[ $DoHeading == true ]] ; then
        DoHeading=false                     # First line is the heading.
        MenuText="$Line"                    # Heading for whiptail.
        FSTYPE_col="${Line%%FSTYPE*}"           
        FSTYPE_col="${#FSTYPE_col}"         # FS Type, ie `ext4`, `ntfs`, etc.
        MOUNTPOINT_col="${Line%%MOUNTPOINT*}"
        MOUNTPOINT_col="${#MOUNTPOINT_col}" # Required to ensure not mounted.
        continue
    fi

    Line="$Line$SPACES"                     # Pad extra white space.
    Line=${Line:0:74}                       # Truncate to 74 chars for menu.

    AllPartsArr+=($i "$Line")               # Menu array entry = Tag# + Text.
    (( i++ ))

done < $tmpMenu                             # Read next "lsblk" line.

#
# Display whiptail menu in while loop until no errors, or escape,
# or valid partion selection .
#

DefaultItem=0

while true ; do

    # Call whiptail in loop to paint menu and get user selection
    Choice=$(whiptail \
        --title "Use arrow, page, home & end keys. Tab toggle option" \
        --backtitle "Mount Partition" \
        --ok-button "Select unmounted partition" \
        --cancel-button "Exit" \
        --notags \
        --default-item "$DefaultItem" \
        --menu "$MenuText" 24 80 16 \
        "${AllPartsArr[@]}" \
        2>&1 >/dev/tty)

    clear                                   # Clear screen.
    if [[ $Choice == "" ]]; then            # Escape or dialog "Exit".
        CleanUp
        exit 1;
     fi

    DefaultItem=$Choice                     # whiptail start option.
    ArrNdx=$(( $Choice * 2 + 1))            # Calculate array offset.
    Line="${AllPartsArr[$ArrNdx]}"          # Array entry into $Line.

    # Validation - Don't wipe out Windows or Ubuntu 16.04:
    # - Partition must be ext4 and cannot be mounted.

    if [[ "${Line:MOUNTPOINT_col:4}" != "    " ]] ; then
        echo "Partition is already mounted."
        read -p "Press <Enter> to continue"
        continue
    fi

    # Build "/dev/Xxxxx" FS name from "├─Xxxxx" menu line
    MountDev="${Line%% *}"
    MountDev=/dev/"${MountDev:2:999}"

    # Build File System Type
    MountType="${Line:FSTYPE_col:999}"
    MountType="${MountType%% *}"

    break                                   # Validated: Break menu loop.

done                                        # Loop while errors.

#
# Mount partition
#

echo ""
echo "====================================================================="
mount -t auto $MountDev $MountName


# Display partition information.
echo "Mount Device=$MountDev" > $tmpInfo
echo "Mount Name=$MountName" >> $tmpInfo
echo "File System=$MountType" >> $tmpInfo

# Build Mount information (the partition selected for cloning to)
LineCnt=$(ls $MountName | wc -l)
if (( LineCnt > 2 )) ; then 
    # More than /Lost+Found exist so it's not an empty partition.
    if [[ -f $MountName/etc/lsb-release ]] ; then
        cat $MountName/etc/lsb-release >> $tmpInfo
    else
        echo "No LSB-Release file on Partition." >> $tmpInfo
    fi
else
    echo "Partition appears empty" >> $tmpInfo
    echo "/Lost+Found normal in empty partition" >> $tmpInfo
    echo "First two files/directories below:" >> $tmpInfo
    ls $MountName | head -n2 >> $tmpInfo
fi

sed -i 's/DISTRIB_//g' $tmpInfo      # Remove DISTRIB_ prefix.
sed -i 's/=/:=/g' $tmpInfo           # Change "=" to ":="
sed -i 's/"//g' $tmpInfo             # Remove " around "Ubuntu 16.04...".

# Align columns from "Xxxx:=Yyyy" to "Xxxx:      Yyyy"
cat $tmpInfo | column -t -s '=' > $tmpWork
cat $tmpWork > $tmpInfo

# Mount device free bytes
df -h --output=size,used,avail,pcent "$MountDev" >> $tmpInfo

# Display partition information.
cat $tmpInfo

CleanUp                             # Remove temporary files

exit 0

umount-menu.sh卸载驱动器/分区

重复脚本的文件创建/执行位标记过程umount-menu.sh。此脚本仅卸载由安装的驱动器/分区mount-menu.sh。它具有相同的选择菜单并完成并显示以下消息:

=====================================================================

/dev/nvme0n1p10 mounted on /mnt/mount-menu.FPRAW unmounted.

要调用脚本,请使用:sudo umount-menu.sh

umount-menu.shbash 脚本:

!/bin/bash

# NAME: umount-menu.sh
# PATH: /usr/local/bin
# DESC: Select mounted partition for unmounting
# DATE: May 10, 2018. Modified May 11, 2018.

# $TERM variable may be missing when called via desktop shortcut
CurrentTERM=$(env | grep TERM)
if [[ $CurrentTERM == "" ]] ; then
    notify-send --urgency=critical \ 
                "$0 cannot be run from GUI without TERM environment variable."
    exit 1
fi

# Must run as root
if [[ $(id -u) -ne 0 ]] ; then echo "Usage: sudo $0" ; exit 1 ; fi

#
# Create unqique temporary file names
#

tmpMenu=$(mktemp /mnt/mount-menu.XXXXX)   # Menu list

#
# Function Cleanup () Removes temporary files
#

CleanUp () {
    [[ -f "$tmpMenu" ]] && rm -f "$tmpMenu" #  at various program stages
}


#
# Mainline
#

lsblk -o NAME,FSTYPE,LABEL,SIZE,MOUNTPOINT > "$tmpMenu"

i=0
SPACES='                                                                     '
DoHeading=true
AllPartsArr=()      # All partitions.

# Build whiptail menu tags ($i) and text ($Line) into array

while read -r Line; do
    if [[ $DoHeading == true ]] ; then
        DoHeading=false                     # First line is the heading.
        MenuText="$Line"                    # Heading for whiptail.
        MOUNTPOINT_col="${Line%%MOUNTPOINT*}"
        MOUNTPOINT_col="${#MOUNTPOINT_col}" # Required to ensure mounted.
        continue
    fi

    Line="$Line$SPACES"                     # Pad extra white space.
    Line=${Line:0:74}                       # Truncate to 74 chars for menu.

    AllPartsArr+=($i "$Line")               # Menu array entry = Tag# + Text.
    (( i++ ))

done < "$tmpMenu"                           # Read next "lsblk" line.

#
# Display whiptail menu in while loop until no errors, or escape,
# or valid partion selection .
#

DefaultItem=0

while true ; do

    # Call whiptail in loop to paint menu and get user selection
    Choice=$(whiptail \
        --title "Use arrow, page, home & end keys. Tab toggle option" \
        --backtitle "Mount Partition" \
        --ok-button "Select unmounted partition" \
        --cancel-button "Exit" \
        --notags \
        --default-item "$DefaultItem" \
        --menu "$MenuText" 24 80 16 \
        "${AllPartsArr[@]}" \
        2>&1 >/dev/tty)

    clear                                   # Clear screen.

    if [[ $Choice == "" ]]; then            # Escape or dialog "Exit".
        CleanUp
        exit 1;
     fi

    DefaultItem=$Choice                     # whiptail start option.
    ArrNdx=$(( $Choice * 2 + 1))            # Calculate array offset.
    Line="${AllPartsArr[$ArrNdx]}"          # Array entry into $Line.

    if [[ "${Line:MOUNTPOINT_col:15}" != "/mnt/mount-menu" ]] ; then
        echo "Only Partitions mounted by mount-menu.sh can be unounted."
        read -p "Press <Enter> to continue"
        continue
    fi

    # Build "/dev/Xxxxx" FS name from "├─Xxxxx" menu line
    MountDev="${Line%% *}"
    MountDev=/dev/"${MountDev:2:999}"

    # Build Mount Name
    MountName="${Line:MOUNTPOINT_col:999}"
    MountName="${MountName%% *}"

    break                                   # Validated: Break menu loop.

done                                        # Loop while errors.

#
# Unmount partition
#

echo ""
echo "====================================================================="
umount "$MountName" -l                      # Unmount the clone
rm  -d "$MountName"                         # Remove clone directory

echo $(tput bold)                           # Set to bold text
echo $MountDev mounted on $MountName unmounted.
echo $(tput sgr0)                           # Reset to normal text

CleanUp                                     # Remove temporary files

exit 0

相关内容