如何发现 /dev/sdxx 的分区名称?

如何发现 /dev/sdxx 的分区名称?

我更喜欢尽可能多地使用命令行。Nautilus 有一个巧妙的技巧,应该可以通过命令行来实现,但我不知道该怎么做。

我将一个外部磁盘插入 USB 端口。它有一个名为“200503exfat”的 exFAT 分区。如果我没记错的话,我使用 macOS 磁盘实用程序应用程序为该分区命名。我想在 Linux 中挂载该分区,并且我想使用分区名称作为挂载点名称。我可以这样做:

$ lsblk | grep sd
sda           8:0    0 931,5G  0 disk 
├─sda1        8:1    0   200M  0 part 
└─sda2        8:2    0 931,3G  0 part  # Must be this partition. What's its name?
$ sudo mkdir /media/myusername/200503exfat  # I happen to know the name but I...
$ sudo mount /dev/sda2 !$
sudo mount /dev/sda2 /media/myusername/200503exfat #...don't want to hard code it.
FUSE exfat 1.2.8
$

我想将其放入 bash 脚本中,但我希望脚本发现分区名称(此处为 200503exfat)。问题就在这里。如果我启动 Nautilus,Bingo,我会在左侧窗格中看到名称“200503exfat”。因此,如果 Nautilus 可以做到这一点,那么在命令行中也应该可以做到,对吗?

这似乎是以前问过无数次的问题,但我找不到答案。我查看了 lsblk、fdisk、sfdisk、parted……当然还有谷歌。

短暂性脑缺血。

PS 对于可选的额外学分...Nautilus 如何知道 /dev/sda2 可安装但 /dev/sda1 不可安装?

答案1

在终端中输入blkid将显示标签以及其他有用信息。我的一些没有标签。

$ blkid
/dev/sda1: LABEL="Windows RE tools" UUID="8222C2A122C29995" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="9a07eb60-62a1-4531-a6ea-332b76c5f25c"
/dev/sda2: LABEL="SYSTEM" UUID="04C5-0D79" TYPE="vfat" PARTLABEL="EFI system partition" PARTUUID="120686dc-3078-4c71-b9e9-602f70ae69b1"
/dev/sda4: LABEL="Windows" UUID="B60AEC350AEBEFF3" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="3d050941-d614-4139-a9d8-9be3afec194b"
/dev/sda5: UUID="261A4F451A4F10EB" TYPE="ntfs" PARTUUID="9527964a-811b-4595-9d90-e105c0e7bfc6"
/dev/sda6: LABEL="Old_D" UUID="1e257c3b-a275-4b87-b6dc-bbfb945b8e5c" TYPE="ext4" PARTLABEL="Basic data partition" PARTUUID="55d4f90c-6da8-4650-aa37-744c0a338f2d"
/dev/sda7: LABEL="Recovery image" UUID="72D8CB52D8CB1371" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="81d7a341-55f3-4c4a-8409-fad88cbebaa5"
/dev/sdb1: UUID="0ca22edb-e6b0-412b-ad6e-00b2126b37c7" TYPE="ext4" PARTUUID="84e30647-bc14-4ca7-ba96-441ce8cd19de"
/dev/sdb2: UUID="adc97911-6d93-4094-8ec4-15cab05377e7" TYPE="swap" PARTUUID="4df44d3e-e24c-4130-aa21-5f2a63cff165"
/dev/sdb3: LABEL="home" UUID="e98fac97-277b-4e8a-907a-0f1bf4f428e6" TYPE="ext4" PARTUUID="de2793a9-1046-4e88-ad4a-56b389519cf3"
/dev/sdc1: LABEL="Old_E" UUID="1f0d243f-9dcd-41e8-b9da-2f75b0e3c356" TYPE="ext4" PARTUUID="2203843a-8ddf-8c44-600c-fc025d375992"
/dev/sdc2: LABEL="Old_F" UUID="27b594c2-4392-45ba-9d0f-fd586189d886" TYPE="ext4" PARTUUID="846cb5b3-1c37-f49f-0b74-5ab5300bc914"

lsblk使用选项也可以。

$ lsblk -o model,size,name,fstype,label,mountpoint 
MODEL              SIZE NAME   FSTYPE LABEL            MOUNTPOINT
TOSHIBA DT01ACA1 931.5G sda                            
                   800M ├─sda1 ntfs   Windows RE tools 
                   260M ├─sda2 vfat   SYSTEM           /boot/efi
                   128M ├─sda3                         
                 149.7G ├─sda4 ntfs   Windows          
                   350M ├─sda5 ntfs                    
                 759.3G ├─sda6 ext4   Old_D            /media/Win-D
                    21G └─sda7 ntfs   Recovery image   
WDC WD30EFRX-68E   2.7T sdb                            
                  73.2G ├─sdb1 ext4                    /
                  48.8G ├─sdb2 swap                    [SWAP]
                   2.6T └─sdb3 ext4   home             /home
Hitachi HDS5C404   3.7T sdc                            
                   1.7T ├─sdc1 ext4   Old_E            /media/Win-E
                     2T └─sdc2 ext4   Old_F            /media/Win-F
RAM GH95N         1024M sr0                            

答案2

我说过我想写一个 bash 脚本。现在就写到这里,特别感谢@Organic Marble 帮我解决了难题...

#!/bin/bash

# Discover and mount any and all un-mounted and labelled /dev/sdxx partitions.
# Special case: Ignore partitions named "EFI". These are system partitions.
# Mount point will be /media/$USERNAME/<partition-name>/ .
# If any given partition is already mounted, skip it.
# If there are no partitions to mount, do nothing silently and successfully.

# Caveat: I've written this for personal use only.  I am posting this code
# one hour after I wrote it, with minimal testing, so beware. That said, it
# does seem to work for me as intended.

# I welcome any comments or suggestions.

process_blkid_lines() {
    echo $blkid_line | grep -q LABEL= || continue  # no label? => skip
    label=$( echo $blkid_line | sed 's/^.* LABEL="//' | sed 's/".*//' )
    [[ -z "$label"      ]]     && continue  # no label?         => skip
    [[ "$label" = "EFI" ]]     && continue  # system partition? => skip
    dev_path=$( echo $blkid_line | awk -F: '{print $1}' )  # eg /dev/sda2
    mount | grep -q ^$dev_path && continue  # already mounted?  => skip
    mount_point=/media/$USERNAME/$label
    sudo mkdir -p $mount_point
    sudo mount $dev_path $mount_point
}

# On my system, blkid lives in /sbin/.
# I added /sbin to my $PATH to avoid "command not found".
# If you prefer, you could just change "blkid" to "/sbin/blkid".
sudo blkid | grep ^/dev/sd | process_blkid_lines

相关内容