summaryrefslogtreecommitdiffstats
path: root/core/modules/slx-issue/data/opt/openslx/scripts/openslx-create_issue
blob: de6331c7cf846aa9c2cb196870df2d1027361caf (plain) (blame)
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
#!/bin/bash
# Needs bash for string manipulation
# -----------------------------------------------------------------------------
#
# Copyright (c) 2012..2018 bwLehrpool-Projektteam
#
# This program/file is free software distributed under the GPL version 2.
# See https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
#
# If you have any feedback please consult https://bwlehrpool.de and
# send your feedback to bwlehrpool@hs-offenburg.de.
#
# General information about bwLehrpool can be found at https://bwlehrpool.de
#
# -----------------------------------------------------------------------------
#
# First script for initial ramfs for OpenSLX linux stateless clients
#############################################################################

export PATH=$PATH:/opt/openslx/bin:/opt/openslx/sbin

# Set greeting and add information about the booted system

declare -rg INFILE=/opt/openslx/etc/issue.template
declare -rg TMPFILE=$(mktemp)
declare -rg OUTFILE=/etc/issue

. /opt/openslx/config

# get_dmi_info <class>
get_dmi_info() {
	[ -n "$1" ] || return
	echo $(dmidecode -s $1 | grep -vP "unknown|filled|^#")
}
# Replace known variables and determine maximum line length
MAX=0
while IFS='' read -r line || [ -n "$line" ]; do
	line="${line//"%hostname%"/"$SLX_HOSTNAME"}"
	tst="${line//"%minspace%"/}"
	tst="${tst//"%space%"/}"
	len=${#tst}
	[ "$len" -gt "$MAX" ] && MAX=$len
	echo "$line"
done < "$INFILE" > "$TMPFILE"
player=$(< /etc/vmware/config grep -m1 '^product.version' | awk -F= '{print $2}')
kernel=$(uname -r)
system_manufacturer="$(get_dmi_info system-manufacturer)"
[ -z "$system_manufacturer" ] && system_manufacturer="$(get_dmi_info baseboard-manufacturer)"
system_product="$(get_dmi_info system-product-name)"
[ -z "$system_product" ] && system_product="$(get_dmi_info baseboard-product-name)"
system="$system_manufacturer $system_product"
[ -z "${system// /}" ] && system="<unknown>"

# Support the boot interface name eventually saved as SLX_PXE_NETIF
# from the new dracut-based stage3, fallback to old eth0 if not set
linkspeed=$(cat /sys/class/net/${SLX_PXE_NETIF:-eth0}/speed)

# Detect if the system is backed by a disk
if slx-tools fs_path_isvolatile /tmp; then
	tmpstatus="RAMDISK"
else
	tmpstatus="HDD"
fi
tmpstatus="$(slx-tools fs_path_space /tmp | awk '{printf "%.1f", $2 / 1024 / 1024}')GiB ($tmpstatus)"

cat >> "$TMPFILE" <<EOF
System model:%space% $system
Kernel version:%space% $kernel
Temporary directory:%space% $tmpstatus
Boot server:%space% $SLX_PXE_SERVER_IP
Client address:%space% $SLX_PXE_CLIENT_IP
EOF
if [ -n "$linkspeed" ]; then
	echo "Link speed:%space% ${linkspeed}MBit" >> "$TMPFILE"
fi
if [ -n "$SLX_SHUTDOWN_SCHEDULE" ]; then
	echo "Scheduled shutdown:%space% $SLX_SHUTDOWN_SCHEDULE" >> "$TMPFILE"
fi
if [ -n "$SLX_REBOOT_SCHEDULE" ]; then
	echo "Scheduled reboot:%space% $SLX_REBOOT_SCHEDULE" >> "$TMPFILE"
fi
if [ -n "$player" ]; then
	echo "VMware version:%space% ${player//'"'/}" >> "$TMPFILE"
fi

echo "" >> "$TMPFILE"

# Crop to console width
[ "$MAX" -lt 80 ] && MAX=80
export TERM=linux
T="$( tput cols 2> /dev/console < /dev/console )"
[ -z "$T" ] && T="$( tput cols 2> /dev/tty1 < /dev/tty1 )"
if [ -z "$T" ]; then
	T="$( stty size 2> /dev/console < /dev/console )"
	[ -z "$T" ] && T="$( stty size 2> /dev/tty1 < /dev/tty1 )"
	T="${T#* }"
fi
[ -n "$T" ] && [ "$T" -le "$MAX" ] && MAX=$(( T - 1 ))
unset TERM

# Fix up spacing for right-aligned text
while IFS='' read -r line || [ -n "$line" ]; do
	tst=${line/"%space%"/}
	tst=${tst/"%minspace%"/}
	if [ "${#line}" != "${#tst}" ]; then
		space=
		while true; do
			tst=${line/"%space%"/"$space"}
			tst2=${tst/"%minspace%"/"$space"}
			if [ "${#tst2}" -ge "$MAX" ]; then
				line="${tst/"%minspace%"/"${space:0:-2}"}"
				break
			elif [ "${#space}" -gt 0 ] && [ "$(( MAX - ${#tst2} ))" -ge "${#space}" ]; then
				space="$space$space"
			else
				space=" $space"
			fi
		done
	fi
	echo "${line:0:$MAX}"
done < "$TMPFILE" > "$OUTFILE"

rm -f -- "$TMPFILE"
exit 0