1 |
#!/bin/bash |
2 |
############################################################################## |
3 |
# # |
4 |
# imgconvert, (C) 2007-2010 S. Georgaras <sng@hellug.gr> # |
5 |
# # |
6 |
# This file is part of Clonezilla-SysRescCD. # |
7 |
# # |
8 |
# Clonezilla-SysRescCD is free software: you can redistribute it and/or # |
9 |
# modify it under the terms of the GNU General Public License as published # |
10 |
# by the Free Software Foundation, either version 2 of the License, or # |
11 |
# (at your option) any later version. # |
12 |
# # |
13 |
# Clonezilla-SysRescCD is distributed in the hope that it will be useful, # |
14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
16 |
# GNU General Public License for more details. # |
17 |
# # |
18 |
# You should have received a copy of the GNU General Public License along # |
19 |
# with Clonezilla-SysRescCD. If not, see <http://www.gnu.org/licenses/>. # |
20 |
# # |
21 |
############################################################################## |
22 |
# DEBUG=true |
23 |
versionNumber='0.2' |
24 |
|
25 |
function checkPartitionName(){ |
26 |
# check partition |
27 |
if [ -z "$(echo "$1" | grep -e '[hs]d[a-z][0-9]')" ];then |
28 |
# printVersion |
29 |
echo error |
30 |
echo "Error: Partition name \"$1\" is not valid!!!" |
31 |
exit 1 |
32 |
fi |
33 |
} |
34 |
|
35 |
|
36 |
function validateImage(){ |
37 |
# set -x |
38 |
|
39 |
[ -d "$1"/parts ] && return 1 |
40 |
local sf=$(find "$1" -name "*-chs.sf" 2>/dev/null | wc -l) |
41 |
[ $sf -eq 0 ] && return 1 |
42 |
local pt=$(find "$1" -name "*-pt.sf" 2>/dev/null | wc -l) |
43 |
[ $pt -eq 0 ] && return 1 |
44 |
# modification to read unsplited images |
45 |
# thanks to Viking (24/11/2008) |
46 |
# local aa=$(find "$1" -name "*.aa" 2>/dev/null | wc -l) |
47 |
# [ $aa -eq 0 ] && return 1 |
48 |
|
49 |
|
50 |
######################################### |
51 |
# accept only disk images # |
52 |
######################################### |
53 |
# [ ! -e "$1"/disk ] && return 2 |
54 |
######################################### |
55 |
return 0 |
56 |
} |
57 |
|
58 |
function testRoot(){ |
59 |
if [ "$(whoami)" != "root" ];then |
60 |
printVersion |
61 |
echo "Error: This script must be executed by root..." |
62 |
exit |
63 |
fi |
64 |
} |
65 |
|
66 |
|
67 |
function printVersion(){ |
68 |
echo "Clonezilla Live Image Conversion |
69 |
$(basename $0) v. $versionNumber - (C) 2009-2011 S. Georgaras <sng@hellug.gr> |
70 |
" |
71 |
} |
72 |
|
73 |
function printHelp(){ |
74 |
printVersion |
75 |
# echo "Usage: $(basename $0) <options> [image] [partition] <new partition> |
76 |
# |
77 |
# Parameters are: |
78 |
# [image] Disk image to be converted to partition image |
79 |
# [partition] Partition name to convert. It must be a valid device name |
80 |
# |
81 |
# Available options: |
82 |
# o [image] Save new imag as [image] |
83 |
# p Save new partition instead of making a link to the old one |
84 |
# d Draft execution. Execute all checks but don't create the image |
85 |
# v Print version info and exit |
86 |
# h Print this screen and exit" |
87 |
echo "Usage: $(basename $0) <options> [image] [partition] <new partition> |
88 |
|
89 |
Parameters are: |
90 |
[image] Disk image to be converted to partition image |
91 |
[partition] Partition name to convert. It must be a valid device name |
92 |
|
93 |
Available options: |
94 |
o [image] Save new imag as [image] |
95 |
p Save new partition instead of making a link to the old one |
96 |
v Print version info and exit |
97 |
h Print this screen and exit" |
98 |
} |
99 |
|
100 |
|
101 |
|
102 |
|
103 |
# |
104 |
# |
105 |
# Script starts here |
106 |
# |
107 |
# |
108 |
# set -x |
109 |
while getopts hvpdo: opt |
110 |
do |
111 |
case "$opt" in |
112 |
p) # output partition name |
113 |
testRoot |
114 |
doCopy=1 |
115 |
;; |
116 |
# d) # do draft (execute all checks but don't create the image) |
117 |
# testRoot |
118 |
# doDraft=1 |
119 |
# ;; |
120 |
o) # output image name |
121 |
testRoot |
122 |
imgOutName="$(echo "$OPTARG" | sed 's|^\./||')" |
123 |
;; |
124 |
v) |
125 |
printVersion |
126 |
exit;; |
127 |
h) |
128 |
printHelp |
129 |
exit;; |
130 |
esac |
131 |
done |
132 |
shift $(( OPTIND - 1 )) |
133 |
|
134 |
if [ -z "$1" ] || [ -z "$2" ];then |
135 |
printHelp |
136 |
exit 1 |
137 |
fi |
138 |
testRoot |
139 |
printVersion |
140 |
|
141 |
# check input image |
142 |
echo 'Determining input image' |
143 |
isRelative=1 |
144 |
imgInName="$(echo "$1" | sed 's|^\./||')" |
145 |
imgInName="$(echo "$imgInName" | sed 's|/$||')" |
146 |
if [ "$(expr substr "$imgInName" 1 1)" = "/" ];then |
147 |
isRelative='' |
148 |
fi |
149 |
|
150 |
hasError=1 |
151 |
if test $isRelative; then |
152 |
# relative path? |
153 |
if [ -d ./"$imgInName" ];then |
154 |
# yes it is |
155 |
relativePath=1 |
156 |
hasError='' |
157 |
imgInName="$(pwd)"/"$imgInName" |
158 |
else |
159 |
# no, is it in /home/partimag? |
160 |
if [ -d /home/partimag/"$imgInName" ];then |
161 |
imgInName=/home/partimag/"$imgInName" |
162 |
hasError='' |
163 |
fi |
164 |
fi |
165 |
elif [ -d "$imgInName" ];then |
166 |
hasError='' |
167 |
fi |
168 |
if test $hasError; then |
169 |
echo "Error: Image \"$imgInName\" does not exist!!!" |
170 |
exit 1 |
171 |
fi |
172 |
echo " Input image: \"$imgInName\"" |
173 |
echo -n ' Validating image... ' |
174 |
validateImage "$imgInName" |
175 |
ans=$? |
176 |
if [ "$ans" = "1" ];then |
177 |
# printVersion |
178 |
echo error |
179 |
echo "Error: Image \"$imgInName\" is not a valid image!!!" |
180 |
exit 1 |
181 |
elif [ "$ans" = "2" ];then |
182 |
# printVersion |
183 |
echo error |
184 |
echo "Error: Image \"$imgInName\" is not a disk image!!!" |
185 |
exit 1 |
186 |
fi |
187 |
echo ok |
188 |
|
189 |
partInName="$2" |
190 |
# check input partition name |
191 |
echo 'Determining input partition' |
192 |
echo " Input partition: \"$partInName\"" |
193 |
echo -n ' Validating input partition... ' |
194 |
checkPartitionName "$partInName" |
195 |
if [ -z "$(grep "$partInName" "$imgInName"/parts)" ];then |
196 |
# printVersion |
197 |
echo 'error' |
198 |
echo "Error: Partition \"$partInName\" not found in image!!!" |
199 |
exit 1 |
200 |
fi |
201 |
echo ok |
202 |
|
203 |
|
204 |
echo 'Determining output image' |
205 |
isRelative=1 |
206 |
|
207 |
if [ -z "$imgOutName" ];then |
208 |
imgOutName="$imgInName"-cnv |
209 |
else |
210 |
imgOutName="$(echo "$imgOutName" | sed 's|^\./||')" |
211 |
imgOutName="$(echo "$imgOutName" | sed 's|/$||')" |
212 |
fi |
213 |
|
214 |
if [ "$(expr substr "$imgOutName" 1 1)" = "/" ];then |
215 |
isRelative='' |
216 |
fi |
217 |
# set -x |
218 |
hasError='' |
219 |
if test $isRelative; then |
220 |
if [ -e "$(pwd)/$imgOutName" ];then |
221 |
if [ -e /home/partimag/"$imgOutName" ];then |
222 |
hasError=1 |
223 |
else |
224 |
hasError='' |
225 |
imgOutName=/home/partimag/"$imgOutName" |
226 |
fi |
227 |
else |
228 |
imgOutName="$(pwd)/$imgOutName" |
229 |
hasError='' |
230 |
fi |
231 |
else |
232 |
if [ -e "$imgOutName" ];then |
233 |
hasError=1 |
234 |
else |
235 |
hasError='' |
236 |
fi |
237 |
fi |
238 |
# set -x |
239 |
echo " Output image: \"$imgOutName\"" |
240 |
echo -n ' Validating output image... ' |
241 |
if test $hasError; then |
242 |
echo error |
243 |
echo "Error: Output image already exists!!!" |
244 |
exit 1 |
245 |
else |
246 |
mkdir "$imgOutName" 2>/dev/null |
247 |
if [ -d "$imgOutName" ];then |
248 |
echo ok |
249 |
echo -n ' Checking permissions... ' |
250 |
touch "$imgOutName"/test.$$ 2>/dev/null || { |
251 |
echo error |
252 |
echo "Error: Output image is not writable!!!" |
253 |
rm -rf "$imgOutName" 2>/dev/null |
254 |
exit 1 |
255 |
} |
256 |
if ! test $doCopy; then |
257 |
ln -s "$imgOutName"/test.$$ "$imgOutName"/test.$$.$$ 2>/dev/null || { |
258 |
echo error |
259 |
echo "Error: File system does not supports links!!!" |
260 |
rm -rf "$imgOutName" 2>/dev/null |
261 |
exit 1 |
262 |
} |
263 |
fi |
264 |
echo ok |
265 |
rm -rf "$imgOutName"/* 2>/dev/null |
266 |
else |
267 |
hasError=1 |
268 |
echo error |
269 |
echo "Error: Image folder cannot be created!!!" |
270 |
fi |
271 |
fi |
272 |
# set +x |
273 |
|
274 |
|
275 |
|
276 |
|
277 |
echo 'Determining output partition' |
278 |
partOutName="$3" |
279 |
# check output partition name |
280 |
|
281 |
if [ -z "$partOutName" ];then |
282 |
partOutName="$partInName" |
283 |
echo " Output partition: \"$partOutName\"" |
284 |
echo ' Validating output partition... ok' |
285 |
else |
286 |
echo " Output partition: \"$partOutName\"" |
287 |
echo -n ' Validating output partition... ' |
288 |
checkPartitionName "$partOutName" |
289 |
if [ ! -z "$(echo "$partOutName" | grep -e '[0-9][0-9][0-9]$')" ];then |
290 |
echo error |
291 |
echo "Error: Partition name \"$partOutName\" is not valid!!!" |
292 |
rm -rf "$imgOutName" 2>/dev/null |
293 |
exit 1 |
294 |
fi |
295 |
echo ok |
296 |
fi |
297 |
|
298 |
hasError='' |
299 |
echo "Creating output image: $imgOutName" |
300 |
cd "$imgOutName" |
301 |
echo "$partOutName" > parts 2>/dev/null || hasError=1 |
302 |
|
303 |
for n in "$imgInName"/"$partInName"*;do |
304 |
if test $doCopy;then |
305 |
# copy files instead of linking |
306 |
echo -n " Copying files... " |
307 |
cp "$n" "$(echo "$n" | sed "s|"$imgInName"/||" | sed "s|"$partInName"|"$partOutName"|")" 2>/dev/null || { |
308 |
hasError=1 |
309 |
echo error |
310 |
} && echo 'done' |
311 |
else |
312 |
# link files |
313 |
echo -n " Linking files... " |
314 |
ln -s "$n" "$(echo "$n" | sed "s|"$imgInName"/||" | sed "s|"$partInName"|"$partOutName"|")" 2>/dev/null || { |
315 |
cd .. |
316 |
rm -rf "$imgOutName" |
317 |
echo "Error: Could not create output image" |
318 |
exit 1 |
319 |
} && echo 'done' |
320 |
fi |
321 |
done |
322 |
if test $hasError ;then |
323 |
echo "Error: Could not create output image" |
324 |
exit 1 |
325 |
fi |
326 |
echo -n " Fixing info files... " |
327 |
disk="$(echo "$partInName" | sed 's|[0-9]*$||')" |
328 |
newDisk="$(echo "$partOutName" | sed 's|[0-9]*$||')" |
329 |
cp "$imgInName"/"$disk"-chs.sf "$newDisk"-chs.sf 2>/dev/null || { |
330 |
cd .. |
331 |
rm -rf "$imgOutName" |
332 |
echo "Error: Could not create output image" |
333 |
exit 1 |
334 |
} && { |
335 |
ptFile="$(echo "$(basename "$imgInName"/"$disk"-pt.sf)" | sed "s|"$disk"|"$newDisk"|")" |
336 |
|
337 |
|
338 |
echo "# partition table of /dev/"$newDisk" |
339 |
unit: sectors |
340 |
" > "$ptFile" || { |
341 |
cd .. |
342 |
rm -rf "$imgOutName" |
343 |
echo "Error: Could not create output image" |
344 |
exit 1 |
345 |
} |
346 |
|
347 |
|
348 |
|
349 |
grep "$partInName" "$imgInName"/"$disk"-pt.sf | sed "s|"$partInName"|"$partOutName"|" >> "$ptFile" || { |
350 |
cd .. |
351 |
rm -rf "$imgOutName" |
352 |
echo "Error: Could not create output image" |
353 |
exit 1 |
354 |
} |
355 |
echo 'done |
356 |
' |
357 |
} |