Skip to content

Instantly share code, notes, and snippets.

@gunnarx
Created June 7, 2017 13:15
Show Gist options
  • Save gunnarx/281c3016ecac9e301c08f9bf8de9c58e to your computer and use it in GitHub Desktop.
Save gunnarx/281c3016ecac9e301c08f9bf8de9c58e to your computer and use it in GitHub Desktop.
# 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
}
@gunnarx
Copy link
Author

gunnarx commented Jun 7, 2017

Oh sorry, this was missing. Maybe something more, IDK
skip_at_beginning ()
{
tail -n +$1
}

@gunnarx
Copy link
Author

gunnarx commented Jun 7, 2017

pick ()
{
awk "NR==$1"
}

@gunnarx
Copy link
Author

gunnarx commented Jun 7, 2017

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