如何在不知道挂载点的情况下判断设备 UUID 已挂载?

如何在不知道挂载点的情况下判断设备 UUID 已挂载?

系统:Linux Mint 19.1 Cinnamon 64位,基于Ubuntu 18.04 LTS。


我想知道是否可以获得以下信息:

这个UUID(块设备的)是否已安装? (不知道挂载点)

不过我已经玩了半天了,还是想不通。

我至少创造了一些下面的工作代码会卸载并关闭两个 USB 硬盘。


我的代码的当前临时版本如下所示:

dismount_and_poweroff_external_drives()
{
    name_external_drive_500gb_ntfs='500GB NTFS USB 2.0 HDD'
    name_external_drive_2_0tb_ext4='2.0TB Ext4 USB 3.0 HDD'
    uuid_external_drive_500gb_ntfs='xxxxxxxxxxxxxxxx' # censored
    uuid_external_drive_2_0tb_ext4='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # censored
    path_external_drive_500gb_ntfs="/dev/disk/by-uuid/${uuid_external_drive_500gb_ntfs}"
    path_external_drive_2_0tb_ext4="/dev/disk/by-uuid/${uuid_external_drive_2_0tb_ext4}"

    tput bold; tput setaf 3; printf '%b' "\\n${name_external_drive_500gb_ntfs} un-mount\\n"; tput sgr0
    # info test ‘-b FILE’: True if FILE exists and is a block special device.
    if [ ! -b "${path_external_drive_500gb_ntfs}" ]
    then
        tput bold; tput setaf 4; printf '%b' "The device is not plugged in or powered on.\\n"; tput sgr0
    else
        if umount "${path_external_drive_500gb_ntfs}"
        then
            tput bold; tput setaf 2; printf '%b' "Un-mounting OK.\\n"; tput sgr0

            if udisksctl power-off --block-device "${path_external_drive_500gb_ntfs}"
            then
                tput bold; tput setaf 2; printf '%b' "Powering-off OK.\\n"; tput sgr0
            else
                tput bold; tput setaf 1; printf '%b' "Powering-off Failed.\\n"; tput sgr0
            fi

        else
            tput bold; tput setaf 1; printf '%b' "Un-mounting Failed.\\n"; tput sgr0
        fi
    fi

    printf '\n'

    tput bold; tput setaf 3; printf '%b' "\\n${name_external_drive_2_0tb_ext4} un-mount\\n"; tput sgr0
    # info test ‘-b FILE’: True if FILE exists and is a block special device.
    if [ ! -b "${path_external_drive_2_0tb_ext4}" ]
    then
        tput bold; tput setaf 4; printf '%b' "The device is not plugged in or powered on.\\n"; tput sgr0
    else
        if umount "${path_external_drive_2_0tb_ext4}"
        then
            tput bold; tput setaf 2; printf '%b' "Un-mounting OK.\\n"; tput sgr0

            if udisksctl power-off --block-device "${path_external_drive_2_0tb_ext4}"
            then
                tput bold; tput setaf 2; printf '%b' "Powering-off OK.\\n"; tput sgr0
            else
                tput bold; tput setaf 1; printf '%b' "Powering-off Failed.\\n"; tput sgr0
            fi

        else
            tput bold; tput setaf 1; printf '%b' "Un-mounting Failed.\\n"; tput sgr0
        fi
    fi

    printf '\n'
}

我忘了强调接受的解决方案必须是POSIX-ly 写的。

答案1

原创解决方案

UUID=<device_uuid>
mount | egrep $(readlink -f /dev/disk/by-uuid/${UUID}) && echo mounted

弗拉斯蒂米尔的笔记

  • -e使用帮助代替-f,可能是个好主意readlink

    -e, --canonicalize-existing   canonicalize by following every symlink in
                                  every component of the given name recursively,
                                  all components must exist
    

    相比于:

    -f, --canonicalize            canonicalize by following every symlink in
                                  every component of the given name recursively;
                                  all but the last component must exist
    

    据我了解,-e保证整个路径存在,可能更好,需要额外的验证或引用。不幸的是,-e发现该选项不可用POSIX- 合规,所以运气不好。将所有信息留在这里以供将来参考。

  • 中没有双引号原解,我建议将它们与一个尾随空格作为一种安全措施以避免匹配例如sda11或类似。

  • 人们还可以利用POSIX-定义fgrep匹配固定字符串,或者甚至更好地仅匹配以该设备开头的行grep "^dev_name"

  • 正如所指出的马克·普洛特尼克mount本身可能不是POSIX-定义,再次,引用会很方便,但无论如何我已经更改了代码以/proc/mounts直接读取。


合理的函数

结果函数用于检查 UUID 是否已安装可以看起来类似于:

is_uuid_mounted()
{
    readlink_output=$( readlink -f /dev/disk/by-uuid/"${1}" )
    [ -n "${readlink_output}" ] &&
        grep -F "${readlink_output} " /proc/mounts > /dev/null 2>&1
}

完整的工作脚本

#!/bin/sh

set -eu

translate_uuid_to_device_name()
{
    # Linux-specific; needs *BSD revision
    readlink -f -n /dev/disk/by-uuid/"${1}"
}

is_uuid_mounted()
{
    device_name=$( translate_uuid_to_device_name "${1}" )

    if [ -n "${device_name}" ]
    then
        # 1. basic regex should be working across platfotms
        #    tested on FreeBSD, OpenBSD, NetBSD with success
        #    I prefer the starting with (^) rather than filtering throung all text
        # 2. /proc/mounts is not available on all *BSDs, needs revision
        proc_mounts=$( grep "^${device_name} " /proc/mounts )
        [ -n "${proc_mounts}" ]
    fi
}

# Simplest Usage Example
if is_uuid_mounted "PUT_SOME_UUID_IN_HERE"
then
    echo "This UUID is mounted."
else
    echo "This UUID isn't mounted."
fi

请随意在评论中解决更多问题。

答案2

如果 findmnt 可用,您可以尝试:

test "$(findmnt -S UUID=$UUID)" || echo $UUID not mounted

相关内容