Created
June 7, 2017 13:15
-
-
Save gunnarx/281c3016ecac9e301c08f9bf8de9c58e to your computer and use it in GitHub Desktop.
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
# mntpart - mount & view a partition from a disk image file | |
# (C) 2015 Gunnar Andersson | |
# License: MPLv2 | |
mntpart () | |
{ | |
function _mntpartusage () | |
{ | |
echo "Usage: mntpart <file> <partition-number> [mountpoint]"; | |
echo "(Default mountpoint is /mnt)" | |
}; | |
file=$1; | |
n=$2; | |
mountpoint=$3; | |
[ -z "$n" ] && { | |
echo "mntpart: No partition number specified"; | |
_mntpartusage; | |
return 1 | |
}; | |
[ -z "$file" ] && { | |
echo "mntpart: No image file specified"; | |
_mntpartusage; | |
return 1 | |
}; | |
[ -z "$mountpoint" ] && mountpoint=/mnt; | |
[ -e "$mountpoint" -a ! -d "$mountpoint" ] && { | |
echo "Mountpoint exists but is not a directory! Giving up. "; | |
return 1 | |
}; | |
[ -d "$mountpoint" ] || { | |
echo "Mountpoint does not exist, creating."; | |
mkdir -p $mountpoint || return 2 | |
}; | |
offset=$(fdisk -l "$file" | skip_at_beginning 9 | sed 's/*//' | pick $n | awk '{print $2}'); | |
byte=$(($offset*512)); | |
echo "Partition $n starts at sector $offset = byte $byte"; | |
cmd="sudo mount -o offset=$byte $file $mountpoint"; | |
echo "$cmd"; | |
$cmd; | |
echo "Listing contents of $mountpoint : "; | |
\ls -F --color -al $mountpoint | |
} |
pick ()
{
awk "NR==$1"
}
For convenience, you might also consider setting up passwordless sudo access to mount command, assuming you're OK with the security aspect. If you're not OK with that, create a safe wrapper for mount that forces mountpoint to /mnt, for example.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh sorry, this was missing. Maybe something more, IDK
skip_at_beginning ()
{
tail -n +$1
}