You are not logged in.

#1 2014-08-15 06:41:59

Ledti
Member
Registered: 2010-07-31
Posts: 122
Website

sd: bash script to mount /dev/sd* devices

Here's a rudimentary bash script I've been using to mount '/dev/sd*' devices since that's all I need to mount 99% of the time. I don't intend to add or change anything (works for me~) but feel free to fork it and post your changes here.

* source: https://gist.github.com/ledti/7eed3cb4356e20540c80.
* uses and requires udisks2 by default.
* by default it visually excludes '/dev/sda' from the listing. This can easily be changed by editing the following in the source, for example if you have two internal drives:

# visually exclude the following /dev/sd* device(s) from the listing:
# syntax example: "abc", to exclude drives a,b, and c.
# use "-" to show everything.
exclude="ab"

---

$ sd
Available devices:
/dev/sdc
/dev/sdd
/dev/sdd1
$ sd --help
Commands:
  -m: mount drive or partition.
  -u: unmount drive or partition.
  --help: show this help dialogue.
Example usage:
  sd        # list devices.
  sd -m c   # mount /dev/sdc.
  sd -m d1  # mount /dev/sdd1.
  sd -u d   # unmount /dev/sdd.
$ sd -m c
Mounted /dev/sdc at /run/media/user/VIMERA.
$ sd -u b1
Unmounted /dev/sdb1.

Last edited by Ledti (2014-08-15 19:53:30)

Offline

#2 2014-08-15 12:35:22

fijit
Member
Registered: 2012-07-06
Posts: 24

Re: sd: bash script to mount /dev/sd* devices

Shouldn't you check at line 19 that you are not trying to mount something from the excludes list, rather than just hardcoding "i will work on anything but a"?

Offline

#3 2014-08-15 16:58:52

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: sd: bash script to mount /dev/sd* devices

I'm using

usm ()
{
    DRIVE="/dev/sdb1";
    if mount | grep -q $DRIVE; then
        sudo umount $DRIVE;
    else
        sudo mount -o user,rw,umask=000 $DRIVE /media/usb;
    fi
}

but my needs are dead simple.

Offline

#4 2014-08-15 17:07:44

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,530
Website

Re: sd: bash script to mount /dev/sd* devices

Thanks Karol - I modified that slightly for my own use.  I had been using udevil, but as of a week or so ago it's not been properly unmounting devices and it leaves a dot-file in the mountpoint so they cannot be removed and just accumulate.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2014-08-15 19:01:24

Ledti
Member
Registered: 2010-07-31
Posts: 122
Website

Re: sd: bash script to mount /dev/sd* devices

fijit wrote:

Shouldn't you check at line 19 that you are not trying to mount something from the excludes list, rather than just hardcoding "i will work on anything but a"?

The excludes list was meant to be more about visually excluding certain block devices from the listing rather than preventing you from mounting/unmounting them at all. The bit at line 19 was just meant to make sure the syntax was correct-ish, but at the time I made it into another manual check just because my needs were static. I didn't remember until last night that it was even there.

I changed the wording to reflect that and I'll edit the original post now.

Offline

#6 2014-08-15 21:08:20

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,412
Website

Re: sd: bash script to mount /dev/sd* devices

Trilby wrote:

Thanks Karol - I modified that slightly for my own use.  I had been using udevil, but as of a week or so ago it's not been properly unmounting devices and it leaves a dot-file in the mountpoint so they cannot be removed and just accumulate.

Did you try running udevil clean ?

edit: I have some functions for udevil here, it runs sync/udevil clean maybe 60 times. neutral

Last edited by Alad (2014-08-15 21:13:54)


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

#7 2014-08-15 21:12:43

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,530
Website

Re: sd: bash script to mount /dev/sd* devices

Alad, thanks for the suggestion - I had not.  I never needed to do that before, but recently it started leaving those junk files after every mount/unmount.  While `udevil clean` might clean up the current mess, it wasn't immediately obvious how to prevent it from continuuing to make new messes.  I quite likely am just missing a new configuration setting that is needed, but just using the mount command is simpler for me know anyhow.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2014-08-16 21:28:38

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: sd: bash script to mount /dev/sd* devices

Instead of a exclude list, it might be more sensible to check for the `HintSystem` attribute if the intension is to exclude system devices.

EDIT:
I wrote this to mount/unmount all external disks -- if I wanted to do it to a certain disk, I'd just use udisksctl.

(removed)

EDIT:
v2 (superseded)

EDIT:
v3 -- add "is filesystem" check; allow specifying a list of devices to (un)mount.

#!/bin/bash

set -o pipefail
shopt -s extglob globasciiranges nullglob

disk_info() {
  udisksctl info -b "$1" | awk '
  {
    if ($1 == "IdUsage:" && $2 != "filesystem")
      exit 1
    if ($1 == "HintSystem:" && $2 == "true")
      printf("s")
    else if ($1 == "MountPoints:" && NF > 1)
      printf("m")
  }
  '
}

cmd=$1
shift || { printf 'error: no command given\n' >&2; exit 1; }
(( $# )) || set -- /dev/sd[a-z]+([0-9])
case $cmd in
  m*)
    for disk in "$@"; do
      if ! info=$(disk_info "$disk"); then
        printf 'warning: not a filesystem: %s\n' "$disk"
        continue
      fi
      if [[ $info != *[sm]* ]]; then
        udisksctl mount -b "$disk"
      fi
    done
    ;;
  u*)
    for disk in "$@"; do
      if ! info=$(disk_info "$disk"); then
        printf 'warning: not a filesystem: %s\n' "$disk"
        continue
      fi
      if [[ $info != *s* && $info == *m* ]]; then
        udisksctl unmount -b "$disk"
      fi
    done
    ;;
esac

Last edited by lolilolicon (2014-08-17 13:26:36)


This silver ladybug at line 28...

Offline

Board footer

Powered by FluxBB