#!/bin/bash # # mount-img-partitions # # Skeleton of a shell script # Does usage + help display, argument parsing and error reporting. # # Copyright (C) by Volker Kuhlmann # http://volker.top.geek.nz/contact.html # All rights reserved. # Released under the terms of the GNU General Public License (GPL) Version 2. # See http://www.gnu.org/ for details. # # Not released - This is an un-released work. # If you have a copy, it is an illegal one. Please delete all your copies # and inform the author above. # # Volker Kuhlmann # 1.0, 23 Apr 2015 Created. # 1.1, 25 Apr 2015 Error if no partitions are found to mount. # 1.2, 03 May 2015 Ensure C locale is used for fdisk output to parse. # Thanks to Wojciech ...@ire.pw.edu.pl for finding it. # chr0(){ echo @;};chr1(){ echo .;} VERSION="1.2, 03 May 2015" AUTHOR="Volker Kuhlmann " COPYRIGHT="Copyright (C) 2015" #### #### Check bash is version 2 and load functions #### unset VERSION_FUNCLIB_NUM checkbash2=1 requireversion=63 source functions_vk.bash || exit $? CMD_LOG_PREFIX="+ " #CALL_WITH_HELP_TEXT="" CMD_LOG_TRACE=1 #### #### Constants and initialised variables #### #### #### Variables #### #### #### Version, Usage, Help #### # # Call only these from application: Version, Usage, Help # show_usage() { echo " Usage: ${0##*/} [OPTIONS] IMAGE [MOUNT-OPTIONS] Version $VERSION $COPYRIGHT by $AUTHOR (Using functions_vk.bash $VERSION_FUNCLIB) " test -n "$1" && echo "$1 " } show_help() { show_usage echo "\ Mount all partitions in disk image IMAGE, with MOUNT-OPTIONS. OPTIONS: -h|--help Show help. --version Show version. -t|--test Only show what would be done. -l|--list List partitions in IMAGE, don't mount. Columns are: partition, start sector, end sector, size. -m PREFIX Create mount points PREFIX-XX and mount partitions. " } #### #### Error/Exit codes #### exitwith() { exec 1>&2 # write stdout on stderr instead exitwith_funclib "$@" case "$1" in # more codes in here ---> errNoPartitions) echo "No partitions found in '$2'" exit 2;; # <--- more codes in here *) exitwith_illegal "$@";; esac } #### #### Parse command line parameters #### parse_cmd_line() { local cmdline arg unset -v debug mountprefix listparts cmdline=("$@") test "$1" = "--debug" && debug=1 # Pre-check for debug. #set -- -opt1 "$@" # pre-init options while [ -n "$1" ]; do test $debug && echo "Current arg: $1" case "$1" in --) shift; break;; # Stop if word is "--". # -[^-]?*) arg="$1";; # "-long" or "--long" for long options. # -[^-][A-Za-ce-z]*) # "-long" or "--long", except for some # arg="$1";; # shortcuts. -*) arg="${1#-}";; # Strip one hyphen. *) break;; # Stop if word does not start with "-". esac test $debug && echo " cmd arg: $arg" case "$arg" in -debug) debug=1;; -version) Version;; -usage) Usage;; h|help|-help) Help;; t|-test) dryrun=1 _drycreate=1;; l|-list) listparts=1;; m) checkarg2 "$@"; mountprefix="$2"; shift;; "") break;; # allow "-" as file arg *) exitwith ErrBadoption "$1";; #*) break;; # stop option scanning with first unknown option esac shift done # save option and file arguments for later: FILEARGS=("$@") OPTARGS=("${cmdline[@]:0:${#cmdline[@]} - ${#FILEARGS[@]}}") } #### #### Functions #### list_partitions() { local devfile="$1" checkforexist "$devfile" checkforexistreadable "$devfile" #cfdisk -Pt "$devfile" 2>/dev/null | { # while read -r part v1 v2 v3 v4 v5 v6 v7 v8 startsec size rest; do # case "$part" in # [0-9]|[0-9][0-9]) # test "$size" -gt 0 && \ # echo -e "$part\t$startsec\t$size" # # cfdisk -Pt gives wrong/unhelpful numbers for logical partitions. LANG=C LC_ALL=C fdisk -l "$devfile" | { while read -r line; do line="${line#$devfile}" line="${line/\*/}" #echo "Line: '$line'" echo "$line" | ( read -r part startsec endsec rest case "$part" in [0-9]|[0-9][0-9]) size=$(($endsec - $startsec + 1)) test "$size" -gt 0 && \ echo -e "$part\t$startsec\t$endsec\t$size" : ;; esac ) done } } mount_partitions() { local devfile="$1" shift local partitions partitions="`list_partitions "$devfile"`" || exit #echo "Partitions: '$partitions'" test -n "$partitions" || exitwith errNoPartitions "$devfile" echo "$partitions" | { local dry="" ifset dryrun && dry=":" while read -r part start end size rest; do mp="$mountprefix-$part" ( set -x $dry mkdir -p "$mp" $dry mount -ooffset=$((512 * $start)) "$devfile" "$mp" "$@" ) done } } #### #### Main #### test $# -eq 0 && Usage 1 parse_cmd_line "$@" if [ -n "$listparts" ]; then list_partitions "${FILEARGS[@]}" else test -n "$mountprefix" || Help 1 mount_partitions "${FILEARGS[@]}" fi