Release 2.9.23-4
[nbd.git] / debian / nbd-client.init.d
blob29e74489421d8b18edff5ab969bf1f13bb0ca92a
1 #! /bin/bash
2 # vim: ft=sh
4 # skeleton example file to build /etc/init.d/ scripts.
5 # This file should be used to construct scripts for /etc/init.d.
7 # Written by Miquel van Smoorenburg <miquels@cistron.nl>.
8 # Modified for Debian GNU/Linux
9 # by Ian Murdock <imurdock@gnu.ai.mit.edu>.
10 # Modified for the nbd-client package
11 # by Wouter Verhelst <wouter@debian.org>
13 ### BEGIN INIT INFO
14 # Provides: nbd-client
15 # Required-Start: $network $local_fs
16 # Required-Stop: $network
17 # Default-Start: S
18 # Default-Stop: 0 6
19 # X-Start-Before: mountnfs
20 # Short-Description: Network Block Device client
21 ### END INIT INFO
23 # Version: @(#)skeleton 1.8 03-Mar-1998 miquels@cistron.nl
25 PATH=/sbin:/bin:/usr/sbin:/usr/bin
26 DAEMON="/sbin/nbd-client"
27 NAME="nbd-client"
28 DESC="NBD client process"
30 test -f /etc/nbd-client && . /etc/nbd-client
32 test -x $DAEMON || exit 0
34 get_devices() {
35 DEVICES=
36 i=0
37 while [ ! -z "${NBD_TYPE[$i]}" ]
39 if [ "${NBD_TYPE[$i]}" == "$1" ]
40 then
41 DEVICES="$DEVICES ${NBD_DEVICE[$i]}"
43 i=$(($i + 1))
44 done
47 get_all_devices() {
48 if [ "$KILLALL" == "false" ]
49 then
50 DEVICES=${NBD_DEVICE[*]}
51 else
52 if [ -d /dev/nbd ]
53 then
54 DEVICES=/dev/nbd/*
55 else
56 DEVICES=/dev/nb*
61 get_swap_devices() {
62 if [ "$KILLALL" == "false" ]
63 then
64 get_devices s
65 else
66 if [ -f /proc/swaps ]
67 then
68 DEVICES=`grep '^/dev/nb' /proc/swaps | cut -d ' ' -f1`
69 else
70 get_all_devices
75 get_mount_devices() {
76 if [ "$KILLALL" == "false" ]
77 then
78 get_devices f
79 else
80 DEVICES=`mount | cut -d " " -f 1 | grep /dev/nbd`
85 case "$1" in
86 connect)
87 # I don't use start-stop-daemon: nbd-client only does some setup
88 # for the connection; the 'nbd-client' you see in your ps output
89 # later on is a kernel thread...
90 modprobe nbd
91 echo -n 'Connecting...'
92 i=0
93 while [ ! -z "${NBD_TYPE[$i]}" ]
95 # cfq deadlocks NBD devices, so switch to something else if cfq is
96 # selected by default
97 # This doesn't take into account non-udev devnames, but since
98 # there's really no other option these days...
99 if grep '\[cfq\]' "/sys/block/${NBD_DEVICE[$i]/\/dev\//}/queue/scheduler" >/dev/null; then
100 echo deadline > "/sys/block/${NBD_DEVICE[$i]/\/dev\//}/queue/scheduler"
102 if nbd-client -c "${NBD_DEVICE[$i]}" >/dev/null
103 then
104 echo "${NBD_DEVICE[$i]} already connected, skipping..."
105 else
106 if [ ! -z "${NBD_NAME[$i]}" ]
107 then
108 name="-N ${NBD_NAME[$i]}"
109 else
110 name=""
112 if $DAEMON "${NBD_HOST[$i]}" $name "${NBD_PORT[$i]}" "${NBD_DEVICE[$i]}" ${NBD_EXTRA[$i]}
113 then
114 echo "connected ${NBD_DEVICE[$i]}"
115 else
116 echo "could not connect ${NBD_DEVICE[$i]}"
119 i=$(($i + 1))
120 done
122 start)
123 echo -n "Starting $DESC: "
124 $0 connect
125 $0 activate
126 if [ ! -f /run/sendsigs.omit.d/nbd-client ]
127 then
128 for x in $(cat /proc/cmdline); do
129 case $x in
130 nbdroot=*,*,*)
131 nbdroot="${x#nbdroot=}"
132 nbdbasedev=$(echo "$nbdroot" | sed -e 's/^.*,//')
133 nbdrootdev=/dev/$nbdbasedev
135 root=/dev/nbd*)
136 nbdrootdev="${x#root=}"
138 esac
139 done
140 OMITKILL="$OMITKILL ${nbdrootdev%p*}"
141 for x in $OMITKILL
143 nbd-client -c $x >> /run/sendsigs.omit.d/nbd-client
144 done
147 activate)
148 echo 'Activating...'
150 while [ ! -z "${NBD_TYPE[$i]}" -a nbd-client -c "${NBD_DEVICE[$i]}" ]
152 case "${NBD_TYPE[$i]}" in
153 "s")
154 /sbin/mkswap "${NBD_DEVICE[$i]}"
155 /sbin/swapon "${NBD_DEVICE[$i]}"
156 echo "${NBD_DEVICE[$i]}: swap activated."
158 "f")
159 line=$(grep "${NBD_DEVICE[$i]}" /etc/fstab | grep "_netdev")
160 if [ -z "$line" ]
161 then
162 # sysvinit takes care of these.
163 spinner="-C"
164 case "$TERM" in
165 dumb|network|unknown|"") spinner="" ;;
166 esac
167 /sbin/fsck $spinner -a "${NBD_DEVICE[$i]}"
168 if [ $? -lt 2 ]
169 then
170 /bin/mount "${NBD_DEVICE[$i]}"
171 echo "${NBD_DEVICE[$i]}: filesystem mounted."
172 else
173 echo "fsck of ${NBD_DEVICE[$i]} failed. Not mounting."
177 "r")
178 # Nothing needs to be done
179 echo "${NBD_DEVICE[$i]}: raw selected. doing nothing."
182 echo "Error: NBD_TYPE[$i] contains unknown value ${NBD_TYPE[$i]}"
184 esac
185 i=$(( $i + 1 ))
186 done
187 echo "$NAME."
189 stop)
190 echo "Stopping $DESC: "
191 get_mount_devices
192 for dev in $DEVICES
194 # Ignore devices with _netdev option (sysvinit takes care of those)
195 line=$(grep $dev /etc/fstab | grep "_netdev")
196 if [ -z "$line" ]
197 then
198 echo "umounting filesystem from $dev..."
199 umount $dev 2>/dev/null
200 if [ $? -eq 1 ]
201 then
202 echo -n "umount of $dev failed! Data loss may occur! will continue in 10 seconds..."
203 sleep 1
204 for i in 9 8 7 6 5 4 3 2 1
206 echo -n $i" "
207 sleep 1
208 done
209 echo "ok, going on..."
212 echo $dev
213 done
214 get_swap_devices
215 if [ "$DEVICES" ]
216 then
217 echo "Invoking swapoff on $DEVICES..."
218 swapoff $DEVICES 2>/dev/null
220 get_all_devices
221 for i in $DEVICES
223 echo "Disconnecting $i"
224 $DAEMON -d $i 2>/dev/null >/dev/null
225 done
226 rmmod nbd
227 echo "$NAME."
229 restart|force-reload)
230 if dpkg --compare-versions "$(uname -r)" gt "2.4"
231 then
232 $0 stop
233 sleep 10
234 $0 start
235 else
236 echo "Need 2.4-kernel for disconnect. As such, restart won't work"
237 echo "Either upgrade to a 2.4-kernel, or use $0 stop, restart the server,"
238 echo "and do $0 start."
242 N=/etc/init.d/$NAME
243 echo "Usage: $N {start|connect|stop|restart|force-reload|activate}" >&2
244 exit 1
246 esac
248 exit 0