Last active
July 7, 2023 15:40
-
-
Save tosyu/2280625a796271b8109b754aac1b7ca8 to your computer and use it in GitHub Desktop.
image mounting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
WHICH=/bin/which | |
REALPATH=$($WHICH realpath) | |
SED=$($WHICH sed) | |
GPG=$($WHICH gpg) | |
MV=$($WHICH mv) | |
LN=$($WHICH ln) | |
LOSETUP=$($WHICH losetup) | |
SUDO=$($WHICH sudo) | |
MOUNT=$($WHICH mount) | |
MKTEMP=$($WHICH mktemp) | |
CHOWN=$($WHICH chown) | |
ID=$($WHICH id) | |
CUT=$($WHICH cut) | |
REV=$($WHICH rev) | |
RM=$($WHICH rm) | |
FILE=$($REALPATH $1) | |
FILE_N=$(echo $FILE | $SED -e 's/\.gpg//g') | |
SYM=$FILE_N-dir | |
EXT=$(echo $FILE | $REV | $CUT -f1 -d. | $REV) | |
if [ ! -f "$FILE" ]; then | |
echo "ERROR $FILE does not exist" | |
exit 1 | |
fi | |
if [ "$EXT" = "gpg" ]; then | |
if [ -f "$FILE_N" ]; then | |
$RM -f $FILE_N | |
fi | |
$GPG -q -d -o $FILE_N $FILE | |
if [ $? -ne 0 ]; then | |
echo "could not decrypt file" | |
exit 1 | |
fi | |
FILE=$FILE_N | |
fi | |
DIR=$($MKTEMP -d) | |
if [ $? -ne 0 ]; then | |
echo "could not create temporary location" | |
exit 1 | |
fi | |
$SUDO $LOSETUP -f $FILE | |
if [ $? -ne 0 ]; then | |
echo "could not find loop device" | |
exit 1 | |
fi | |
DEV=$($LOSETUP -j $FILE | $CUT -f1 -d:) | |
$SUDO $MOUNT $DEV $DIR | |
if [ $? -ne 0 ]; then | |
echo "could not mount device" | |
exit 1 | |
fi | |
$SUDO $CHOWN $UID:$($ID -g -n $UID) $DIR | |
if [ $? -ne 0 ]; then | |
echo "could not change access rights" | |
exit 1 | |
fi | |
$LN -s $DIR $SYM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
WHICH=/bin/which | |
REALPATH=$($WHICH realpath) | |
SED=$($WHICH sed) | |
GPG=$($WHICH gpg) | |
MV=$($WHICH mv) | |
LN=$($WHICH ln) | |
LOSETUP=$($WHICH losetup) | |
SUDO=$($WHICH sudo) | |
MOUNT=$($WHICH mount) | |
UMOUNT=$($WHICH umount) | |
MKTEMP=$($WHICH mktemp) | |
CHOWN=$($WHICH chown) | |
ID=$($WHICH id) | |
CUT=$($WHICH cut) | |
REV=$($WHICH rev) | |
RM=$($WHICH rm) | |
GREP=$($WHICH grep) | |
FILE=$($REALPATH $1) | |
FILE_N=$(echo $FILE | $SED -e 's/\.gpg//g') | |
SYM=$FILE_N-dir | |
EXT=$(echo $FILE | $REV | $CUT -f1 -d. | $REV) | |
if [ ! -f "$FILE" ]; then | |
echo "$FILE does not exist" | |
exit 1 | |
fi | |
if [ "$EXT" = "gpg" ]; then | |
$MV -f $FILE $FILE.bak | |
FILE=$FILE_N | |
fi | |
DEV=$($LOSETUP -j $FILE | $CUT -f1 -d:) | |
FOLDER=`$MOUNT | $GREP "$($LOSETUP -j $FILE | $CUT -f1 -d:)" | $CUT -f3 -d" "` | |
$SUDO $UMOUNT $DEV | |
if [ $? -ne 0 ]; then | |
echo "could not unmount" | |
exit 1 | |
fi | |
$SUDO $LOSETUP -d $DEV | |
if [ $? -ne 0 ]; then | |
echo "could not detatch device" | |
exit 1 | |
fi | |
$RM -rf $FOLDER | |
if [ $? -ne 0 ]; then | |
echo "could not remove folder" | |
exit 1 | |
fi | |
$RM -f $SYM | |
if [ "$EXT" = "gpg" ]; then | |
$GPG -q -c -o $FILE.gpg $FILE | |
if [ $? -eq 0 ]; then | |
$RM -f $FILE | |
$RM -f $FILE.bak | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment