#!/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 { unset gid # accept "-G gid" option while getopts "G:" opt { matchstr $(opt) { G { global gid := $(OPTARG)} } } shift $($OPTIND - 1) # prepare /etc/exports for i in [@Argv] { # fsid=0: needed for NFSv4 echo "$i *(rw,fsid=0,insecure,no_root_squash)" >> /etc/exports if test -v gid { chmod 070 $i chgrp $gid $i } # 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 # -V 3: enable NFSv3 /usr/sbin/rpc.mountd -N 2 -V 3 /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 /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: [ (C {(unset)} {(gid)}) (While cond: [(Sentence child:(C {(getopts)} {(DQ ("G:"))} {(opt)}) terminator:)] body: (DoGroup children: [ (Case to_match: {(${ VSub_Name opt)} arms: [ (case_arm pat_list: [{(G)}] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:gid) op: Equal rhs: {(${ VSub_Name OPTARG)} spids: [89] ) ] spids: [89] ) ] spids: [86 87 93 -1] ) ] spids: [77 83 96] ) ] spids: [74 99] ) ) (C {(shift)} { (ArithSubPart anode: (ArithBinary op_id: Arith_Minus left: (ArithWord w:{($ VSub_Name "$OPTIND")}) right: (ArithWord w:{(Lit_Digits 1)}) ) spids: [104 111] ) } ) (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: [144] ) ] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-v)} {(gid)} {(Lit_Other "]")}) terminator: ) ] action: [ (C {(chmod)} {(070)} {($ VSub_Name "$i")}) (C {(chgrp)} {($ VSub_Name "$gid")} {($ VSub_Name "$i")}) ] spids: [-1 161] ) ] spids: [-1 178] ) (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: [130 209] ) spids: [124 128] ) (Sentence child: (SimpleCommand words: [{(/usr/sbin/rpcinfo)} {(127.0.0.1)}] redirects: [(Redir op_id:Redir_Great fd:-1 arg_word:{(/dev/null)} spids:[222])] ) terminator: ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:s) op: Equal rhs: {($ VSub_QMark "$?")} spids: [227] ) ] spids: [227] ) (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 244] ) ] spids: [-1 259] ) (C {(mount)} {(-t)} {(nfsd)} {(nfds)} {(/proc/fs/nfsd)}) (C {(/usr/sbin/rpc.mountd)} {(-N)} {(2)} {(-V)} {(3)}) (C {(/usr/sbin/exportfs)} {(-r)}) (C {(/usr/sbin/rpc.nfsd)} {(-G)} {(10)} {(-N)} {(2)} {(-V)} {(3)}) (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: [364 370] ) } ) (C {(umount)} {(/proc/fs/nfsd)}) (SimpleCommand words: [{(echo)}] redirects: [(Redir op_id:Redir_Great fd:-1 arg_word:{(/etc/exports)} spids:[380])] ) (C {(exit)} {(0)}) ] spids: [335] ) spids: [329 334] ) (C {(trap)} {(stop)} {(TERM)}) (C {(start)} {(DQ ($ VSub_At "$@"))}) (While cond: [(Sentence child:(C {(true)}) terminator:)] body: (DoGroup children:[(C {(sleep)} {(5)})] spids:[415422]) ) ] )