You are not logged in.

#1 2020-12-28 02:09:00

kudykennedy
Member
Registered: 2015-04-11
Posts: 21

Bash Executable / Failed to change directory after mounting / BOI

I have a native Binding of Isaac (BOI) in a Bash script that I have run successfully before, but not now.
Running ./Isaac.sh outputs this into the terminal

./Isaac.sh 
./Isaac.sh: line 62: cd: Mounted /dev/loop0 at /run/media/kudy/disk: No such file or directory
Failed to change directory to Mounted /dev/loop0 at /run/media/kudy/disk

Unmounting /dev/loop0... Unmounted /dev/loop0.

And these are the functions

#!/bin/sh
# {{{ INFO 
# vim: foldmethod=marker
# GASFA: Gazelle's Automatic SquashFs Application
# Main script
# Written by ZeDoCaixao
# Licence: WTFPL - http://www.wtfpl.net/about/
# }}} INFO

# {{{ Basic variables
VERSION=0.5.2
OFFSET=8192
IMAGE="$0"
RUNNING_DIR="$(pwd)"
read_usage () { USAGE=$(cat); }
read_usage <<EOF
USAGE:
    "$IMAGE" [APPLICATION OPTIONS]
    "$IMAGE" [--gasfa-options]

gasfa options:
    --gasfa-mount
        Only mount an image without running anything.
    --gasfa-extract [DIR]
       Extract the content of image to DIR.
       If DIR isn't specified, extract to "gasfa-content" in current directory.
    --gasfa-help
        Show this help message.

Can also be mounted with:
    sudo mount -o loop,offset=$OFFSET "$IMAGE" /mnt

gasfa-script version $VERSION
EOF
# }}} Basic variables

# {{{ Functions
fail () {
    echo "$2" >&2
    exit "$1"
}

image_mount () {
    LOOP="$(
        udisksctl loop-setup \
        --offset "$OFFSET" -f "$IMAGE" --no-user-interaction \
        | sed -e 's@.* \(/.*\)\.$@\1@'
    )"

    [ -z "$LOOP" ] && fail 1 "ERROR: Mounting $IMAGE failed"

    DIRECTORY="$(
        udisksctl info -b "$LOOP" | sed -ne 's/.*MountPoints: *\(.*\)/\1/p'
    )"

    [ -z "$DIRECTORY" ] && DIRECTORY="$(
        udisksctl mount -b "$LOOP" --no-user-interaction \
            | sed -e 's@.* \(/.*\)\.$@\1@'
    )"

    [ -z "$DIRECTORY" ] && fail 1 "Failed to mount an image to $LOOP"
    cd "$DIRECTORY" || fail 1 "Failed to change directory to $DIRECTORY"

    echo "Image mounted as $DIRECTORY"
}

image_unmount () {
    cd / || fail 1 "Can't cd to /"
    printf "\n%s" "Unmounting $LOOP... "
    UNMOUNTED=NO
    while [ $UNMOUNTED = NO ]; do
        if udisksctl unmount -b "$LOOP" --no-user-interaction ; then
            UNMOUNTED=YES
        else
            echo "Failed to unmount $LOOP" >&2
            echo "CD from the mounted directory in another terminal/FM" >&2
            echo "CD from the mounted directory and press ENTER" >&2
            read -r
        fi
    done
    udisksctl loop-delete -b "$LOOP" --no-user-interaction 2> /dev/null
}

image_run () {
    cd "$DIRECTORY" || fail 1 "Can't cd to $DIRECTORY"

    # Find the executable file
    [ -f "AppRun" ] && EXEC="./AppRun"

    [ -z "$EXEC" ] && [ -f "start.sh" ] && EXEC="./start.sh"

    [ -z "$EXEC" ]  \
        && [ "$(find . -maxdepth 1 -type f -executable | wc -l)" -eq 1 ] \
        && EXEC="$(find . -maxdepth 1 -type f -executable)"

    [ -z "$EXEC" ] && [ "$(uname -m)" = "x86_64" ] \
        && [ "$(find . -maxdepth 1 -type f -executable -name '*.x86_64' \
                | wc -l)" -eq 1 ] && EXEC="$(ls ./*.x86_64)"

    [ -z "$EXEC" ] \
        && [ "$(find . -maxdepth 1 -type f -executable -name '*.x86' \
                | wc -l)" -eq 1 ] && EXEC="$(ls ./*.x86)"

    [ -z "$EXEC" ] \
        && fail 64 "No executeable file found check your squashfs content"

    # Finally run the application
    if [ -z "$APPRUNNER" ]; then
        "$EXEC"  "$@"
        RESULT=$?
    else
        $APPRUNNER "$EXEC" "$@"
        RESULT=$?
    fi

    # Exit with the same result as the application exited with
    exit $RESULT
}
# }}} Functions

# {{{ Main script
[ "$1" = "--gasfa-help" ] || [ "$1" = "--gasfa-info" ] && {
    echo "$USAGE"
    exit 0
}

# Unmount the image if the script stops in any way
trap image_unmount EXIT
trap 'exit 127' 1 2 3 6 15
# Mount the image
image_mount

[ "$1" = "--gasfa-mount" ] && {
    while true; do
        sleep 600
    done
}
[ "$1" = "--gasfa-extract" ] && {
    cd "$RUNNING_DIR" || fail 1 "ERROR: Can't change directory to $RUNNING_DIR"
    TARGET="$2"
    [ -z "$TARGET" ] && TARGET=gasfa-content
    [ -d "$TARGET" ] && fail 1 "Can't extract to $TARGET - directory exists"
    [ -f "$TARGET" ] && \
        fail 1 "Can't extract to $TARGET - file with same name exists"

    printf "%s" "Now extracting $IMAGE data to $TARGET... "
    cp -R "$DIRECTORY" "$TARGET" || {
        echo "Failed extracting application data."
        fail 1 "Do you have enough drive space and permission to write here?"
    }
    echo "Done"
    exit 0
}

# And finally, if none --gasfa-* options given, run the application
image_run "$@"
# }}} Main script

(...what follows are garbage looking characters, presumably the actual program)

Line 62 is

cd "$DIRECTORY" || fail 1 "Failed to change directory to $DIRECTORY"

Running it creates a disk directory in my mnt/kudy folder, but then promptly removed
So I guess the mounting was successful but somehow the script could not access the folder?
And yes, I have chmod +x the file.

Last edited by kudykennedy (2020-12-28 02:12:12)

Offline

Board footer

Powered by FluxBB