aboutsummaryrefslogtreecommitdiff
path: root/share/hydractl/mount-media
blob: c25eee372e24073b527c4acae58a4fb11ca014d3 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
#
# mount-media
#

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

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

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

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

# Disk and device config
DISK="/dev/`echo ${VOLUME} | sed -e s/[0-9]\$//g`"
DEVICE="/dev/$VOLUME"

# Check if LVM
if $sudo lvdisplay /dev/$MEDIA/root &> /dev/null; then
  LVM="yes"
  DEVICE="/dev/$MEDIA/root"
  MAPPING="$MAPPING-unlocked"
fi

if [ "$BASENAME" == "mount-media" ]; then
  if [ -e "$DEVICE" ]; then
    echo "Checking drive health status..."
    $sudo smartctl -H $DISK

    echo "Disabling STANDBY on drive..."
    $sudo sdparm --clear STANDBY -6 $DISK
  fi

  if [ "$LVM" == "yes" ]; then
    $sudo vgchange -a y $MEDIA
  fi

  echo "Initializing crypto layer..."
  $sudo cryptsetup luksOpen $DEVICE $MAPPING && \

  echo "Checking filesystem..."
  $sudo fsck -v -y /dev/mapper/$MAPPING && \

  echo "Mounting volume at $MOUNTPOINT..."
  $sudo mkdir -p $MOUNTPOINT
  $sudo mount /dev/mapper/$MAPPING $MOUNTPOINT
elif [ "$BASENAME" == "umount-media" ]; then
  $sudo umount $MOUNTPOINT && \
  $sudo cryptsetup luksClose $MAPPING

  if [ "$LVM" == "yes" ]; then
    $sudo vgchange -a n $MEDIA
  fi
fi