-
Notifications
You must be signed in to change notification settings - Fork 8
/
mount-img-parts
executable file
·99 lines (91 loc) · 3.14 KB
/
mount-img-parts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh
#
# Script to easily access/mount and manipulate with partitions of image file
#
# Copyright (C) 2014 Pavel Pisa <[email protected]>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
my_dir="$(pwd)"
lopart_dir="$my_dir/lopart"
imgmnt_dir="$my_dir/imgmnt"
img_file=$my_dir/rpi-wheezy-overlay
if [ $# -lt 1 ] ; then
echo "usage: mount-img-parts [mount|umount|losetup|lodetach] [img_file]"
exit 1
fi
action=$1
if [ $# -ge 2 ] ; then
img_file="$(readlink -f "$2")"
fi
case $action in
losetup)
for loop_dev in $(find $lopart_dir -name "pdev*" ) ; do
/sbin/losetup -d $loop_dev 2>/dev/null
done
for loop_dev in $(losetup -a | sed -n -e 's#^\([^ ]*\): [^(]*('"$img_file"').*$#\1#p') ; do
/sbin/losetup -d $loop_dev 2>/dev/null
done
rm -f "$lopart_dir/pdev"*
echo "setting loops for /home/pi/projects/rpi/archive/lopart/loop0"
loop_dev="$(losetup -f --show $img_file)"
if [ $? -ne 0 ] ; then
echo >2 "failed: losetup -f --show $img_file"
exit 1
fi
cp -a "$loop_dev" "$lopart_dir/pdev0"
;;
lodetach)
for mntpt_dir in $(sed -n -e 's#/dev/loop[0-9]* \('"$imgmnt_dir/[^ ]*"'\) .*$#\1#p' /proc/mounts) ; do
echo "unmounting $mntpt_dir"
umount "$mntpt_dir"
rmdir "$mntpt_dir"
done
for loop_dev in $(find $lopart_dir -name "pdev*" ) ; do
/sbin/losetup -d $loop_dev 2>/dev/null
done
for loop_dev in $(losetup -a | sed -n -e 's#^\([^ ]*\): [^(]*('"$img_file"').*$#\1#p') ; do
echo "losetup detach $loop_dev"
/sbin/losetup -d $loop_dev
done
rm -f "$lopart_dir/pdev"*
exit 0
;;
esac
/sbin/parted $img_file -ms unit B p | tr ':B' ' ' | (
while read pnum pstart pend psize ptype ; do
case $pnum in
[1-4])
echo "$action pnum=$pnum pstart=$pstart pend=$pend psize=$psize ptype=$ptype"
case $action in
mount)
mkdir -p "$imgmnt_dir/p$pnum" || exit 1
mount -o loop,offset=$pstart,sizelimit=$psize "$img_file" "$imgmnt_dir/p$pnum" || exit 1
;;
umount)
umount "$imgmnt_dir/p$pnum"
rmdir "$imgmnt_dir/p$pnum"
;;
losetup)
mkdir -p "$lopart_dir" || exit 1
loop_dev="$(losetup --offset $pstart --sizelimit $psize -f --show $img_file)"
if [ $? -ne 0 ] ; then
echo >2 "failed: losetup --offset $pstart --sizelimit $psize -f --show $img_file"
exit 1
fi
rm -f "$lopart_dir/pdev0p$pnum"
cp -a "$loop_dev" "$lopart_dir/pdev0p$pnum"
esac
;;
esac
done
)