Menu

[r8889]: / experimental / nanobsd / image_tool.sh  Maximize  Restore  History

Download this file

375 lines (325 with data), 8.8 kB

  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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/bin/sh
#
# Image manipulation tool for BSDRP
#
# Copyright (c) 2009, The BSDRP Development Team
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# Uncomment for enable debug:
#set -x
DEST_ROOT=/tmp/freenas_root
DEST_CFG=/tmp/freenas_cfg
DEST_DATA=/tmp/freenas_data
BSDRP_SRC=/usr/src/tools/tools/nanobsd/FreeNAS
# Get options passed by user
getoption () {
OPTION="$1"
FILENAME="$2"
case "$OPTION" in
mount)
mount_img
;;
umount)
umount_img
;;
update)
update_img
;;
qemu)
convert_2qemu
;;
#convert)
# convert
# ;;
help|h)
usage
;;
*)
if [ "${OPTION}" = "" ];
then
echo "missing option"
else
echo "illegal option: $OPTION"
fi
usage
;;
esac
}
# Display help
usage () {
echo "BSD Router Project image manipulation tool"
# value $0 is the name of the called script
echo "Usage: $0 option"
echo " - mount <filename> : Mount BSDRP image"
echo " - umount : umount BSDRP image"
echo " - update : Copy some of the BSDRP source Files/ to mounted root"
echo " - qemu <filename> : Convert BSDRP image to compressed qcow2 (qemu) format (not mandatory for using it with qemu)"
# echo " - convert : Convert ad0 to da0 and vice e versa"
echo " - help (h) [option] : Display this help message"
exit 0
}
# Check if image is mounted
check_is_mount () {
# Verifing that destination mount points are mounted
(mount | grep "${DEST_ROOT}" > /dev/null 2>&1 )
if [ ! $? -eq 0 ]
then
echo "It seem that ${DEST_ROOT} is not mounted"
echo '"Use "image_tools mount" before'
exit 1
fi
(mount | grep "${DEST_CFG}" > /dev/null 2>&1 )
if [ ! $? -eq 0 ]
then
echo "It seem that ${DEST_CFG} is not mounted"
echo '"Use "image_tools mount" before'
exit 1
fi
(mount | grep "${DEST_DATA}" > /dev/null 2>&1 )
if [ ! $? -eq 0 ]
then
echo "It seem that ${DEST_DATA} is not mounted"
fi
}
# Convert function
convert_drive () {
echo "mount -o ro /dev/${NEW_DRIVE}s3" > ${DEST_ROOT}/conf/default/etc/remount
echo "NANO_DRIVE=${NEW_DRIVE}" > ${DEST_ROOT}/etc/nanobsd.conf
echo "/dev/${NEW_DRIVE}s1a / ufs ro 1 1" > ${DEST_ROOT}/etc/fstab
echo "/dev/${NEW_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> ${DEST_ROOT}/etc/fstab
echo "Done!"
echo "Don't forget to change the filename from ${NANO_DRIVE} to ${NEW_DRIVE} once image will be umounted"
exit 0
}
# Convert Disk type (ad0 to da0, da0 to ad0)
convert () {
check_is_mount
# Get original disk type (set the variable NANO_DRIVE)
. ${DEST_ROOT}/etc/nanobsd.conf
case "$NANO_DRIVE" in
ad0)
echo "ad0 detected, convert to da0:"
NEW_DRIVE=da0
convert_drive
exit 0
;;
da0)
echo "da0 detected, convert to ad0:"
NEW_DRIVE=ad0
convert_drive
exit 0
;;
*)
echo "Unknown value: ${NANO_DRIVE}"
exit 1
esac
}
# update image
update_img () {
check_is_mount
# Verifing the presence of BSDRP Files folder:
if [ ! -d ${BSDRP_SRC}/Files ]
then
echo "Don't found ${BSDRP_SRC}/Files !"
exit 1
fi
# Copying all Files (and create dir)
# execpt:
# .svn and /usr/local/etc (because need special permission for quagga)
# boot/ (because line are added to this file in serial or dual mode)
(
cd ${BSDRP_SRC}/Files
find . -print | grep -Ev '/(CVS|\.svn|etc|boot)' | cpio -dumpv ${DEST_ROOT}/
)
(
cd ${BSDRP_SRC}/Files/etc
find . -print | grep -Ev '/(CVS|\.svn)' | cpio -dumpv ${DEST_ROOT}/etc/
)
}
# Check validity of image file
check_img () {
if [ "${FILENAME}" = "" ];
then
echo "Missing filename"
usage
fi
# Checking if file exist
if [ ! -f ${FILENAME} ]
then
echo "Can't found ${FILENAME}"
exit 1
fi
# Checking file type
(file -b ${FILENAME} | grep "boot sector" > /dev/null 2>&1 )
if [ ! $? -eq 0 ]
then
echo "Not a BSDRP image file detected"
echo "If your BSDRP image is zipped, unzip it before to use
with this tools"
exit 1
fi
}
# Convert to qcow2 format
convert_2qemu () {
check_img
if [ ! -f /usr/local/bin/qemu-img ]
then
echo "Don't found qemu-img: Is qemu installed ?"
exit 1
fi
# Checking if file exist
if [ ! -f ${FILENAME} ]
then
echo "Can't found ${FILENAME}"
exit 1
fi
# Checking if destination file exist
if [ -f ${FILENAME}.qcow2 ]
then
echo "Destination file allready exist."
echo "Move existing file to .old"
mv ${FILENAME}.qcow2 ${FILENAME}.qcow2.old
fi
echo "Converting image…"
qemu-img convert -c -f raw -O qcow2 ${FILENAME} ${FILENAME}.qcow2
if [ ! $? -eq 0 ]
then
echo "Meet a problem during qemu-img convert"
exit 1
fi
echo "Img convert successfully"
echo "New image: ${FILENAME}.qcow2"
exit 0
}
# Check if BSDRP image is allready mounted
check_notmounted () {
# Verifing that destination mount point are free
(mount | grep "${DEST_ROOT}" > /dev/null 2>&1 )
if [ $? -eq 0 ]
then
echo "It seem that ${DEST_ROOT} is allready mounted"
echo '"Use "image_tools umount" before to use mount'
exit 1
fi
(mount | grep "${DEST_CFG}" > /dev/null 2>&1 )
if [ $? -eq 0 ]
then
echo "It seem that ${DEST_CFG} is allready mounted"
echo '"Use "image_tools umount" before to use mount'
exit 1
fi
(mount | grep "${DEST_DATA}" > /dev/null 2>&1 )
if [ $? -eq 0 ]
then
echo "It seem that ${DEST_DATA} is allready mounted"
echo '"Use "image_tools umount" before to use mount'
exit 1
fi
}
# Mount image
mount_img () {
check_notmounted
check_img
# Create the destination folders
if [ ! -d ${DEST_ROOT} ]
then
mkdir ${DEST_ROOT}
fi
if [ ! -d ${DEST_CFG} ]
then
mkdir ${DEST_CFG}
fi
if [ ! -d ${DEST_DATA} ]
then
mkdir ${DEST_DATA}
fi
# Create RAM disk
MD=`mdconfig -a -t vnode -f ${FILENAME} -x 63 -y 16`
if [ ! $? -eq 0 ]
then
echo "Meet a problem for creating memory disk."
exit 1
fi
# Save MD name (for umount)
echo "MD=$MD" > /tmp/bsdrp_image_tools.tmp
mount /dev/${MD}s1a ${DEST_ROOT}
if [ ! $? -eq 0 ]
then
echo "Meet a problem for mounting root partition of the image."
echo "Destroying Ram drive..."
mdconfig -d -u $MD
exit 1
fi
mount /dev/${MD}s3 ${DEST_CFG}
if [ ! $? -eq 0 ]
then
echo "Meet a problem for mounting cfg partition of the image."
exit 1
fi
mount /dev/${MD}s4 ${DEST_DATA}
if [ ! $? -eq 0 ]
then
echo "Meet a problem for mounting data partition of the image."
fi
echo "Successful mount BSDRP image into:"
echo " - root partition: ${DEST_ROOT}"
echo " - cfg partition : ${DEST_CFG}"
echo " - data partition : ${DEST_DATA}"
exit 0
}
# umount the image
umount_img () {
check_is_mount
umount ${DEST_ROOT}
if [ ! $? -eq 0 ]
then
echo "Meet a problem for umounting root partition of the image."
echo "Still in use ?"
exit 1
fi
umount ${DEST_CFG}
if [ ! $? -eq 0 ]
then
echo "Meet a problem for umounting cfg partition of the image."
echo "Still in use ?"
exit 1
fi
umount ${DEST_DATA}
if [ ! $? -eq 0 ]
then
echo "Meet a problem for umounting data partition of the image."
echo "Not a blocking problem"
fi
# Get the Memory Disk identifier:
. /tmp/bsdrp_image_tools.tmp
# Destroy memory disk:
echo "Destroy ${MD}"
mdconfig -d -u $MD
echo "Successful umount BSDRP image"
# cleanup
rm /tmp/bsdrp_image_tools.tmp
exit 0
}
### Main function ###
getoption $*
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.