-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnilrt-make-netboot-image.sh
executable file
·213 lines (165 loc) · 6.51 KB
/
nilrt-make-netboot-image.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
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
#!/bin/bash
set -euo pipefail
umask 0133
function error()
{
echo "ERROR: $*"
false
}
function warning()
{
echo "WARNING: $*"
}
function status()
{
echo "INFO: $*"
}
function print_help_and_exit () {
cat <<ENDHELP
Usage: $0 -i /mnt/userfs -o /media/my_usb_storage/nilrt-image.dir [ -k /mnt/userfs/boot/runmode/bzImage ]
Creates a PXE bootable image of an NI Linux RT system. This image is a
direcotry containing the runmode kernel and an inital RAM disk (initrd)
image of it's runmode filesystem, with identity information (like
hostname, network config, ssh keys, certificates, etc) stripped out.
Args:
-i: Path to root filesystem of directory NI Linux RT runmode which
are copied into the initrd image.
-o: Output direcotry where image file will be stored. All existing
files and dirs under this path will be erased and replaced with
generated image. This must be an absolute path beginning with '/'.
-k: (Optional) Path to kernel image. Defaults to /boot/runmode/bzImage
under the sysroot. This must be an absolute path beginning with
'/' if specified.
-b (Optional) Files and directories to blacklist - exclude from image.
You can pass this parameter multiple times. '*' is wildcard, which
matches anything. E.g. Passing '-b foo.txt -b bar.dir -b gaz.dir/*'
excludes the file /foo.txt, /bar.dir but NOT it's contents, and
/gaz.dir's contents but NOT /gaz.dir itself. Paths are relative to
the specified root filesystem (-i option).
Written by Haris Okanovic <haris.okanovic@ni.com> to demonstrate
network boot capabilities of NI Linux RT. Use at your own risk.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ENDHELP
if [ $# -gt 0 ]; then
error "$*" || true
echo ""
false
else
exit 0
fi
}
image_dir=""
sysroot_dir=""
kernel_img_file=""
blacklist_paths=()
while getopts "ho:i:k:b:" opt; do
case "$opt" in
h ) print_help_and_exit ;;
o ) image_dir="$OPTARG" ;;
i ) sysroot_dir="$OPTARG" ;;
k ) kernel_img_file="$OPTARG" ;;
b ) blacklist_paths+=( "$OPTARG" )
esac
done
shift $(($OPTIND - 1))
[ -n "$image_dir" ] || print_help_and_exit "Must specify output image path with -o"
[ "${image_dir:0:1}" == "/" ] || print_help_and_exit "Output image path (-o) must be absolute, start with \"/\""
[ ! -e "$image_dir" ] || warning "$image_dir already exists, overwriting"
[ -n "$sysroot_dir" ] || print_help_and_exit "Must specify sysroot directory with -i"
if [ -n "$kernel_img_file" ]; then
[ "${kernel_img_file:0:1}" == "/" ] || print_help_and_exit "Kernel image path (-k) must be absolute, start with \"/\""
[ -f "$kernel_img_file" ] || error "$kernel_img_file does not exist"
else
if [ -e "$sysroot_dir/boot/runmode/bzImage" ]; then
kernel_img_file="$sysroot_dir/boot/runmode/bzImage"
elif [ -e "/boot/runmode/bzImage" ]; then
warning "No kernel image found at \"$sysroot_dir/boot/runmode/bzImage\", defaulting to \"/boot/runmode/bzImage\""
kernel_img_file="/boot/runmode/bzImage"
else
error "No kernel image found at \"$sysroot_dir/boot/runmode/bzImage\" nor \"/boot/runmode/bzImage\", must specify with -k"
fi
fi
find_blacklist_exclude=()
for i in ${!blacklist_paths[@]}; do
path="${blacklist_paths[i]}"
[ "${path:0:1}" != "/" ] || print_help_and_exit "Blacklist path=$path must be relative to root filesystem, must not start with /"
# Relative to sysroot_dir
path="./$path"
find_blacklist_exclude+=( "-a" "-not" "-path" "$path" )
done
readonly TEMP_DIR=$(mktemp -d "/tmp/netboot-temp-XXXXXXX")
chmod 0700 "$TEMP_DIR"
function cleanup()
{
local exitCode="$?"
set +e
status "Cleanup TEMP_DIR=$TEMP_DIR"
rm -Rf "$TEMP_DIR"
exit "$exitCode"
}
trap cleanup EXIT
status "Clearing $image_dir"
rm -Rf "$image_dir"
[ ! -e "$image_dir" ] || error "Failed to clear image_dir=$image_dir"
mkdir -m 0755 "$image_dir"
status "Packing up $sysroot_dir into init"
cd "$sysroot_dir" >/dev/null
(
find "." -xdev \
-not -path "./boot/*" \
-a -not -path "./etc/natinst/share/*" \
-a -not -path "./init" \
-a -not -path "./etc/fstab" \
-a -not -path "./etc/hostname" \
-a -not -path "./etc/init.d/niopendisks" \
-a -not -path "./etc/init.d/niclosedisks" \
"${find_blacklist_exclude[@]}" \
-print
if [ -e "./etc/natinst/share" ]; then
find "./etc/natinst/share" -xdev \
-not -path "./etc/natinst/share/ni-rt.ini" \
-a -not -path "./etc/natinst/share/random-seed" \
-a -not -path "./etc/natinst/share/ssh/*" \
-a -not -path "./etc/natinst/share/certstore/open_csrs/*" \
-a -not -path "./etc/natinst/share/certstore/server_certs/*" \
-a -not -path "./etc/natinst/share/certstore/temp/*" \
-a -not -path "./etc/natinst/share/certstore/certstore/wireless/client/*" \
-a -not -path "./etc/natinst/share/certstore/certstore/wireless/pac/*" \
"${find_blacklist_exclude[@]}" \
-print
fi
) | cpio --quiet -H newc -o | gzip -9 -n > "$image_dir/init"
cd - >/dev/null
status "Create override dir"
readonly OVR_DIR="$TEMP_DIR/override"
mkdir -m 0755 "$OVR_DIR"
ln -sf "sbin/init" "$OVR_DIR/init"
if [ -e "$sysroot_dir/etc/fstab" ]; then
mkdir -m 0755 "$OVR_DIR/etc"
sed "/^LABEL=/d; /^\/dev\//d" "$sysroot_dir/etc/fstab" > "$OVR_DIR/etc/fstab"
chmod 0644 "$OVR_DIR/etc/fstab"
fi
if [ -e "$sysroot_dir/etc/natinst/share/ni-rt.ini" ]; then
mkdir -m 0755 "$OVR_DIR/etc/natinst"
mkdir -m 0755 "$OVR_DIR/etc/natinst/share"
sed "/^PrimaryMAC=/d; /^host_name=/d" "$sysroot_dir/etc/natinst/share/ni-rt.ini" > "$OVR_DIR/etc/natinst/share/ni-rt.ini"
fi
status "Appending $OVR_DIR to init"
cd "$OVR_DIR" >/dev/null
find "." -xdev "${find_blacklist_exclude[@]}" -print | cpio --quiet -H newc -o | gzip -9 -n >> "$image_dir/init"
cd - >/dev/null
status "Copying $kernel_img_file"
cp "$kernel_img_file" "$image_dir/"
cd "$image_dir" >/dev/null
status "Checksum files"
sha256sum * > SHA256SUM
status "Successfully created image at $image_dir"
ls -alFh
cd - >/dev/null
status "DONE"