2

Condition: find reliably device name where disk label (MasiWeek) and disk size (2 TB) are known
Motivation: trying to determine what Ubuntu's GUI button mount does
Characteristics of the system

  • Disk label is the name of the disc given by the user. It is listed in /media/masi/ if mounted correctly.

  • Command lsblk -no name,label,partlabel gives

    sda                    
    ├─sda1                 
    ├─sda2                 
    └─sda3                 
    sdb                    
    └─sdb1 MasiWeek 
    
  • I know the disk label is MasiWeek and its size is 2 TB, visible in the command as 1.8T. I want to find reliably such a disc such that I can do the following where I need the info for the variable $label

    # https://askubuntu.com/a/593375/25388
    partition=$(basename $(readlink $label))
    sudo mkdir /media/$USER/$label
    sudo mount /dev/$partition /media/$USER/$label
    

System: Linux Ubuntu 16.04 64 bit
Related: What is the Equivalent Command to Ubuntu's GUI “Mount”?

8
  • 2
    Dude, as I said, you're trying to reinvent the wheel... Run man udisksctl and read the instructions there (it's what your "gui button" does underneath, to answer both questions). Commented Jul 21, 2016 at 16:25
  • 1
    If you're using Ubuntu, the GUI almost certainly uses the gvfs-mount command: you can run gvfs-mount --list --detail and look for the unix-device field, and mount it (as user, not root) with something like gvfs-mount -d /dev/sdb Commented Jul 21, 2016 at 16:47
  • 1
    @Masi I have attempted to answer in your related askubuntu question What is the Equivalent Command to Ubuntu's GUI “Mount”? Commented Jul 21, 2016 at 18:59
  • 1
    You're making this far more complicated than it needs to be. mount already understands both LABEL=name and UUID=uuid arguments as well as the /dev/xxxx arg. It also has -L and -U options. So, just use mount LABEL=MasiWeek /media/masi/MasiWeek or mount -L MasiWeek /media/masi/MasiWeek Commented Jul 27, 2016 at 9:33
  • 1
    i have no idea, i don't use gvfs-mount. but given that you say that gvfs-mount doesn't have a label option, I'l take a wild guess and say that the answer is "No". Commented Jul 27, 2016 at 13:03

2 Answers 2

1
  1. use mount's -L option or specify the mount device with LABEL=name.

    e.g.

    mount LABEL=MasiWeek /media/masi/MasiWeek

    or

    mount -L MasiWeek /media/masi/MasiWeek

    mount also has a -U option and understands UUID=uuid if you prefer to use the block device's UUID.

  2. The easiest way to get a list of all block devices, along with the LABEL and/or UUID details (if any) is to use blkid. e.g.

    # blkid
    /dev/sda1: LABEL="kaliboot" UUID="c0182339-da69-4f30-b131-c2fdb778f6b0" TYPE="ext3" PARTUUID="6fb80985-01"
    /dev/sda2: UUID="4c367cee-8bed-41d5-b466-38c7f3a03330" TYPE="swap" PARTUUID="6fb80985-02"
    /dev/sda3: LABEL="kaliroot" UUID="6bb6d228-0581-49ae-9d49-dd148c273ecc" TYPE="xfs" PARTUUID="6fb80985-03"
    

    Note that the swap partition has a UUID, but doesn't have a label. That's because I didn't bother to use the -L option when I created it with mkswap.

    Note also that this can be slow and produce lots of output (one line per block device) if you have lots of LVM LVs or ZFS ZVOLs (as I do on my main machine, which is why i used the output from another machine) or similar.

5
  • Is it more stable to use mkswap with the option label? Do you do it that way? Better usability later? Commented Jul 27, 2016 at 13:32
  • 1
    it makes no difference to stability. labels on swap partitions (and on filesystems too) are mostly cosmetic, also convenient so you don't have to remember the swap's /dev/ node or the long and ugly UUID. I only mentioned it in my answer to highlight the fact that labels are optional. Commented Jul 27, 2016 at 13:47
  • How do you use swaps in your system everyday? I do not understand how I could benefit from it when I just create the swap and use it. Commented Jul 27, 2016 at 13:49
  • 1
    that's how you use it. you create the swap device, run swapon (or put it in your /etc/fstab) and..........that's it. nothing more to do. your system now has more virtual memory (RAM plus swap) available. The kernel will swap stuff in and out of RAM as needed, automatically. You really don't need to do anything more (except maybe enable zswap as I mentioned in my answer to your other, related question). Commented Jul 27, 2016 at 13:53
  • if you meant "how do i use a swap label", you use it the same as you use a filesystem label. e.g. instead of something like /dev/sdaX none swap defaults,discard 0 0 in /etc/fstab, you'd use LABEL=swaplabel none swap defaults,discard 0 0. You can also use swapon -L swaplabel instead of, e.g., swapon /dev/sdaX. Commented Jul 27, 2016 at 13:54
1

If you only know the partition label and want to find the corresponding kernel name:

lsblk -rno label,name | awk '$1=="LABEL"{print $2}'

outputs something like sdb3. You can then mount the partition the same way your file manager does, via udisksctl:

udisksctl mount -b /dev/$(lsblk -rno label,name | awk '$1=="LABEL"{print $2}')

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.