SyntaxHighlighter

Donnerstag, 13. Dezember 2012

Mounten verschiedener Partitionen aus einem Image

Test von prettyprint... und nebenbei ein vielleicht nützliches Script zum Mounten verschiedener Partitionen aus ein und dem selben Abbild(image).

Code (auch auf  PasteBin: mm_loop.sh):
#!/bin/bash
# multi partition from one image-file mounter
# 2012 Armas Spann

I_FILE=$1
I_NAME=$(echo $I_FILE | sed -e 's/\./\n/g' | head -1)
B_SIZE=$(/sbin/fdisk -l $I_FILE | grep physical | sed -e 's/\/ /\n/g' | tail -n 1 | sed 's/ bytes//g')

TMP_FILE=$(mktemp)
P_CNT=$#
P_MNT=$2

#creating tempfile:
$(/sbin/fdisk -l $I_FILE | grep $I_NAME | grep -v "$I_FILE:" | sed -e 's/\*/\ /g' > $TMP_FILE)

#checking root privileges
if [[ ${EUID} == 0 ]] ; then
  ROOT=1
else
  ROOT=0
fi

function chkinput {
  if [ $P_CNT -ne 2 ]; then
    howuse
  else
    if [ ! -f $I_FILE ] && [ ! -d $P_MNT ]; then
      echo -e "File or mountpoint not found!"
      quit
    else
      selectpartition
    fi
  fi
}

function howuse {
  echo -e "Usage:"
  echo -e "$0  "
  quit
}

function quit {
  rm $TMP_FILE
  unset ROOT
  unset I_FILE
  unset I_NAME
  unset OFFSET
  unset LIMIT
  unset MNT_CMD
  unset what
  unset FS
  unset B_SIZE
  i=0; while [ $i -lt $idx ]; do
    unset P_TABLE[$i]
    i=$(($i+1))
  done
  unset i
  unset idx
  unset P_CNT
  unset P_MNT
  exit
}


#fetching partitions...
idx=0; while read partition; do idx=$(($idx+1));
  if [ "$(echo $partition | awk '{print $5}')" != "82" ]; then
    P_TABLE+=( [$idx-1]="$partition" )
  else
    idx=$(($idx-1))
  fi
done < $TMP_FILE


function selectpartition {
  #output...
  clear
  echo blocksize is: $B_SIZE bytes
  echo found $idx mountable Partitions:
  i=0; while [ $i -lt $idx ]; do
    echo $(($i+1))": ${P_TABLE[$i]}"
    i=$(($i+1))
  done

  #if not root
  if [ "$ROOT" != "1" ]; then
    echo -e "\nSorry u have to be \"root\" to do the mounting stuff..."
    echo -e "ERROR: NO ROOT-ACCESS!"
    quit
  fi

  # select partition
  while :
  do
  read -p "Please choose one of them to mount by number(or 'q' to quit): " -n1 what
  echo -e ""
  case $what in          
    "q") echo -e "Bye... ;-)"; quit
      ;;
    *) if [ $what -gt "0" ] && [ $(($what-1)) -lt $idx ]; then
        TMP_IDX=$(($what-1))
        echo -e "\nYour choice: \n${P_TABLE[TMP_IDX]}"
        read -p "Is this correct? (y/n): " -n1 what
        echo -e "\n"
        if [ $what == "y" ]; then
          OFFSET=$(($(echo ${P_TABLE[TMP_IDX]}|awk '{print $2}')*$B_SIZE))
          LIMIT=$(($(echo ${P_TABLE[TMP_IDX]}|awk '{print $4}')*$B_SIZE))
          read -p "Please enter filesystem (mount option -t): " FS
          MNT_CMD="mount -o loop,ro,offset=$OFFSET,sizelimit=$LIMIT $I_FILE -t $FS $P_MNT"
          mountit
        else
          selectpartition
        fi
      else
        echo -e "Wrong Choice, try again."
        selectpartition
      fi
      ;;
    esac
  done
}

function mountit {
  #should i mount it for you?
  while :
  do
    echo -e "Should i mount it for you? \nCommand: $MNT_CMD"
    read -p "(y/n):" -n1 what
    echo -e ""
    if [ $what == "y" ]; then
      $($MNT_CMD)
      echo -e "Done..."
      quit
    else
      selectpartition
    fi
  done
}

chkinput

Keine Kommentare:

Kommentar veröffentlichen