#!/bin/bash # Copyright 2015 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. proc start { # prepare /etc/exports for i in [@Argv] { # fsid=0: needed for NFSv4 echo "$i *(rw,fsid=0,insecure,no_root_squash)" >> /etc/exports # move index.html to here /bin/cp /tmp/index.html $i/ chmod 644 $i/index.html echo "Serving $i" } # start rpcbind if it is not started yet /usr/sbin/rpcinfo 127.0.0.1 > /dev/null; global s := $Status if test $s -ne 0 { echo "Starting rpcbind" /usr/sbin/rpcbind -w } mount -t nfsd nfds /proc/fs/nfsd # -N 4.x: disable NFSv4 # -V 3: enable NFSv3 /usr/sbin/rpc.mountd -N 2 -V 3 -N 4 -N 4.1 /usr/sbin/exportfs -r # -G 10 to reduce grace time to 10 seconds (the lowest allowed) /usr/sbin/rpc.nfsd -G 10 -N 2 -V 3 -N 4 -N 4.1 2 /usr/sbin/rpc.statd --no-notify echo "NFS started" } proc stop { echo "Stopping NFS" /usr/sbin/rpc.nfsd 0 /usr/sbin/exportfs -au /usr/sbin/exportfs -f kill $[ pidof rpc.mountd] umount /proc/fs/nfsd echo > /etc/exports exit 0 } trap stop TERM start @Argv # Ugly hack to do nothing and wait for SIGTERM while true { sleep 5 } (CommandList children: [ (FuncDef name: start body: (BraceGroup children: [ (ForEach iter_name: i iter_words: [{(DQ ($ VSub_At "$@"))}] do_arg_iter: False body: (DoGroup children: [ (SimpleCommand words: [ {(echo)} {(DQ ($ VSub_Name "$i") (" *(rw,fsid=0,insecure,no_root_squash)"))} ] redirects: [ (Redir op_id: Redir_DGreat fd: -1 arg_word: {(/etc/exports)} spids: [83] ) ] ) (C {(/bin/cp)} {(/tmp/index.html)} {($ VSub_Name "$i") (/)}) (C {(chmod)} {(644)} {($ VSub_Name "$i") (/index.html)}) (C {(echo)} {(DQ ("Serving ") ($ VSub_Name "$i"))}) ] spids: [69 116] ) spids: [63 67] ) (Sentence child: (SimpleCommand words: [{(/usr/sbin/rpcinfo)} {(127.0.0.1)}] redirects: [(Redir op_id:Redir_Great fd:-1 arg_word:{(/dev/null)} spids:[129])] ) terminator: ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:s) op: Equal rhs: {($ VSub_QMark "$?")} spids: [134] ) ] spids: [134] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {($ VSub_Name "$s")} {(-ne)} {(0)} {(Lit_Other "]")}) terminator: ) ] action: [(C {(echo)} {(DQ ("Starting rpcbind"))}) (C {(/usr/sbin/rpcbind)} {(-w)})] spids: [-1 151] ) ] spids: [-1 166] ) (C {(mount)} {(-t)} {(nfsd)} {(nfds)} {(/proc/fs/nfsd)}) (C {(/usr/sbin/rpc.mountd)} {(-N)} {(2)} {(-V)} {(3)} {(-N)} {(4)} {(-N)} {(4.1)}) (C {(/usr/sbin/exportfs)} {(-r)}) (C {(/usr/sbin/rpc.nfsd)} {(-G)} {(10)} {(-N)} {(2)} {(-V)} {(3)} {(-N)} {(4)} {(-N)} {(4.1)} {(2)}) (C {(/usr/sbin/rpc.statd)} {(--no-notify)}) (C {(echo)} {(DQ ("NFS started"))}) ] spids: [50] ) spids: [44 49] ) (FuncDef name: stop body: (BraceGroup children: [ (C {(echo)} {(DQ ("Stopping NFS"))}) (C {(/usr/sbin/rpc.nfsd)} {(0)}) (C {(/usr/sbin/exportfs)} {(-au)}) (C {(/usr/sbin/exportfs)} {(-f)}) (C {(kill)} { (CommandSubPart command_list: (CommandList children:[(C {(pidof)} {(rpc.mountd)})]) left_token: spids: [293 299] ) } ) (C {(umount)} {(/proc/fs/nfsd)}) (SimpleCommand words: [{(echo)}] redirects: [(Redir op_id:Redir_Great fd:-1 arg_word:{(/etc/exports)} spids:[309])] ) (C {(exit)} {(0)}) ] spids: [264] ) spids: [258 263] ) (C {(trap)} {(stop)} {(TERM)}) (C {(start)} {(DQ ($ VSub_At "$@"))}) (While cond: [(Sentence child:(C {(true)}) terminator:)] body: (DoGroup children:[(C {(sleep)} {(5)})] spids:[344351]) ) ] )