forked from waggle-sensor/waggle_image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_node_id.sh
executable file
·88 lines (60 loc) · 2.07 KB
/
create_node_id.sh
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
#!/bin/bash
set -e
if [ -e /media/boot/boot.ini ] ; then
export ODROIDMODEL=`head -n 1 /media/boot/boot.ini | cut -d '-' -f 1`
fi
echo "ODROIDMODEL: $ODROIDMODEL"
if [ $(echo $ODROIDMODEL | grep "^ODROID" | wc -l) -eq 0 ] ; then
echo "warning: could not detect ODROID model"
fi
### Node ID
export NODE_ID=""
#IS_ODROIDC=$(echo $ODROIDMODEL | grep "^ODROIDC" | wc -l)
# try MAC address (some older models do not have unique MAC addresses)
# try to detect network device, e.g. "eth0"
#export NETWORK_DEVICE=$(ifconfig -a | grep "Ethernet" | grep "^eth" | sort | head -n 1 | grep -o "^eth[0-9]" | tr -d '\n')
export NETWORK_DEVICE="eth0"
echo "NETWORK_DEVICE: ${NETWORK_DEVICE}"
# wait for network device
while [ $( ifconfig ${NETWORK_DEVICE} > /dev/null 2>&1 ; echo $? ) -ne 0 ] ; do
echo "device ${NETWORK_DEVICE} not found, retry in a few seconds"
sleep 3
done
export MACADDRESS=""
# wait for successful read
while [ "${MACADDRESS}x" == "x" ] ; do
MACADDRESS=`ifconfig ${NETWORK_DEVICE} | head -n 1 | grep -o "[[:xdigit:]:]\{17\}" | sed 's/://g'`
sleep 3
done
echo "MACADDRESS: ${MACADDRESS}"
if [ ! ${#MACADDRESS} -ge 12 ]; then
echo "error: could not extract MAC address"
exit 1
else
NODE_ID="0000${MACADDRESS}"
fi
# try memory card serial number
export CID_FILE="/sys/block/mmcblk0/device/cid"
if [ "${NODE_ID}x" == "x" ] && [ -e ${CID_FILE} ]; then
echo "try using serial number from SD-card"
# some devices do not have a unique MAC address, they could use this code
export SERIAL_ID=`python -c "cid = '$(cat ${CID_FILE})' ; len=len(cid) ; mid=cid[:2] ; psn=cid[-14:-6] ; print mid+psn"`
if [ ! ${#SERIAL_ID} -ge 11 ]; then
echo "warning: could not create unique identifier from SD-card serial number"
else
NODE_ID="000000${SERIAL_ID}"
fi
fi
echo "NODE_ID: ${NODE_ID}"
# try random number
if [ "${NODE_ID}x" = "x" ] ; then
NODE_ID=`openssl rand -hex 8`
fi
echo "NODE_ID: ${NODE_ID}"
if [ "${NODE_ID}x" = "x" ] ; then
echo "could not generate NODE_ID"
exit 1
fi
#save node ID
mkdir -p /etc/waggle/
echo ${NODE_ID} > /etc/waggle/node_id