File: ltsp-client-builder.postinst

package info (click to toggle)
ltsp 5.5.4-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,440 kB
  • ctags: 359
  • sloc: sh: 5,106; ansic: 1,590; perl: 280; python: 197; makefile: 171; lex: 152; yacc: 59
file content (258 lines) | stat: -rw-r--r-- 6,577 bytes parent folder | download | duplicates (4)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#! /bin/sh
set -e

. /usr/share/debconf/confmodule

log () {
    logger -t ltsp-client-builder "$@"
}

# default to cdrom
USE_CDROM=true
if db_get ltsp-client-builder/use_cdrom ; then
    case $RET in
        true|false) USE_CDROM="$RET" ;;
        *) log "unknown value for ltsp-client-builder/use_cdrom: $RET" ;;
    esac
fi

# check if we have a valid CD-ROM mounted
if [ "$USE_CDROM" != "false" ] && [ ! -f /cdrom/.disk/info ]; then
    log "no CD-ROM found ! Not installing ltsp chroot"
    exit 0
fi

# get the ok from the user to build the ltsp chroot
db_input medium ltsp-client-builder/run || [ $? -eq 30 ]
if ! db_go; then
    exit 10 # back up to menu
fi

db_get ltsp-client-builder/run
if [ "$RET" = false ]; then
    log "user requested no ltsp chroot"
    exit 0
fi

#load and use generic arguments
if db_get ltsp-client-builder/build-client-opts && [ -n "$RET" ] ; then
    BUILD_CLIENT_OPTS="$RET"
    log "ltsp-build-client options: $BUILD_CLIENT_OPTS"
fi

# configure network interface
if db_get ltsp-client-builder/configure_network_interface && [ "$RET" != "false" ]; then
    CONFIGURE_NETWORK_INTERFACE=true
fi

# handle NBD images
if db_get ltsp-client-builder/ltsp_update_image && [ "$RET" != "false" ]; then
    UPDATE_IMAGE=true
fi

# build the chroot
db_progress START 0 7 ltsp-client-builder/progress

if db_get ltsp-client-builder/server_packages && [ -n "$RET" ]; then
    SERVER_PACKAGES="$RET"
fi
for package in $SERVER_PACKAGES ; do
    log "Installing package $package in target"
    apt-install $package
done

db_progress STEP 1

if [ "$USE_CDROM" != "false" ] && [ ! -f /target/media/cdrom/.disk/info ]; then
    # Read mountpoints of cdrom devices from /proc/mounts, and mount
    # at /target/media/cdrom/
    while [ ! -f /target/media/cdrom/.disk/info ] && read device mountpoint otherstuff ; do
        case "$mountpoint" in
            *cdrom*) log "mounting $device on /target/media/cdrom"
                 mount "$device" /target/media/cdrom
                 ;;
        esac
    done < /proc/mounts
    if [ ! -f /target/media/cdrom/.disk/info ]; then
	# Last-ditch failsafe...
	log "Mounting /media/cdrom in the chroot."
	in-target mount /media/cdrom
    fi
fi

# workaround for: http://bugs.debian.org/390647
# Must be done in chroot in case /etc/resolv.conf is a absolute
# symlink (resolvconf)
in-target /bin/touch /etc/resolv.conf

running_stamp=/var/run/ltsp-build-client.running
failed_stamp=/var/run/ltsp-build-client.failed
if [ -f "$running_stamp" ]; then
    log "ERROR: ltsp-build-client already running... $running_stamp"
    exit 1
fi
if [ -f "$failed_stamp" ]; then
    log "note: removing failed stamp from previous run"
    rm -f "$failed_stamp"
fi
touch $running_stamp

progress_log=/target/var/log/ltsp-build-client.progress.log
# remove progress.log from previous attempt
test -f "$progress_log"  && rm -f "$progress_log"

(
    if in-target ltsp-build-client $BUILD_CLIENT_OPTS ; then
	rm -f "$running_stamp" 
    else
	touch "$failed_stamp" 
    fi
) < /dev/null > /tmp/ltsperror.txt 2>&1 \
    3> /dev/null 4>&3 5>&3 6>&3 7>&3 8>&3 9>&3 &
pid=$!

# some progress handling...
old=0
while [ -f "$running_stamp" ]; do
    if [ -f "$failed_stamp" ]; then
        log "ERROR: ltsp-build-client failed to run"
        rm -f "$failed_stamp"
        rm -f "$running_stamp"
        db_progress STOP
        exit 1
    fi
    i=1
    if [ -f "$progress_log" ]; then
        while read line ; do
            i=$(($i+1))
        done < "$progress_log"
    fi
    if [ "$i" -gt "$old" ] && [ "$i" -gt "1" ]; then
        db_progress SET "$i"
    fi
    sleep 1
    old="$i"
done

# Wait for the ltsp-build-client process to stop, to make sure we
# avoid race conditions when talking to debconf.
wait $pid

db_progress STOP

ltsp_update_image(){

# ensure squashfs-tools is available in the chroot
apt-install squashfs-tools

# make sure the keys are up to date with the new interface
chroot /target ltsp-update-sshkeys

# compress the image
# (we need the percentage of mksquashfs' output
# need to pipe through a file ...)

COMPRESS_LOG=/var/log/ltsp-image-build.log

db_progress START 0 100 ltsp-client-builder/compress

chroot /target ltsp-update-image >$COMPRESS_LOG 2>&1 &
PID=$!

OLDVAL=0
while [ -z ${LOOP} ]; do
    rawval=$(tail -1 $COMPRESS_LOG |sed -e 's/^.* //')
    if [ "$(echo $rawval|grep '[0-9]\%$')" ];then
        val=$(echo $rawval|tr -d %)
        if [ "$val" -gt 95 ]; then
            db_progress SET 100
            sleep 3
            LOOP="False"
        else
            if [ "$val" -gt "${OLDVAL}" ];then
                db_progress SET $val
                OLDVAL=$val
            fi
        fi
    fi
done
wait $PID

# save compression log
if [ -e $COMPRESS_LOG ]; then
    mv $COMPRESS_LOG /target/var/log/
fi

db_progress STOP
}


# configure a default interface for usage with LTSP
NET=192.168.0
DEFAULTROUTE_IFACE="$(route -n|grep ^0|sed -e 's/.* //')"
IFACE_LIST="$(cat /proc/net/dev|sed -e 's/^ *//' -e 's/:.*//'|grep -Ev '(\||lo|sit)')"

interfaces_entry() {
	cat <<EOF >>/target/etc/network/interfaces

auto $IFACE_LIST
iface $IFACE_LIST inet static
    address $NET.254
    netmask 255.255.255.0
    network $NET.0
    broadcast $NET.255

EOF

chroot /target ifconfig $IFACE_LIST $NET.254
}

configure_network_interface() {
# if we already have a matching interface, there is no need to go on
if [ -n "$(ifconfig|grep $NET)" ];then
	exit 0
fi

# exclude the interface with the default route, we only want unconfigured ones
IFACE_TMP=""
for iface in $IFACE_LIST;do
	if [ $iface != $DEFAULTROUTE_IFACE ]; then
		IFACE_TMP="${IFACE_TMP:+$IFACE_TMP}$iface"
	fi
done
IFACE_LIST=$IFACE_TMP

# check if we still have more than one possible interface.
# if thats the case, show a selector with the detected ones
if [ "$(echo ${IFACE_LIST}|wc -w)" -gt 1 ]; then
	db_subst ltsp-client-builder/dhcp-interface choices "$(echo "$IFACE_LIST" | sed 's/ /, /g')"

	db_fset ltsp-client-builder/dhcp-interface seen false
	db_input high ltsp-client-builder/dhcp-interface || true
    db_go || true

	db_get ltsp-client-builder/dhcp-interface || true
	IFACE_LIST="$RET"

	interfaces_entry
elif [  -z "$IFACE_LIST" ];then
	# we didnt get a proper interface, tell the user about it
	db_fset ltsp-client-builder/dhcp-manual seen false
	db_input high ltsp-client-builder/dhcp-manual || true
	db_go || true
else
	interfaces_entry
fi
}

if [ "$UPDATE_IMAGE" = "true" ]; then
    ltsp_update_image
fi

if [ "true" = "$CONFIGURE_NETWORK_INTERFACE" ]; then
    configure_network_interface
fi

#DEBHELPER#

exit 0