aboutsummaryrefslogtreecommitdiff
path: root/share/hydractl/mount-media
blob: 0073292d1f7c303d7035aaeedd68d76e2d94efeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
#
# mount-media
#

# Parameters
MEDIA="$1"
VOLUME="$2"
BASENAME="`basename $0`"
MOUNTPOINT="/media/$MEDIA"

# Check media config
if [ -z "$MEDIA" ]; then
  echo "usage: $BASENAME <media> [volume]"
  echo "example: $BASENAME mymedia sdb1"
  exit 1
fi

# Check volume config
if [ -z "$VOLUME" ]; then
  VOLUME="sdb1"
fi

DISK="`echo ${VOLUME} | sed -e s/[0-9]\$//g`"

# Set sudo config
if [ "`whoami`" != 'root' ]; then
  sudo="sudo"
fi

if [ "$BASENAME" == "mount-media" ]; then
  echo "Checking drive health status..."
  $sudo smartctl -H /dev/$DISK
  echo "Disabling STANDBY on drive..."
  $sudo sdparm --clear STANDBY -6 /dev/$DISK
  echo "Initializing crypto layer..."
  $sudo cryptsetup luksOpen /dev/$VOLUME $MEDIA && \
  echo "Checking filesystem..."
  $sudo fsck -v -y /dev/mapper/$MEDIA && \
  echo "Mounting volume at $MOUNTPOINT..."
  $sudo mkdir -p $MOUNTPOINT
  $sudo mount /dev/mapper/$MEDIA $MOUNTPOINT
elif [ "$BASENAME" == "umount-media" ]; then
  $sudo umount $MOUNTPOINT && \
  $sudo cryptsetup luksClose $MEDIA
fi