了解 USB Thumbdrive 的自动安装并自己完成

了解 USB Thumbdrive 的自动安装并自己完成

我可以将其分为两个子组件:

  1. 为什么/如何这个自动安装程序创建(和销毁)它自己的安装点?

  2. 为什么我在自己执行此操作时必须手动创建自己的挂载点(或者我如何跳到自动执行此操作的方式)?

我不清楚将 USB 闪存盘插入系统时发生的确切过程。我看到发生了很多事情...

例如,插入旧的 USB2 1GB 记忆棒:

[76187.152010] usb 3-6: new high-speed USB device number 18 using ehci-pci
[76187.285314] usb 3-6: New USB device found, idVendor=1221, idProduct=3234
[76187.285317] usb 3-6: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[76187.285319] usb 3-6: Product: Flash Disk
[76187.285321] usb 3-6: Manufacturer: USB2.0
[76187.285323] usb 3-6: SerialNumber: 100000000000099E
[76187.285627] usb-storage 3-6:1.0: USB Mass Storage device detected
[76187.285704] scsi host27: usb-storage 3-6:1.0
[76188.285460] scsi 27:0:0:0: Direct-Access     USB2.0   Flash Disk       2.60 PQ: 0 ANSI: 2
[76188.285731] sd 27:0:0:0: Attached scsi generic sg11 type 0
[76188.286201] sd 27:0:0:0: [sdk] 2048000 512-byte logical blocks: (1.04 GB/1000 MiB)
[76188.291250] sd 27:0:0:0: [sdk] Write Protect is off
[76188.291255] sd 27:0:0:0: [sdk] Mode Sense: 0b 00 00 08
[76188.292333] sd 27:0:0:0: [sdk] No Caching mode page found
[76188.292337] sd 27:0:0:0: [sdk] Assuming drive cache: write through
[76188.296951]  sdk: sdk1
[76188.300321] sd 27:0:0:0: [sdk] Attached SCSI removable disk

并且它是这样安装的:

/dev/sdk1 on /media/madivad/5859-77E7 type vfat (rw,nosuid,nodev,uid=1000,gid=1000,shortname=mixed,dmask=0077,utf8=1,showexec,flush,uhelper=udisks2)

自动挂载过程中是否有某些内容为挂载过程创建(然后销毁)目录?

我是否能够在mount命令行上识别某些内容,以将其直接创建为流程的一部分,然后将其删除umount

这是您通常会自动生成脚本以记住首先创建的mount内容,然后umount再删除目录的内容吗?

我想这个过程中让我烦恼的一部分是我最终留下了忘记删除的目录,特别是当我测试东西时。

当我安装某些东西时,我希望这样(例如):

sudo mount /dev/sdk1 /mnt/usbkey1g

如果挂载点已创建和/或正在使用,我会收到警告,但更重要的是,如果它不存在,则即时创建它。同样,当我umount拿到钥匙时,将其取下。

我安装了桌面版本和服务器版本的 Ubuntu 14.04 LTS。

我在这里问这个问题而不是 AU,主要是因为我问这个问题与实际操作系统无关,并且一般参考 Linux,但它适用于我。如果 AU 更合适,请随意将其迁移到 AU。

干杯。

答案1

简而言之,Ubuntu 上发生的事情是udisksctl由 udev 规则调用的,然后将设备作为用户安装到媒体文件/media/$USER/diskname夹中。

出于所有实际目的,手动执行 udisksctl mount -b /dev/sdb1 就足够了

请记住将您的设备替换为真实名称

由于在您的问题中您还要求跨发行版解决方案,因此您最好的解决方案是命令mount,但是,下面是一个我基本上模拟udisksctl命令的脚本。它将创建动态安装的目录,并且只要有可用的 、 和 命令,就可以在任何发行版blkidmount工作awk

将源代码保存在中mountStuff.sh,使其可执行chmod +x /path/to/mountStuff.sh。为了避免不断地从完整路径运行脚本,建议将其存储在$PATH变量的目录中

运行如下:

skolodya@ubuntu:$ sudo ./mountStuff.sh /dev/sdb5                               
[sudo] password for xieerqi: 
Mounted /dev/sdb5 at /media/xieerqi/0ca7543a-5463-4a07-8bbe-233a7b0bd625
#!/usr/bin/env bash
# Author: Serg Kolo
# Date: January 23, 2016
# Script Description: Helper script that
# emulates behavior of udisksctl; reads blkid
# and creates a directory where to mount, based
# on disk label or uuid
#
#
# Copyright Sergiy Kolodyazhnyy 2016
#
# Permission to use, copy, modify, and distribute this software is hereby granted 
# without fee, provided that  the copyright notice above and this permission statement
# appear in all copies.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL 
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
# DEALINGS IN THE SOFTWARE.
if [ "$(id -u)" -ne 0  ];then
   printf ">>> ERR:Must be root"
   exit 1 
fi

if [ -z "$1"  ]; then
     printf ">>> ERR: Must have a parameter, e.g. /dev/sdb1"
     exit 1
fi

if [ "${1%/*}" != "/dev"   ];then
   printf ">>> ERR: Wrong parameter; Must be a device file"
   exit 1
fi

# split output of blkid into array;
# Element 0 is device, element 1 is LABEL or UUID, element 2 is filesystem 
  ARRAY=($( blkid | awk -v DEV="$1"  '$0~DEV{ gsub(/\"/,"");gsub(/\=/," "); print $1,$3,$NF  }'  ))
# the username who ran script as sudo given in $SUDO_USER
if [ ! -d "/media/$SUDO_USER/${ARRAY[1]}" ];
then
  mkdir "/media/$SUDO_USER/${ARRAY[1]}"
fi

mount -t "${ARRAY[2]}" -o rw,user "$1" "/media/$SUDO_USER/${ARRAY[1]}"

if [ "$?" -eq  "0"  ]; then
   printf "Mounted %s at %s" "$1" "/media/$SUDO_USER/${ARRAY[1]}"
fi

skolodya@ubuntu:$ sudo ./mountStuff.sh /dev/sdb5                               
[sudo] password for xieerqi: 
Mounted /dev/sdb5 at /media/xieerqi/0ca7543a-5463-4a07-8bbe-233a7b0bd625

相关内容