source: /cluster/svnroot/bccd-ng/trunk/bin/extract_squashfs

Last change on this file was 6134, checked in by skylar, 17 months ago

merge addresses #1015

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1#!/usr/bin/env bash
2
3# $Id: extract_squashfs 6134 2019-06-05 22:00:18Z skylar $
4
5set -eo pipefail
6
7PATH=/bin:/sbin:/usr/bin:/usr/sbin
8
9function usage() {
10    echo -e "$(basename "$0") -i <path-to-bccd-iso> [ -t /path/to/tmp/dir ] \\n" >&2
11    echo "Extract the KNOPPIX/KNOPPIX squashfs to a temporary directory rooted in the path given in -t" >&2
12    echo "(defaults to /tmp)" >&2
13    echo "See http://bccd.net/wiki/index.php/DevelopmentInstructions#Regenerating_KNOPPIX.2FKNOPPIX_squashfs for more details" >&2
14}
15
16function cleanup() {
17    local RC="$1"
18    local ISO_MOUNT="$2"
19    local SQUASHFS_MOUNT="$3"
20    local ISO_DIR="$4"
21    local SQUASHFS_DIR="$5"
22
23    # Clear trap so we don't get in a loop
24    trap - INT TERM ERR EXIT
25
26    # Don't care about failure at this point
27    set +e
28
29    for DIR in ${ISO_MOUNT} ${SQUASHFS_MOUNT}; do
30        if mount | grep -q "${DIR}" > /dev/null; then
31            umount "${DIR}"
32            rmdir "${DIR}"
33        fi
34    done
35
36    # Only delete squashfs on failure
37    if [[ ${RC} -ne 0 ]]; then
38        if [[ ${SQUASHFS_DIR} =~ ^/tmp ]] && [[ -d "${SQUASHFS_DIR}" ]]; then
39            rm -fr "${SQUASHFS_DIR}"
40        fi
41        if [[ ${ISO_DIR} =~ ^/tmp ]] && [[ -d "${ISO_DIR}" ]]; then
42            rm -fr "${ISO_DIR}"
43        fi
44    fi
45
46    exit "${RC}"
47}
48
49# Cleanup temp dirs on error or exit, using $? as exit
50trap 'cleanup $? ${ISO_MOUNT} ${SQUASHFS_MOUNT} ${ISO_DIR} ${SQUASHFS_DIR}' \
51    INT TERM ERR EXIT
52
53TMPDIR="/tmp"
54
55while getopts "hi:t:" option; do
56    case "${option}" in
57        h )
58            usage
59            exit 1
60            ;;
61        i )
62            ISO_PATH="${OPTARG}"
63            ;;
64        t )
65            TMPDIR="${OPTARG}"
66            ;;
67        * )
68            usage
69            exit 1
70            ;;
71    esac
72done
73
74export TMPDIR
75
76if [[ "$(whoami)" != "root" ]]; then
77    echo "$(basename "$0") must be run as root!" >&2
78    exit 2
79elif [[ -z ${ISO_PATH} ]]; then
80    echo "Supply all arguments!" >&2
81    usage
82    exit 1
83elif [[ ! -f "${ISO_PATH}" ]] || [[ ! -r "${ISO_PATH}" ]]; then
84    echo "${ISO_PATH} does not exist or is unreadable" >&2
85    exit 2
86fi
87
88ISO_MOUNT=$(mktemp -d -p "${TMPDIR}" bccd_mount.XXXXXX)
89SQUASHFS_MOUNT=$(mktemp -d -p "${TMPDIR}" squashfs_mount.XXXXXX)
90ISO_DIR=$(mktemp -d -p "${TMPDIR}" bccd.XXXXXX)
91SQUASHFS_DIR=$(mktemp -d -p "${TMPDIR}" squashfs.XXXXXX)
92SQUASHFS="${ISO_DIR}/live/filesystem.squashfs"
93
94echo "Mounting ${ISO_PATH} to ${ISO_MOUNT}" >&2
95mount "${ISO_PATH}" "${ISO_MOUNT}" -o loop,ro
96
97echo "Copying ${ISO_MOUNT} to ${ISO_DIR}" >&2
98tar -C "${ISO_MOUNT}" -cf - . | tar -C "${ISO_DIR}" -xf -
99
100if [[ ! -f "${SQUASHFS}" ]]; then
101    echo "${SQUASHFS} does not exist, not BCCD ISO?" >&2
102    exit 2
103fi
104
105echo "Mounting ${SQUASHFS} to ${SQUASHFS_MOUNT}" >&2
106mount "${SQUASHFS}" "${SQUASHFS_MOUNT}" -o loop,ro
107
108echo "Copying ${SQUASHFS_MOUNT} to ${SQUASHFS_DIR}" >&2
109tar -C "${SQUASHFS_MOUNT}" -cf - . | tar -C "${SQUASHFS_DIR}" -xf -
110
111echo "ISO: ${ISO_DIR}"
112echo "SQUASHFS: ${SQUASHFS_DIR}"
Note: See TracBrowser for help on using the repository browser.