aboutsummaryrefslogtreecommitdiff
path: root/share/hydractl/mount-media
blob: d6adf70c34d918b5254d7e7d58fb642bb6a0f586 (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
47
48
49
50
51
#!/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 config
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