bin/p9c-server (2585 bytes)
1 #!/bin/sh 2 # p9c-server: the storage node. Runs plan9port venti (archival block store) 3 # and fossil (live 9P file server on top of it) from files in $P9C_DIR. 4 # The first run formats the files. 9P listens on 127.0.0.1 only, with 5 # authentication off: other nodes reach it through SSH tunnels (p9c-tunnel), 6 # so SSH does the authentication and encryption. 7 # 8 # p9c-server # foreground; run it under a supervisor (runit) 9 # 10 # Environment: PLAN9 (default /etc/plan9), P9C_DIR (default ~/plan9), 11 # P9C_ARENAS (arena file size, default 4G), P9C_PORT (default 5640), 12 # P9C_FOSSIL (fossil binary; use one built with patches/fossil-remove-uaf.diff: 13 # stock plan9port fossil crashes when a client removes a file). 14 set -eu 15 PLAN9=${PLAN9:-/etc/plan9} 16 PATH=$PATH:$PLAN9/bin 17 export PLAN9 PATH 18 d=${P9C_DIR:-$HOME/plan9} 19 port=${P9C_PORT:-5640} 20 vaddr=tcp!127.0.0.1!17034 21 mkdir -p "$d" 22 cd "$d" 23 24 if [ ! -f venti.conf ]; then 25 # ponytail: one arena file and one index section; add more arena 26 # files (venti/fmtarenas) when this one fills up. 27 truncate -s "${P9C_ARENAS:-4G}" arenas0 28 truncate -s 512M isect0 29 truncate -s 64M bloom 30 cat >venti.conf <<EOF 31 index main 32 arenas $d/arenas0 33 isect $d/isect0 34 bloom $d/bloom 35 mem 16M 36 bcmem 32M 37 icmem 32M 38 addr $vaddr 39 httpaddr tcp!127.0.0.1!17035 40 EOF 41 "$PLAN9/bin/venti/fmtisect" isect0. isect0 42 "$PLAN9/bin/venti/fmtarenas" arenas0. arenas0 43 "$PLAN9/bin/venti/fmtbloom" bloom 44 "$PLAN9/bin/venti/fmtindex" venti.conf 45 fi 46 47 # A fossil left over from an earlier run holds the disk and the port. 48 pkill -f "fsys main config $d/fossil.disk" || true 49 "$PLAN9/bin/venti/venti" -s -c venti.conf & 50 venti=$! 51 # wait until venti answers before fossil needs it 52 i=0 53 until nc -z 127.0.0.1 17034 2>/dev/null || [ $i -ge 30 ]; do sleep 1; i=$((i+1)); done 54 55 if [ ! -f fossil.disk ]; then 56 truncate -s 2G fossil.disk 57 "$PLAN9/bin/fossil/flfmt" -y fossil.disk 58 fi 59 60 # fossil puts itself in the background. open -A turns authentication off. 61 # snaptime archives to venti daily at 04:00 and keeps a temporary snapshot 62 # every hour. /share is the writable tree for all nodes (unames differ per 63 # host, so world-writable); on later starts create fails harmlessly 64 # because it exists. 65 "${P9C_FOSSIL:-$PLAN9/bin/fossil/fossil}" -c "fsys main config $d/fossil.disk" \ 66 -c "fsys main venti $vaddr" \ 67 -c 'fsys main open -A -c 3000' \ 68 -c 'fsys main' \ 69 -c 'snaptime -a 0400 -s 60' \ 70 -c 'create /active/share adm adm d777' \ 71 -c "listen tcp!127.0.0.1!$port" 72 # The supervisor watches venti: when it dies this script exits, and the 73 # restart replaces the fossil too (pkill above). 74 wait $venti