source: hybrid/branches/releng-9.0/nanobsd/tools/write-image.sh@ 10608

Last change on this file since 10608 was 10608, checked in by rick, 13 years ago

The conditional statements and variables where not set correctly at all (last
commit properly done too late).

While here:

  • Cleanup logging format to logger, as all but the PREINIT hooks get called in privileged [priv] (forked) environment, where stdout goes to nothing.
  • Print more details to yield more logic to the user.

Related-To: nodefactory#129

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
File size: 1.9 KB
Line 
1#!/bin/sh
2# Wrapper allowing to write image to card writer
3
4. $(dirname $0)/package-build.inc.sh
5
6BASEDIR=`dirname $0`
7if [ -n "$1" ]; then
8 IMG=$1
9else
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
16fi
17
18if [ ! -r "${IMG}" ]; then
19 p_err Source ${IMG} does not exists
20fi
21IMGSIZE=`ls -l ${IMG} | awk '{print $5 / 1024 / 1024}'`
22
23# Make sure we are root from this point on
24f_check_root
25
26# Find which daX device holds a active flash drive
27CARD_FOUND=0
28DA_LIST=`cd /dev/; echo da[0-9]`
29for 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
35done
36
37if [ ${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
41fi
42
43# Detect cardreader type/version
44CARDREADER=`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
47p_warn Going to write ${IMG} "(${IMGSIZE}MB)" to ${DA} "(${CARDREADER})"
48p_warn 5 seconds to quit using CTRL+C
49p_sleep 5
50
51TMPFILE=`mktemp -t dd`
52p_info Writing image... "(`date`)"
53# Sending INFO to dd seems to confuse it sometimes so
54# make it EXPERIMENTAL for now
55
56if [ -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
70else
71 dd if=${IMG} of=/dev/${DA} bs=64k
72fi
73rm ${TMPFILE}
74p_info Image writing succesfull at /dev/$DA "(`date`)"
Note: See TracBrowser for help on using the repository browser.