#!/bin/bash #en /usr/local/bin/mios_antiguos/mount_cryptas está el antiguo script, pensado para montar en bucle (/dev/loop) BUSCAR="$1" function preparacion () { echo -n "Preparando. " LINEA_FSTAB=`egrep -v "^[[:space:]]*$|^#" /etc/fstab | grep "$BUSCAR"` LINEA_CR=`egrep -v "^[[:space:]]*$|^#" /etc/crypttab | grep "$BUSCAR"` # echo "Info /etc/crypttab: " $LINEA_CR # echo "Info /etc/fstab: " $LINEA_FSTAB if test -z "$LINEA_CR" -a -z "$LINEA_FSTAB" ; then echo -e "\nEntry not found in /etc/crypttab nor /etc/fstab" exit 2 fi # Either we have the crypttab data or the fstab data in $1. Grep for one, locate the corresponding other. if test -n "$LINEA_CR" ; then # We start with a crypttab match. echo -n "C" ALT_IFS="$IFS" IFS=" " set $LINEA_CR CR_NAME="$1" CR_DEVICE="$2" CR_P1="$3" CR_P2="$4" LINEA_FSTAB=`egrep -v "^[[:space:]]*$|^#" /etc/fstab | grep "$CR_NAME"` #echo -e -"\n$CR_DEVICE"-+-"$LINEA_FSTAB"- if test -n "$LINEA_FSTAB" ; then echo -n "f" ALT_IFS="$IFS" IFS=" " set $LINEA_FSTAB FSTAB_DEVICE="$1" FSTAB_MOUNT="$2" FSTAB_TYPE="$3" FSTAB_PARAM="$4" FSTAB_P1="$5" FSTAB_P2="$6" IFS=$ALT_IFS else echo -e "\nCrosover error with /etc/fstab" exit 2 fi else # We don't have a crypttab match, so we try with fstab. if test -n "$LINEA_FSTAB" ; then echo -n "F" #echo -e "\n$LINEA_FSTAB" ALT_IFS="$IFS" IFS=" " set $LINEA_FSTAB FSTAB_DEVICE="$1" FSTAB_MOUNT="$2" FSTAB_TYPE="$3" FSTAB_PARAM="$4" FSTAB_P1="$5" FSTAB_P2="$6" IFS=$ALT_IFS # fstab device is like "/dev/mapper/cr_Datum", but crypttab name is only "cr_Datum" NOMBRE=`basename $FSTAB_DEVICE` #echo $NOMBRE LINEA_CR=`egrep -v "^[[:space:]]*$|^#" /etc/crypttab | grep "$NOMBRE"` #echo "-$LINEA_CR-" if test -n "$LINEA_CR" ; then echo -n "c" ALT_IFS="$IFS" IFS=" " set $LINEA_CR CR_NAME="$1" CR_DEVICE="$2" CR_P1="$3" CR_P2="$4" IFS=$ALT_IFS else echo -e "\nCrosover error with /etc/crypttab" exit 2 fi fi fi echo -e "." #CR: cr_Datum /dev/disk/by-uuid/39334baa-941f-42ab-a0a4-7c20a427aff2 none noauto #FSTAB: /dev/mapper/cr_Datum /data/Datum xfs noauto,nofail 1 5 if ! test -b $CR_DEVICE ; then echo "Device $CR_DEVICE is not an special block or it is not present." if test -f $CR_DEVICE ; then TESTLUK=`file $CR_DEVICE | grep "LUKS encrypted file"` if test -n "$TESTLUK" ; then echo " But it is a LUKS image file instead, proceeding." else echo " It is a regular file, but it contains no LUKS image." exit 3 fi else echo " It does not exist as a regular file, either" exit 3 fi fi if ! test "/dev/mapper/$CR_NAME" = "$FSTAB_DEVICE" ; then echo "Device mismatch between entries in fstab ($FSTAB_DEVICE) and crypttab (/dev/mapper/$CR_NAME)." exit 3 fi MOUNTED=`mount | grep $CR_NAME` MAPPED=`/sbin/dmsetup ls | grep $CR_NAME` } #if ! test "" -EQ "" ; then # echo "" # exit 3 #fi function informacion () { echo echo "===> Dumping the header information of the LUKS device ($CR_DEVICE - $CR_NAME):" echo cryptsetup luksDump $CR_DEVICE echo echo "===> Status report of $CR_NAME:" #echo cryptsetup status $CR_NAME echo "===> Disk device is:" blkid $CR_DEVICE echo "===> Mapped device:" #dmsetup ls | grep $CR_NAME echo $MAPPED blkid /dev/mapper/$CR_NAME echo "===> Device type (if you see 'data' something is wrong)" file --dereference --special-files /dev/mapper/$CR_NAME echo "===> Is device mounted?" mount | grep $CR_NAME echo echo "===> disk space used:" if ! test -z "$MOUNTED" ; then df -h $FSTAB_MOUNT fi } function mapealo () { if ! test -z "$MOUNTED" ; then echo "Already mounted." else if test -z "$MAPPED" ; then cryptsetup luksOpen $CR_DEVICE $CR_NAME else echo "Already mapped." fi fi } function desmapealo () { if test -n "$MOUNTED" ; then #echo "umounting device first; attempting to umount it first" umount /dev/mapper/$CR_NAME if test $? -eq 0 ; then cryptsetup remove $CR_NAME else echo "Impossible to umount, probably busy device." lsof /dev/mapper/$CR_NAME fi else cryptsetup remove $CR_NAME fi } function montalo () { if ! test -z "$MOUNTED" ; then echo "Already mounted." else if test -z "$MAPPED" ; then cryptsetup luksOpen $CR_DEVICE $CR_NAME mount $FSTAB_MOUNT else mount $FSTAB_MOUNT fi fi } function desmontalo () { if test -n "$MOUNTED" ; then #echo "Attempting to umount." umount /dev/mapper/$CR_NAME if test $? -ne 0 ; then echo "Impossible to umount, probably busy device." lsof /dev/mapper/$CR_NAME fi echo "Warning: device is umounted but still mapped." else echo "Device is already umounted; requested operation is pointless." fi } function comprobacion () { echo "Mounted: -$MOUNTED- mapped: -$MAPPED-" echo if test -n "$MOUNTED" ; then echo "Device is mounted; attempting to umount for fsck." umount /dev/mapper/$CR_NAME if test $? -eq 0 ; then echo "Doing fsck on device..." fsck /dev/mapper/$CR_NAME echo "Mounting device again." mount /dev/mapper/$CR_NAME else echo "Impossible to umount, probably busy device." lsof /dev/mapper/$CR_NAME fi else # Not mounted. echo "Device is not mounted." if test -n "$MAPPED" ; then echo "Device is mapped but not mounted; doing fsck on device..." fsck /dev/mapper/$CR_NAME else echo "Device is not even mapped; it has to be mapped to run fsck on it." cryptsetup luksOpen $CR_DEVICE $CR_NAME if test $? -eq 0 ; then echo "Doing fsck on device..." fsck /dev/mapper/$CR_NAME echo "Call 'crypto_unmap $CR_NAME' to unmap it if you are done." fi fi fi } function listar () { echo "Currently available devices are:" echo /sbin/dmsetup ls echo echo "Possible devices are:" echo egrep -v "^[[:space:]]*$|^#" /etc/crypttab | cut -f 1 } function ayuda () { echo "Does several operations on a a crypto device." echo " crypto_ --> help " echo " crypto_lst --> lists devices" echo " crypto_fsck [DEVMAP|MOUNT_POINT] --> filesystem check" echo " crypto_sinfo [DEVMAP|MOUNT_POINT] --> device information" echo " crypto_info [DEVMAP|MOUNT_POINT] --> device information" echo " crypto_mount [DEVMAP|MOUNT_POINT] --> mount device" echo " crypto_umount [DEVMAP|MOUNT_POINT] --> umounts device (without unmaping it)" echo " crypto_map [DEVMAP|MOUNT_POINT] --> map device without mounting it" echo " crypto_unmap [DEVMAP|MOUNT_POINT] --> unmap (and umount) device" } function scheck () { if test -n "$MAPPED" -a -n "$MOUNTED" ; then echo -e "\nDevice is mapped on /dev/mapper/$CR_NAME and mounted on $FSTAB_MOUNT" echo -n " " blkid /dev/mapper/$CR_NAME echo -n "Disk device is: " blkid $CR_DEVICE echo -e "\n Status:" cryptsetup status $CR_NAME echo -e "\n Space:" df -h $FSTAB_MOUNT else if test -n "$MAPPED" ; then echo "Device is mapped on /dev/mapper/$CR_NAME" echo cryptsetup status $CR_NAME fi if test -n "$MAPPED" -a -n "$MOUNTED" ; then echo "Device is mounted on $FSTAB_MOUNT" fi fi } function check () { if test -z $BUSCAR ; then echo "Error: Missing paramenter." ayuda exit 1 else preparacion fi } # umount_cryptas es un symlink o un hardlink a mount_cryptas # Según el nombre se elije uno u otro comportamiento :-) case "$0" in *crypto_) ayuda ;; *crypto_lst) listar exit 0 ;; *crypto_umount) check desmontalo ;; *crypto_mount) check # echo "Debug: comprobado" montalo ;; *crypto_map) check mapealo ;; *crypto_unmap) check desmapealo ;; *crypto_sinfo) check scheck ;; *crypto_info) check informacion ;; *crypto_fsck) check comprobacion ;; *) echo "Error: internal error, must programm it" ; exit 1 ;; esac