1 | #!/bin/sh
|
---|
2 | # Get nanobsd image ready to be booted from NFS
|
---|
3 | # NFS instructions at
|
---|
4 | # http://www.wirelessleiden.nl/projects/nodefactory/wiki/TestingViaNFS
|
---|
5 |
|
---|
6 | IMAGE_BASE="/usr/obj/nanobsd.wleiden"
|
---|
7 | IMAGE_SLICE="${IMAGE_BASE}/_.disk.image"
|
---|
8 | IMAGE_FULL="${IMAGE_BASE}/_.disk.full"
|
---|
9 | IMAGE_NFS="${IMAGE_SLICE}-nfs"
|
---|
10 | # Structure: $NFSBASE
|
---|
11 | # ./cfg = /cfg mounpoint
|
---|
12 | # ./base = / mountpoint
|
---|
13 | # ./nfs = /nfs mountpoint
|
---|
14 | NFSBASE='/usr/data'
|
---|
15 | MNT="${NFSBASE}/base"
|
---|
16 | CFG="${NFSBASE}/cfg"
|
---|
17 | NFS="${NFSBASE}/nfs"
|
---|
18 |
|
---|
19 | p_err() {
|
---|
20 | echo "[ERROR] $*" 1>&2
|
---|
21 | }
|
---|
22 |
|
---|
23 | usage() {
|
---|
24 | (
|
---|
25 | echo "Usage: $0 [-fn]"
|
---|
26 | echo " -f force umount, memory device whipes"
|
---|
27 | echo " -n do not delete/clean cfg partition"
|
---|
28 | echo " -u unload/eject procedure"
|
---|
29 | ) 1>&2
|
---|
30 | exit 2
|
---|
31 | }
|
---|
32 |
|
---|
33 | # No Root, no fun
|
---|
34 | if [ `id -u` -ne 0 ]; then
|
---|
35 | p_err "Root only"
|
---|
36 | exit 1
|
---|
37 | fi
|
---|
38 |
|
---|
39 | # Argument parsing using getopts
|
---|
40 | OPT_FORCE=0
|
---|
41 | OPT_CLEAN=1
|
---|
42 | OPT_UNLOAD=0
|
---|
43 | while getopts "hfnu" OPT; do
|
---|
44 | case "$OPT" in
|
---|
45 | f) OPT_FORCE=1;;
|
---|
46 | n) OPT_CLEAN=0;;
|
---|
47 | u) OPT_UNLOAD=1;;
|
---|
48 | h) usage;;
|
---|
49 | \?) usage;;
|
---|
50 | esac
|
---|
51 | done
|
---|
52 |
|
---|
53 |
|
---|
54 | # Eeks, we are going to be nasty, hold your horses
|
---|
55 | if [ $OPT_FORCE -eq 1 -o $OPT_UNLOAD -eq 1 ]; then
|
---|
56 | umount -f $MNT
|
---|
57 | for MD in `mdconfig -l -v | grep "${IMAGE_NFS}" | awk '{print $1}'`; do
|
---|
58 | mdconfig -d -u $MD
|
---|
59 | done
|
---|
60 | fi
|
---|
61 |
|
---|
62 | if [ $OPT_UNLOAD -eq 1 ]; then
|
---|
63 | echo "All done"
|
---|
64 | exit 1;
|
---|
65 | fi
|
---|
66 |
|
---|
67 | # If mount point is already used, bail out
|
---|
68 | if mount | grep -q "${MNT}"; then
|
---|
69 | p_err "'${MNT}' already mounted"
|
---|
70 | exit 1
|
---|
71 | fi
|
---|
72 |
|
---|
73 | # If target nfs image is mounted somehow, bail out
|
---|
74 | if mdconfig -l -v | grep -q "${IMAGE_NFS}"; then
|
---|
75 | MD=`mdconfig -l -v | grep "${IMAGE_NFS}" | cut -c -4`
|
---|
76 | p_err "'${IMAGE_NFS}' already mounted at '$MD'"
|
---|
77 | exit 1
|
---|
78 | fi
|
---|
79 |
|
---|
80 |
|
---|
81 | # Prepare image for use with NFS
|
---|
82 | cp -v ${IMAGE_SLICE} ${IMAGE_NFS}
|
---|
83 |
|
---|
84 | MD=`mdconfig -a -t vnode -f ${IMAGE_NFS}`
|
---|
85 | mount /dev/${MD}a ${MNT}
|
---|
86 |
|
---|
87 | # Config files lives at NFS location
|
---|
88 | echo "mount -t nfs -o ro 192.168.4.1:${CFG}" > ${MNT}/conf/default/etc/remount
|
---|
89 |
|
---|
90 | # Create nfs mount location
|
---|
91 | mkdir ${MNT}/nfs
|
---|
92 |
|
---|
93 | # $MNT, $CFG, $NFS lives at nfs
|
---|
94 | (
|
---|
95 | echo "192.168.4.1:${MNT} / nfs ro 0 0"
|
---|
96 | echo "192.168.4.1:${CFG} /cfg nfs rw,noauto 0 0"
|
---|
97 | echo "192.168.4.1:${NFS} /nfs nfs rw 0 0"
|
---|
98 | ) > /${MNT}/conf/base/etc/fstab
|
---|
99 |
|
---|
100 | if [ ${OPT_CLEAN} -eq 1 ]; then
|
---|
101 | echo "DELETING all files at ${CFG}, start fresh ;-)"
|
---|
102 | rm -vfR ${CFG}/*
|
---|
103 | else
|
---|
104 | echo "PRESERVING all files at ${CFG}"
|
---|
105 | fi
|
---|
106 |
|
---|
107 | echo "DELETING all files at ${NFS}, start fresh ;-)"
|
---|
108 | rm -vfR ${NFS}/*
|
---|
109 |
|
---|
110 | # Allow build images to be used directy via NFS
|
---|
111 | ln -f ${IMAGE_SLICE} ${NFS}/`basename ${IMAGE_SLICE}`
|
---|
112 | ln -f ${IMAGE_FULL} ${NFS}/`basename ${IMAGE_FULL}`
|
---|
113 |
|
---|
114 | # XXX: Proper unmounting after all has finished
|
---|