Release 2.9.23-4
[nbd.git] / debian / nbd-server.postinst
blob6e0a408828ae0cfa97cc2daaae4b9824ff964217
1 #! /bin/bash
2 # postinst script for nbd
4 # see: dh_installdeb(1)
6 set -e
8 . /usr/share/debconf/confmodule
10 # Quotes each argument for shell expression inclusion, to prevent
11 # interpretation of special characters.
12 function shell_quote () {
13 local first=true
14 while [ "$#" -gt 0 ]; do
15 if [ ! "$first" ]; then
16 echo -n ' '
18 # sed expression transforms instances of ' to '\''
19 echo -n "'$(echo "$1" | sed -e "s/'/'\\\\''/g")'"
20 first=
21 shift
22 done
25 # Convert an old-style configuration file to a new-style one
26 function convert_config () {
27 [ -f /etc/nbd-server.oldconf ] && . /etc/nbd-server.oldconf
28 [ -f /etc/nbd-server ] && . /etc/nbd-server
29 TMPFILE=$(mktemp /tmp/nbd-server.XXXXXX)
30 if [ "$AUTO_GEN" = "n" ]
31 then
32 db_input critical nbd-server/autogen
33 db_go
34 exit 1
36 cat /usr/share/nbd-server/nbd-server.conf.tmpl > $TMPFILE
37 while [ ! -z ${NBD_FILE[$i]} ]
39 nbd-server -o "export-$i" ${NBD_PORT[$i]} ${NBD_FILE[$i]} ${NBD_SERVER_OPTS[$i]} >> $TMPFILE
40 i=$(( $i + 1 ))
41 done
44 # summary of how this script can be called:
45 # * <postinst> `configure' <most-recently-configured-version>
46 # * <old-postinst> `abort-upgrade' <new version>
47 # * <conflictor's-postinst> `abort-remove' `in-favour' <package>
48 # <new-version>
49 # * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
50 # <failed-install-package> <version> `removing'
51 # <conflicting-package> <version>
52 # for details, see /usr/share/doc/packaging-manual/
54 # quoting from the policy:
55 # Any necessary prompting should almost always be confined to the
56 # post-installation script, and should be protected with a conditional
57 # so that unnecessary prompting doesn't happen if a package's
58 # installation fails and the `postinst' is called with `abort-upgrade',
59 # `abort-remove' or `abort-deconfigure'.
61 case "$1" in
62 configure)
63 if [ -f /etc/nbd-server -o -f /etc/nbd-server.oldconf ]
64 then
65 db_get nbd-server/convert
66 if [ "$RET" = "true" ]
67 then
68 convert_config;
69 exit 0;
72 db_get nbd-server/number
73 NUMBER=${RET:-0}
74 umask 066
75 if [ "$NUMBER" -gt 0 ]
76 then
77 TMPFILE=`mktemp /tmp/nbd-server.XXXXXX`
78 cat /usr/share/nbd-server/nbd-server.conf.tmpl > $TMPFILE
79 db_get nbd-server/useports
80 DOPORTS="$RET"
81 if [ "$DOPORTS" ]
82 then
83 echo " oldstyle = true" >> $TMPFILE
85 for i in $(seq 0 $(( $NUMBER - 1 )) )
87 # stay downward compatible
88 if [ "$i" -eq 0 ]
89 then
90 unset i
92 db_get nbd-server/port$i
93 PORT=$RET
94 db_get nbd-server/name$i
95 NAME=$RET
96 db_get nbd-server/filename$i
97 FN=$RET
98 ( echo "[$NAME]"
99 echo " exportname = $FN"
100 ) >> $TMPFILE
101 if [ $DOPORTS ]
102 then
103 echo " port = $PORT" >> $TMPFILE
105 done
106 ucf --debconf-ok $TMPFILE /etc/nbd-server/config
108 if ! id nbd >/dev/null 2>&1; then
109 adduser --system --group --home /etc/nbd-server --no-create-home nbd
113 abort-upgrade|abort-remove|abort-deconfigure)
118 echo "postinst called with unknown argument \`$1'">&2
119 exit 0
121 esac
123 db_stop
125 # dh_installdeb will replace this with shell code automatically
126 # generated by other debhelper scripts.
128 #DEBHELPER#
130 exit 0