commit | author | age
|
849fbd
|
1 |
#!/bin/bash |
G |
2 |
# This shell script used mount/umount an linux system image and update it |
|
3 |
|
|
4 |
mnt_boot=boot |
|
5 |
mnt_root=rootfs |
|
6 |
|
|
7 |
function mount_image() |
|
8 |
{ |
|
9 |
img_file=$1 |
|
10 |
loop_dev=`losetup -f | cut -d/ -f3` |
|
11 |
|
|
12 |
if [ ! -s $img_file ] ; then |
|
13 |
echo "ERROR: $img_file not found!" |
|
14 |
exit 1; |
|
15 |
fi |
|
16 |
|
|
17 |
if [ -z $loop_dev ] ; then |
|
18 |
echo "ERROR: loop dev not found!" |
|
19 |
exit 2; |
|
20 |
fi |
|
21 |
|
|
22 |
echo "INFO: losetup /dev/${loop_dev} ${img_file}" |
|
23 |
losetup /dev/${loop_dev} ${img_file} |
|
24 |
if [ $? != 0 ] ; then |
|
25 |
echo "ERROR: losetup /dev/${loop_dev} ${img_file} failed!" |
|
26 |
exit 3; |
|
27 |
fi |
|
28 |
|
|
29 |
echo "INFO: kpartx -av /dev/${loop_dev}" |
|
30 |
kpartx -av /dev/${loop_dev} |
|
31 |
|
|
32 |
echo "INFO: mount ${mnt_boot} ${mnt_root}" |
|
33 |
mkdir -p ${mnt_boot} ${mnt_root} |
|
34 |
mount /dev/mapper/${loop_dev}p1 ${mnt_boot} |
|
35 |
mount /dev/mapper/${loop_dev}p2 ${mnt_root} |
|
36 |
|
|
37 |
echo "INFO: mount $img_file done." |
|
38 |
} |
|
39 |
|
|
40 |
function umount_image() |
|
41 |
{ |
|
42 |
img_file=$1 |
|
43 |
|
|
44 |
mountpoint $mnt_boot > /dev/null 2>&1 |
|
45 |
if [ $? == 0 ] ; then |
|
46 |
echo "INFO: umount ${mnt_boot}" |
|
47 |
umount ${mnt_boot} |
|
48 |
rmdir ${mnt_boot} |
|
49 |
fi |
|
50 |
|
|
51 |
mountpoint $mnt_root > /dev/null 2>&1 |
|
52 |
if [ $? == 0 ] ; then |
|
53 |
echo "INFO: umount ${mnt_root}" |
|
54 |
umount ${mnt_root} |
|
55 |
rmdir ${mnt_root} |
|
56 |
fi |
|
57 |
|
|
58 |
# loop_dev should be 'loopX' such as 'loop9'. |
|
59 |
loop_dev=`losetup -a | grep $img_file| cut -d: -f1 | cut -d/ -f3` |
|
60 |
if [[ -z $loop_dev ]] ; then |
|
61 |
exit; |
|
62 |
fi |
|
63 |
|
|
64 |
if [[ -e /dev/mapper/${loop_dev}p1 ]] ; then |
|
65 |
echo "INFO: kpartx -dv /dev/${loop_dev}" |
|
66 |
kpartx -dv /dev/${loop_dev} |
|
67 |
fi |
|
68 |
|
|
69 |
echo "INFO: losetup -d /dev/${loop_dev}" |
|
70 |
losetup -d /dev/${loop_dev} |
|
71 |
|
|
72 |
echo "INFO: umount $img_file done." |
|
73 |
} |
|
74 |
|
|
75 |
|
|
76 |
function do_usage() |
|
77 |
{ |
|
78 |
echo "" |
|
79 |
echo "Usage:" |
|
80 |
echo " $0 [-m] [-u] image_file" |
|
81 |
echo " -m: mount the image file" |
|
82 |
echo " -u: umount the image file" |
|
83 |
echo "" |
|
84 |
exit; |
|
85 |
} |
|
86 |
|
|
87 |
action= |
|
88 |
img_file= |
|
89 |
|
|
90 |
while getopts "mu" OPTNAME |
|
91 |
do |
|
92 |
case "${OPTNAME}" in |
|
93 |
"m") |
|
94 |
action=mount |
|
95 |
;; |
|
96 |
|
|
97 |
"u") |
|
98 |
action=umount |
|
99 |
;; |
|
100 |
|
|
101 |
"*") |
|
102 |
do_usage |
|
103 |
;; |
|
104 |
esac |
|
105 |
done |
|
106 |
|
|
107 |
shift $(( $OPTIND-1 )) |
|
108 |
img_file=$1 |
|
109 |
|
|
110 |
if [[ -z $img_file ]] || [[ -z $action ]] ; then |
|
111 |
do_usage |
|
112 |
exit; |
|
113 |
fi |
|
114 |
|
|
115 |
if [ $action == "mount" ] ; then |
|
116 |
mount_image $img_file |
|
117 |
elif [ $action == "umount" ] ; then |
|
118 |
umount_image $img_file |
|
119 |
fi |
|
120 |
|