Debian Codegraph @

setup/files/misc/init.drbl

raw
1 #!/bin/bash
2 # This file is borrowed from Debian's diskless.
3 # Modified by Steven Shiau <steven _at_ clonezilla org>, Ceasar Sun <ceasar _at_ nchc org tw>
4 # License: GPL
5 # 2006/02/28
6 # Thanks to James MacLean for providing clientdir method in kernel cmdline.
7 # it looks like:
8 # append initrd=initrd-pxe.img ramdisk_size=12288 devfs=nomount drblthincli=off selinux=0 clientdir=images
9
10 #
11 DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
12
13 # Append a default search path.
14 PATH="$PATH:/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:$DRBL_SCRIPT_PATH/sbin:$DRBL_SCRIPT_PATH/bin"
15 export PATH
16
17 # NFS opt for /etc, /var, /usr, /opt..
18 nfs_ver_opt="nfsvers=3"
19 # If need, you can add "rsize=65536,wsize=65536"
20 RO_NFS_EXTRA_OPT="ro,tcp,nolock,$nfs_ver_opt"
21 RW_NFS_EXTRA_OPT="rw,tcp,nolock,$nfs_ver_opt"
22
23 # This param nosharecache is for Kernel 2.6.23 (some in kernel 2.6.22 in Fedora) or later... Kernel 2.6.23 or later will detect the nfs mounting parameters are the same or not for mounting point and sub-tree mouting point. If not, it will exit unless nosharecache is added. See "man 5 nfs" or https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=251655 for more details.
24 NFS_OPT_TO_ADD="nosharecache"
25
26 # Flag to have tmpfs /run or not. This value will be enabled by drblpush if /run is found as tmpfs mounted on DRBL server.
27 TMPFS_RUN="true"
28
29 #
30 create_dev(){
31 # Forget the devfs, we create it by ourself...
32 echo -n "Unmounting the old dev if it exists... "
33 umount -l /dev 2>/dev/null
34 echo "done!"
35
36 if ! mount -n -o size=10240k,mode=0755 -t devtmpfs devtmpfs /dev; then
37 echo "W: devtmpfs not available, falling back to tmpfs for /dev"
38 mount -n -t tmpfs -o nr_inodes=24576 -o mode=0755 udev /dev
39 [ -e /dev/console ] || mknod -m 0600 /dev/console c 5 1
40 [ -e /dev/null ] || mknod /dev/null c 1 3
41 fi
42
43 echo -n "Creating dev... "
44 # Note! We put dev.tgz as the first priority so that if necessary, such MDK 10, we can force to use dev.tgz instead of udev. (Since in MDK10, there is /usr/sbin/udev, but no /usr/sbin/udevstart or /usr/sbin)
45 if [ -f /etc/diskless-image/dev.tgz ]; then
46 # the option "m" to make sure the time will NOT be extracted, it will use
47 # the time now, so that we can avoid the wrong clock or timezone.
48 echo -n "Using old-style dev instead of udev... "
49 tar -xzmf /etc/diskless-image/dev.tgz -C /dev
50 elif [ -n "$(ls /usr/sbin/udev* /usr/bin/udev* 2>/dev/null)" ]; then
51 echo -n "Mounting sysfs... "
52 mount -n -t sysfs none /sys
53 ln -snf /proc/self/fd /dev/fd
54 ln -snf /proc/self/fd/0 /dev/stdin
55 ln -snf /proc/self/fd/1 /dev/stdout
56 ln -snf /proc/self/fd/2 /dev/stderr
57 ln -snf /proc/kcore /dev/core
58 [ -d /dev/pts ] || mkdir -m 0755 /dev/pts
59 [ -d /dev/shm ] || mkdir -m 1777 /dev/shm
60 echo -n "Using udev... "
61 # http://www.redhat.com/archives/rhl-devel-list/2006-April/msg00785.html
62 # * Thu Apr 13 2006 Harald Hoyer <harald redhat com> - 089-1
63 # - version 089
64 # - start_udev uses udevtrigger now instead of udevstart
65 # From FC12, it's udevadm trigger
66 udevadm trigger
67
68 # Create console, null, since in FC5, udevstart won't create them.
69 [ ! -e /dev/console ] && mknod -m 600 /dev/console c 5 1
70 [ ! -e /dev/null ] && mknod -m 666 /dev/null c 1 3
71 else
72 echo "Failed to create the dev filesystem!"
73 echo "System will not be able to function!"
74 echo "Program terminated!"
75 exit 1
76 fi
77 echo "done!"
78 } # end of create_dev()
79 #
80 do_nfs_mount() {
81 local nfs_src="$1"
82 local mnt_point="$2"
83 local nfs_opt="$3"
84 local nfs_opt_try="$4"
85 local flag
86 flag="0"
87 # Ex: mount -t nfs -n -o $RW_NFS_EXTRA_OPT $nfsserver:$nfshostsdir/$IP/etc /etc 2>/dev/null
88 mount -t nfs -n -o $nfs_opt $nfs_src $mnt_point 2>/dev/null
89 if [ "$?" -gt 0 ]; then
90 echo -n "Try to mount with option \"$NFS_OPT_TO_ADD\"... "
91 # Ex: mount -t nfs -n -o $RW_NFS_EXTRA_OPT,$NFS_OPT_TO_ADD $nfsserver:$nfshostsdir/$IP/etc /etc
92 mount -t nfs -n -o $nfs_opt,$nfs_opt_try $nfs_src $mnt_point
93 [ "$?" -eq 0 ] && flag="1"
94 fi
95 return $flag
96 } # end of do_nfs_mount
97 #
98 add_extra_nfs_opt_to_fstab() {
99 echo "Try to add $NFS_OPT_TO_ADD in /etc/fstab..."
100 if [ -z "$(grep -Ew "nosharecache" /etc/fstab)" ]; then
101 # Make it like:
102 # 192.168.130.254:/tftpboot/nodes/192.168.130.1/etc /etc nfs rw,rsize=65536,wsize=65536,tcp,nosharecache,defaults 0 0
103 perl -pi -e "s|(^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+:.*[[:space:]]+.*nfs.*)(,defaults.*)|\$1,nosharecache\$2|g" /etc/fstab
104 fi
105 } # end of add_extra_nfs_opt_to_fstab
106 #
107 remove_extra_nfs_opt_to_fstab() {
108 echo "Try to remove $NFS_OPT_TO_ADD in /etc/fstab..."
109 if [ -n "$(grep -Ew "nosharecache" /etc/fstab)" ]; then
110 # Make it like:
111 # 192.168.130.254:/tftpboot/nodes/192.168.130.1/etc /etc nfs rw,rsize=65536,wsize=65536,tcp,defaults 0 0
112 perl -pi -e "s|,nosharecache||g" /etc/fstab
113 fi
114 } # end of remove_extra_nfs_opt_to_fstab() {
115 maybe_break(){
116 # Refer from Debian live-boot
117 if echo "${break:-}" | grep -E -q "(,|^)$1(,|$)"; then
118 if command -v chvt >/dev/null 2>&1; then
119 chvt 1
120 fi
121
122 echo "break point=$break"
123
124 #run_scripts /scripts/panic
125 for i in 1 2 3 4 5 6; do
126 [ -c /dev/tty$i ] || mknod /dev/tty$i c 4 $i
127 done
128 REASON="$@" PS1='(init.drbl) ' /bin/sh -i </dev/console >/dev/console 2>&1
129 fi
130 }
131 parse_cmd_append(){
132 for x in $(cat /proc/cmdline); do
133 case $x in
134 break=*)
135 break=${x#break=}
136 ;;
137 esac
138 done
139
140 }
141
142 ############
143 ### MAIN ###
144 ############
145 # Don't do anything special if computer is already booted
146 if [ $$ -ne 1 ]; then
147 exec /usr/sbin/init $*
148 echo "FATAL ERROR: exec /usr/sbin/init failed" >&2
149 exit 1
150 fi
151
152 # Don't do anything special if root is rw (ie master system).
153 if touch /test-rw 2> /dev/null; then
154 rm /test-rw
155 echo "WARNING: master system installed" >&2
156 exec /usr/sbin/init $*
157 echo "FATAL ERROR: exec /usr/sbin/init failed" >&2
158 exit 1
159 fi
160
161 # Try and find configuration file
162 if [ ! -f /etc/diskless-image/config ]; then
163 echo "ERROR: Cannot find image config file /etc/diskless-image/config!"
164 echo " Not mounting NFS-root directories"
165 echo "Program terminated!"
166 exit 1
167 fi
168
169 # Load config file
170 . /etc/diskless-image/config
171
172 # To get IP address, we need to mount /proc first
173 echo -n "Mounting /proc in init... "
174 mount -n none /proc -t proc
175 echo "done!"
176
177 # Remount the root filesystem read-write.
178 # For kernel 2.6.18, it seems if root is ro, all the mount later (such as /etc, /var) will be ro.
179 echo -n "Remounting root filesystem in read-write mode... "
180 mount -n -o remount,rw / 2>/dev/null
181 echo "done!"
182
183 # Create the necessary dev files, this should be as early as possible
184 create_dev
185
186 if [ "$TMPFS_RUN" = "true" ]; then
187 [ ! -d /run ] && mkdir /run
188 mount -t tmpfs -o "nosuid,size=20%,mode=0755" tmpfs /run
189 mkdir /run/initramfs
190 fi
191
192 # find my IP address
193 NETDEVICES="$(LC_ALL=C cat /proc/net/dev | awk -F: '/eth.:|tr.:|p.p.:/{print $1}')"
194 for DEVICE in $NETDEVICES; do
195 # //NOTE// Here we have to use grep "-w" to grep the IPv4 address, avoding getting IPv6 address.
196 # E.g.:
197 # eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
198 # inet 192.168.77.254 netmask 255.255.255.0 broadcast 192.168.77.255
199 # inet6 fe80::20c:29ff:fee9:f24e prefixlen 64 scopeid 0x20<link>
200 IP_tmp="$(LC_ALL=C ip addr show dev $DEVICE | awk '/inet / {print $2}' | cut -d/ -f1 | head -n1)"
201 if [ -n "$IP_tmp" ]; then
202 # Got the IP address, stop to get from other port, so break
203 IP=$IP_tmp
204 echo "My IP address is $IP ([$DEVICE])."
205 break
206 fi
207 done
208
209 # Find NFS server
210 for i in $NFSSERVER_LIST; do
211 # Use the clients-of-$nfsserver.txt in the common root.
212 if [ -n "$(grep -Ew "$IP" /etc/drbl/clients-of-${i}.txt 2>/dev/null)" ]; then
213 nfsserver="$i"
214 break
215 fi
216 done
217 if [ -z "$nfsserver" ]; then
218 # No NFS server found. Use plan B. Try to use gateway as NFS server
219 gateway="$(LC_ALL=C ip route show default 2>/dev/null | awk '/^default/ {for(i=1;i<=NF;i++) if($i=="via") print $(i+1)}')"
220 if [ -n "$gateway" ]; then
221 nfsserver=$gateway
222 else
223 echo "NFSSERVER is not found, use default one: \"$nfsserver_default\"."
224 nfsserver=$nfsserver_default
225 fi
226 fi
227
228 if [ -n "$nfsserver" ]; then
229 echo "NFS server is $nfsserver."
230 else
231 echo "No NFS Server was found!"
232 echo "Program terminated!"
233 exit 1
234 fi
235
236 maybe_break drbl_prenfs
237
238 # Check if single system image is on
239 ssi_flag=0
240 # For PXE, it's clientdir=node_root in /proc/cmdline, while
241 # for GRUB uEFI NB, it's clientdir=\"node_root\"
242 #(added by function add_opt_in_grub_efi_cfg_block) in /proc/cmdline.
243 # It should be converted to be clientdir="node_root" by grub,
244 # like the cases in the booting of CD or USB flash drive.
245 # However somehow the GRUB uEFI NB has different behavior.
246 # Therefore here we have to deal with clientdir=node_root or clientdir=\"node_root\"
247 clientdir="$(LC_ALL=C grep clientdir /proc/cmdline | sed -e "s/.*clientdir=\([^ ]\+\).*$/\1/" | sed -e 's|\"||g' -e 's|\\||g')"
248 [ -n "$clientdir" ] && ssi_flag=1
249
250 # The flag to modify /etc/fstab
251 NFS_OPT_TO_ADD_FLAG=0
252
253 # client's /etc directory (1) NFS (2) created from a tarball template
254 if [ "$ssi_flag" = 0 ]; then
255 # Normal mode, every client has its own /etc, /var
256 echo -en "\\033[0;33m"
257 echo "Full DRBL mode: I have my own NFS-based /etc and /var directories."
258 echo -en "\\033[0;39m"
259 echo -n "Mounting NFS dir /etc... "
260 do_nfs_mount $nfsserver:$nfshostsdir/$IP/etc /etc $RW_NFS_EXTRA_OPT $NFS_OPT_TO_ADD
261 NFS_OPT_TO_ADD_FLAG=$((NFS_OPT_TO_ADD_FLAG + $?))
262 echo "done!"
263 echo -n "Mounting NFS dir /var... "
264 do_nfs_mount $nfsserver:$nfshostsdir/$IP/var /var $RW_NFS_EXTRA_OPT $NFS_OPT_TO_ADD
265 NFS_OPT_TO_ADD_FLAG=$((NFS_OPT_TO_ADD_FLAG + $?))
266 echo "done!"
267 else
268 # DRBL SSI mode
269 # We need to know if it's in clonezilla box mode (DRBL SSI + runlevel=1)
270 # Command "runlevel" in this stage is useless. we have to check by ourself
271 # Check if "1" exists in /proc/cmdline
272 RL=""
273 if grep -qE ".*[[:space:]]+\<1\>([[:space:]]+|$)" /proc/cmdline 2>/dev/null; then
274 RL=1
275 # It's clonezilla box mode, client does not have to use some graphics
276 # setting, expecially large dir like /etc/gconf, /etc/X11, /etc/selinux
277 # In RH, we still need an empty directory /etc/X11 for kudzu to write
278 # X config,,otherwise kudzu will complain. For Debian, it's not necessary
279 # To make it simple, we will create /etc/X11 later.
280 ETC_EXCLUDE="--exclude=gconf --exclude=X11 --exclude=selinux"
281 echo -en "\\033[0;33m"
282 echo "Clonezilla box mode: I use tmpfs /etc and /var, based on tarball templates."
283 echo -en "\\033[0;39m"
284 else
285 # DRBL SSI mode
286 echo -en "\\033[0;33m"
287 echo "DRBL SSI mode: I use tmpfs /etc and /var, based on tarball templates."
288 echo -en "\\033[0;39m"
289 fi
290
291 echo -n "Creating one-time-use dir... "
292 echo -n "/root... "
293 mount -n -t tmpfs -o mode=0700 none /root
294 tar -xzmf /drbl_ssi/template_root.tgz -C /
295 echo -n "/etc... "
296 mount -n -t tmpfs -o mode=0755 none /etc
297 tar $ETC_EXCLUDE -xzmf /drbl_ssi/template_etc.tgz -C /
298 echo -n "/var... "
299 mount -n -t tmpfs -o mode=0755 none /var
300 tar -xzmf /drbl_ssi/template_var.tgz -C /
301 echo "done!"
302 fi
303
304 # NFS-based /usr is necessary both for normal mode and DRBL SSI mode
305 echo -n "Mounting NFS dir /usr... "
306 do_nfs_mount $nfsserver:/usr /usr $RO_NFS_EXTRA_OPT $NFS_OPT_TO_ADD
307 NFS_OPT_TO_ADD_FLAG=$((NFS_OPT_TO_ADD_FLAG + $?))
308 echo "done!"
309
310 # We have to modify the fstab for DRBL SSI mode (drbl-ssi-client-prepare).
311 # Perl (used in drbl-ssi-client-prepare) is only available after
312 # /usr is mounted.
313 if [ "$ssi_flag" = "1" ]; then
314 if [ "$RL" = "1" ]; then
315 # In clonezill box mode, to let kudzu shut up, we need /etc/X11 to write X config, since we already use --exclude=X11 to exclude that dir.
316 mkdir -p /etc/X11
317 else
318 # only DRBL SSI mode, not in clonezilla box mode, use NFS
319 # We need /var/lib/dpkg or /var/lib/rpm so that later in drbl-ssi-client-prepare, we can use dpkg or rpm to query package to get the GDM_CFG or KDE_CFG
320 if [ -d /var/lib/dpkg ]; then
321 echo -n "/var/lib/dpkg... "
322 do_nfs_mount $nfsserver:/tftpboot/node_root/var/lib/dpkg /var/lib/dpkg $RO_NFS_EXTRA_OPT $NFS_OPT_TO_ADD
323 NFS_OPT_TO_ADD_FLAG=$((NFS_OPT_TO_ADD_FLAG + $?))
324 elif [ -d /var/lib/rpm ]; then
325 echo -n "/var/lib/rpm... "
326 do_nfs_mount $nfsserver:/tftpboot/node_root/var/lib/rpm /var/lib/rpm $RO_NFS_EXTRA_OPT
327 NFS_OPT_TO_ADD_FLAG=$((NFS_OPT_TO_ADD_FLAG + $?))
328 fi
329 fi
330 echo "done!"
331 drbl-ssi-client-prepare $nfsserver "$RO_NFS_EXTRA_OPT" $RL
332 fi
333
334 #
335 if [ "$NFS_OPT_TO_ADD_FLAG" -eq 0 ]; then
336 remove_extra_nfs_opt_to_fstab
337 else
338 add_extra_nfs_opt_to_fstab
339 fi
340
341 maybe_break drbl_postnfs
342
343 # Clear mtab
344 rm -f /etc/mtab /etc/mtab~ /etc/mtab~~ /etc/nologin
345
346 # systemd v227 forces to have /etc/mtab as symbolic file to /proc/mounts.
347 # Ref: https://github.com/stevenshiau/drbl/issues/4
348 ## if, for any reason, mtab was not synced with what it should be, Sync it.
349 #while read ilist; do
350 # # skip the "rootfs / rootfs rw 0 0"
351 # [ -n "$(echo $ilist | grep -E "^rootfs \/ rootfs .*")" ] && continue
352 # echo $ilist >> /etc/mtab
353 #done < /proc/mounts
354 ln -fs /proc/mounts /etc/mtab
355
356 # umount the useless /initrd/proc and /initrd if they exist.
357 for i in /initrd/proc /initrd; do
358 if [ -n "$(awk -F " " '{print $2}' /etc/mtab | grep -E "^$i\>")" ]; then
359 umount -l $i
360 fi
361 done
362
363 # Public IP
364 if [ -n "$IP" ]; then
365 # public ip alias
366 if [ -e "/etc/diskless-image/public_ip.drbl" ]; then
367 # PUBLIC_IP result is like: 10.231.97.81 netmask 255.255.255.0
368 PUBLIC_IP="$(grep -Ew "$IP" /etc/diskless-image/public_ip.drbl | awk '{ print $2" netmask "$3; }')"
369 PUBLIC_GW="$(grep -Ew "$IP" /etc/diskless-image/public_ip.drbl | awk '{ print $4; }')"
370 if [ -z "$PUBLIC_IP" ]; then
371 PUBLIC_IP="$(grep -i -Ew "$HWADDR" /etc/diskless-image/public_ip.drbl | awk '{ print $2" netmask "$3; }')"
372 PUBLIC_GW="$(grep -i -Ew "$HWADDR" /etc/diskless-image/public_ip.drbl | awk '{ print $4; }')"
373 fi
374
375 if [ -n "$PUBLIC_IP" ]; then
376 # Convert "1.2.3.4 netmask 255.255.255.0" to "1.2.3.4/255.255.255.0"
377 _pub_ip="$(echo $PUBLIC_IP | awk '{print $1}')"
378 _pub_nm="$(echo $PUBLIC_IP | awk '{print $3}')"
379 echo "Setting public IP $_pub_ip/$_pub_nm in $DEVICE:1 by:"
380 echo "ip addr add $_pub_ip/$_pub_nm dev $DEVICE label $DEVICE:1"
381 LC_ALL=C ip addr add $_pub_ip/$_pub_nm dev $DEVICE label $DEVICE:1 && LC_ALL=C ip link set dev $DEVICE up
382 GW="$(LC_ALL=C ip route show default 2>/dev/null | awk '/^default/ {for(i=1;i<=NF;i++) if($i=="via") print $(i+1)}')"
383 if [ -n "$GW" ]; then
384 LC_ALL=C ip route del default via $GW
385 fi
386 echo "Setting routing table by:"
387 echo "ip route add default via $PUBLIC_GW dev $DEVICE"
388 LC_ALL=C ip route add default via $PUBLIC_GW dev $DEVICE
389 fi
390 fi
391 fi
392
393 # report success
394 echo "Done. NFS-root directories are mounted."
395 echo "Let original init take over..."
396
397 # A normal bootparam in /proc/cmdline for clonezilla client is like:
398 # initrd=initrd-pxe.img devfs=nomount drblthincli=off selinux=0 1 ocs_opt="--language en -b -g auto -y0 -p true --max-time-to-wait 300 --mcast-port 22 32 multicast_restoredisk 2disks hda hdb"
399 # We have to filter the ocs_opt to avoid some options, like -b or 1 inside it, which init will think it's emergency mode or other mode
400 pure_cmdline="$(sed -e "s/ocs_opt=\".*\"//g" /proc/cmdline)"
401
402 maybe_break drbl_pre_run_init
403
404 # Since /usr is NFS mounted to DRBL server's /usr now. The /usr/sbin/init is the original one.
405 exec /usr/sbin/init $pure_cmdline $*
406 # Only should get here if something went wrong
407 echo "FATAL ERROR: exec /usr/sbin/init failed" >&2
408 exit 1