1 | #!/bin/sh
|
---|
2 | # Wrapper allowing to write image to card writer
|
---|
3 |
|
---|
4 | . $(dirname $0)/package-build.inc.sh
|
---|
5 |
|
---|
6 | BASEDIR=`dirname $0`
|
---|
7 | if [ -n "$1" ]; then
|
---|
8 | IMG=$1
|
---|
9 | else
|
---|
10 | CFG="${BASEDIR}/../cfg/nanobsd.wleiden"
|
---|
11 |
|
---|
12 | # Find object directory
|
---|
13 | eval `grep '^NANO_NAME=' ${CFG}`
|
---|
14 | OBJDIR="/usr/obj/nanobsd.${NANO_NAME}"
|
---|
15 | IMG=${OBJDIR}/_.disk.full
|
---|
16 | fi
|
---|
17 |
|
---|
18 | if [ ! -r "${IMG}" ]; then
|
---|
19 | p_err Source ${IMG} does not exists
|
---|
20 | fi
|
---|
21 | IMGSIZE=`ls -l ${IMG} | awk '{print $5 / 1024 / 1024}'`
|
---|
22 |
|
---|
23 | # Make sure we are root from this point on
|
---|
24 | f_check_root
|
---|
25 |
|
---|
26 | # Find which daX device holds a active flash drive
|
---|
27 | CARD_FOUND=0
|
---|
28 | DA_LIST=`cd /dev/; echo da[0-9]`
|
---|
29 | for DA in ${DA_LIST}; do
|
---|
30 | diskinfo ${DA} 1>/dev/null 2>/dev/null
|
---|
31 | if [ $? -eq 0 ]; then
|
---|
32 | CARD_FOUND=1
|
---|
33 | break
|
---|
34 | fi
|
---|
35 | done
|
---|
36 |
|
---|
37 | if [ ${CARD_FOUND} -eq 0 ]; then
|
---|
38 | p_err Sorry no flash card found at active devices, list searched:
|
---|
39 | p_err $(p_list ${DA_LIST})
|
---|
40 | exit 1
|
---|
41 | fi
|
---|
42 |
|
---|
43 | # Detect cardreader type/version
|
---|
44 | CARDREADER=`dmesg | grep ${DA}: | awk -F'[<>]' '/Removable Direct Access/ {print $2}' | tail -1`
|
---|
45 |
|
---|
46 | #XXX: Issue last chance warning, prompting the user to bail out
|
---|
47 | p_warn Going to write ${IMG} "(${IMGSIZE}MB)" to ${DA} "(${CARDREADER})"
|
---|
48 | p_warn 5 seconds to quit using CTRL+C
|
---|
49 | p_sleep 5
|
---|
50 |
|
---|
51 | TMPFILE=`mktemp -t dd`
|
---|
52 | p_info Writing image... "(`date`)"
|
---|
53 | # Sending INFO to dd seems to confuse it sometimes so
|
---|
54 | # make it EXPERIMENTAL for now
|
---|
55 |
|
---|
56 | if [ -n "$ENHANCED_DD" ]; then
|
---|
57 | dd if=${IMG} of=/dev/${DA} bs=64k 2>${TMPFILE} &
|
---|
58 | DD_PID=$!
|
---|
59 | sleep 0.5
|
---|
60 | while `ps ${DD_PID} >/dev/null`; do
|
---|
61 | kill -INFO ${DD_PID}
|
---|
62 | sleep 0.1 #Micro delay allow TMPFILE to populate
|
---|
63 | SIZE_DONE=`awk '/transferred/ {print $1}' ${TMPFILE}`
|
---|
64 | SIZE_DONE=`echo "${SIZE_DONE} / 1024 / 1024" | bc -l`
|
---|
65 | PERCENT_DONE=`echo "${SIZE_DONE} / ${IMGSIZE} * 100" | bc -l`
|
---|
66 | p_info `date "+%H:%m:%S"` `printf "%.02f MB (%.01f%%)" ${SIZE_DONE} ${PERCENT_DONE}`
|
---|
67 | p_sleep 10
|
---|
68 | : > ${TMPFILE}
|
---|
69 | done
|
---|
70 | else
|
---|
71 | dd if=${IMG} of=/dev/${DA} bs=64k
|
---|
72 | fi
|
---|
73 | rm ${TMPFILE}
|
---|
74 | p_info Image writing succesfull at /dev/$DA "(`date`)"
|
---|