forked from mad-ady/OdroidC1-tripleboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_linux
executable file
·231 lines (206 loc) · 6.48 KB
/
extract_linux
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "Script must be run as root !"
exit 0
fi
. params.sh
. func.sh
echo ""
date
echo "***************************************************"
echo "* Extracting structure of odroid linux SD Card *"
echo "* and copying partitions data to directories *"
echo "***************************************************"
echo ""
check_sdcard "nocreate"
if [ $? -ne 0 ]; then
exit $?
fi
umount ${sdcard}* > /dev/null 2>&1
sleep 1
mkdir -p ${bkpdir} > /dev/null 2>&1
#=======================================================================
# Calculate partitions offsets and sizes
echo ""
echo "Analyzing sd card ..."
sdcard_part=`fdisk -l $sdcard | grep Linux | awk '{print $1}'`
sdcard_sect=`fdisk -l $sdcard | grep "Disk $sdcard" | awk '{print $7}'`
if [ "${sdcard_sect}" = "" ]; then
sdcard_sect=`fdisk -l $sdcard | grep total | awk '{print $8}'`
fi
sdcard_end=$(expr $sdcard_sect - 1024)
fat=`fdisk -l $sdcard | grep FAT`
if [ "${fat}" = "" ]; then
linuxpart="1"
else
linuxpart="2"
fi
if [ "${linuxpart}" = "2" ]; then
fat_start=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $2}'`
if [ "${fat_start}" = "*" ]; then
fat_start=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $3}'`
fat_end=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $4}'`
else
fat_end=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $3}'`
fi
fat_size=$(( ($fat_end - $fat_start + 1) / 2048 ))
linux_start=`fdisk -l $sdcard | grep ${sdcard}2 | awk '{print $2}'`
linux_end=`fdisk -l $sdcard | grep ${sdcard}2 | awk '{print $3}'`
linux_size=$(( ($linux_end - $linux_start + 1) / 2048 ))
else
linux_start=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $2}'`
if [ "${linux_start}" = "*" ]; then
linux_start=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $3}'`
linux_end=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $4}'`
else
linux_end=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $3}'`
fi
linux_size=$(( ($linux_end - $linux_start + 1) / 2048 ))
fi
echo ""
echo " SDCard size: $sdcard_sect blocks, $(expr $sdcard_sect / 2048 ) M"
echo "------------------------------------------------"
if [ "${linuxpart}" = "2" ]; then
printf "%14s" "FAT part:"; printf "%12s" $fat_start; printf "%12s" $fat_end; printf "%10s\n" "$fat_size M"
fi
printf "%14s" "linux part:"; printf "%12s" $linux_start; printf "%12s" $linux_end; printf "%10s\n" "$linux_size M"
echo "------------------------------------------------"
echo ""
if [ "${linuxpart}" = "2" ]; then
if [ "${fat_start}" = "" ] || [ "${linux_start}" = "" ]; then
echo "Bad SDCard partition structure !"
exit 1
fi
if [ ! "${fat_start}" = "3072" ] && [ ! "${fat_start}" = "2048" ]; then
echo "Bad SDCard partition structure (start) !"
exit 1
fi
if [ "${fat_start}" = "3072" ]; then
_disk_start="3072"
else
_disk_start="2048"
fi
else
if [ "${linux_start}" = "" ]; then
echo "Bad SDCard partition structure !"
exit 1
fi
if [ ! "${linux_start}" = "3072" ] && [ ! "${linux_start}" = "2048" ]; then
echo "Bad SDCard partition structure (start)!"
exit 1
fi
if [ "${linux_start}" = "3072" ]; then
_disk_start="3072"
else
_disk_start="2048"
fi
echo "SDCard is ext4 only card!"
fi
mkdir _mnt > /dev/null 2>&1
umount _mnt > /dev/null 2>&1
mkdir _mnt1 > /dev/null 2>&1
umount _mnt1 > /dev/null 2>&1
echo ""
echo -n "WARNING: Linux files in $bkpdir/linux WILL BE UPDATED !, Continue (y/N)? "
read -n 1 ANSWER
if [ ! "${ANSWER}" = "y" ] ; then
echo "."
echo "Canceled.."
exit 0
fi
echo ""
# EXTRACTING FILES
echo ""
if [ "${_isimage}" = "yes" ]; then
kpartx -l ${sdcard} > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR testing kpartx"
exit 1
fi
loop_dev=`kpartx -l $sdcard | grep ${_disk_start} | sed s/"\// "/g | awk '{print $6}'`
kpartx -a -s ${sdcard} > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR mounting with kpartx"
exit 1
fi
sleep 1
if [ "${linuxpart}" = "2" ]; then
mkdir ${bkpdir}/lin_fat > /dev/null 2>&1
loop_mapp="/dev/mapper/${loop_dev}p1"
mount ${loop_mapp} _mnt > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR mounting ${loop_mapp}"
kpartx -d -s ${sdcard} > /dev/null 2>&1
dmsetup remove /dev/mapper/${loop_dev}p* > /dev/null 2>&1
losetup -d /dev/${loop_dev} > /dev/null 2>&1
exit 1
fi
echo "Copying FAT partition ..."
rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats _mnt/ ${bkpdir}/lin_fat/ > /dev/null 2>&1
sync
fs_size=`du -s -h ${bkpdir}/lin_fat | awk '{print $1}'`
echo "File system size: $fs_size"
umount _mnt
fi
loop_mapp="/dev/mapper/${loop_dev}p${linuxpart}"
mkdir ${bkpdir}/linux > /dev/null 2>&1
mount ${loop_mapp} _mnt > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR mounting ${loop_mapp}"
kpartx -d -s ${sdcard} > /dev/null 2>&1
dmsetup remove /dev/mapper/${loop_dev}p* > /dev/null 2>&1
losetup -d /dev/${loop_dev} > /dev/null 2>&1
exit 1
fi
echo ""
echo "Copying linux partition ..."
rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats _mnt/ ${bkpdir}/linux/ > /dev/null 2>&1
sync
fs_size=`du -s -h ${bkpdir}/linux | awk '{print $1}'`
echo "File system size: $fs_size"
sync
sleep 2
umount _mnt > /dev/null 2>&1
sleep 2
kpartx -d -s ${sdcard} > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "WARNING: ERROR unmounting with kpartx"
fi
dmsetup remove /dev/mapper/${loop_dev}p* > /dev/null 2>&1
losetup -d /dev/${loop_dev} > /dev/null 2>&1
else
if [ "${linuxpart}" = "2" ]; then
mkdir ${bkpdir}/lin_fat > /dev/null 2>&1
mount ${sdcard}1 _mnt > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR mounting ${sdcard}2"
exit 1
fi
echo "Copying FAT partition ..."
rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats _mnt/ ${bkpdir}/lin_fat/ > /dev/null 2>&1
sync
fs_size=`du -s -h ${bkpdir}/lin_fat | awk '{print $1}'`
echo "File system size: $fs_size"
umount _mnt
fi
mkdir ${bkpdir}/linux > /dev/null 2>&1
mount ${sdcard}${linuxpart} _mnt > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR mounting ${sdcard}${linuxpart}"
exit 1
fi
echo ""
echo "Copying linux partition ..."
rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats _mnt/ ${bkpdir}/linux/ > /dev/null 2>&1
sync
fs_size=`du -s -h ${bkpdir}/linux | awk '{print $1}'`
echo "File system size: $fs_size"
umount _mnt
fi
sync
echo ""
echo "==================================="
echo "Linux SDCard filesystems extracted."
echo "==================================="
echo ""
# -------------------------------------------------------------------