1eb8dc403SDave Cobbley#!/bin/bash
2eb8dc403SDave Cobbley#
3eb8dc403SDave Cobbley# Copyright (c) 2005-2009 Wind River Systems, Inc.
4eb8dc403SDave Cobbley#
5c342db35SBrad Bishop# SPDX-License-Identifier: GPL-2.0-only
6eb8dc403SDave Cobbley#
7eb8dc403SDave Cobbley
8eb8dc403SDave Cobbleyusage() {
9eb8dc403SDave Cobbley	echo "Usage: $0 {start|stop|restart} <nfs-export-dir>"
10eb8dc403SDave Cobbley}
11eb8dc403SDave Cobbley
12eb8dc403SDave Cobbleyif [ $# != 2 ]; then
13eb8dc403SDave Cobbley	usage
14eb8dc403SDave Cobbley	exit 1
15eb8dc403SDave Cobbleyfi
16eb8dc403SDave Cobbley
17eb8dc403SDave Cobbleyif [[ "$1" != "start" && "$1" != "stop" && "$1" != "restart" ]]; then
18eb8dc403SDave Cobbley	echo "Unknown command '$1'"
19eb8dc403SDave Cobbley	usage
20eb8dc403SDave Cobbley	exit 1
21eb8dc403SDave Cobbleyfi
22eb8dc403SDave Cobbley
23eb8dc403SDave Cobbleyif [ ! -d "$2" ]; then
24eb8dc403SDave Cobbley	echo "Error: '$2' does not exist"
25eb8dc403SDave Cobbley	usage
26eb8dc403SDave Cobbley	exit 1
27eb8dc403SDave Cobbleyfi
28eb8dc403SDave Cobbley# Ensure the nfs-export-dir is an absolute path
29eb8dc403SDave CobbleyNFS_EXPORT_DIR=$(cd "$2" && pwd)
30eb8dc403SDave Cobbley
31eb8dc403SDave CobbleySYSROOT_SETUP_SCRIPT=`which oe-find-native-sysroot 2> /dev/null`
32eb8dc403SDave Cobbleyif [ -z "$SYSROOT_SETUP_SCRIPT" ]; then
33eb8dc403SDave Cobbley	echo "Error: Unable to find the oe-find-native-sysroot script"
34eb8dc403SDave Cobbley	echo "Did you forget to source your build environment setup script?"
35eb8dc403SDave Cobbley	exit 1
36eb8dc403SDave Cobbleyfi
37*517393d9SAndrew Geissler. $SYSROOT_SETUP_SCRIPT qemu-helper-native
38eb8dc403SDave Cobbley
39eb8dc403SDave Cobbleyif [ ! -e "$OECORE_NATIVE_SYSROOT/usr/bin/unfsd" ]; then
40eb8dc403SDave Cobbley	echo "Error: Unable to find unfsd binary in $OECORE_NATIVE_SYSROOT/usr/bin/"
41eb8dc403SDave Cobbley
42eb8dc403SDave Cobbley	echo "This shouldn't happen - something is missing from your toolchain installation"
43eb8dc403SDave Cobbley	exit 1
44eb8dc403SDave Cobbleyfi
45eb8dc403SDave Cobbley
46eb8dc403SDave Cobbleyif [ ! -d ~/.runqemu-sdk ]; then
47eb8dc403SDave Cobbley	mkdir -p ~/.runqemu-sdk
48eb8dc403SDave Cobbleyfi
49eb8dc403SDave Cobbley
50eb8dc403SDave CobbleyNFS_INSTANCE=${NFS_INSTANCE:=0}
51eb8dc403SDave CobbleyEXPORTS=~/.runqemu-sdk/exports$NFS_INSTANCE
52eb8dc403SDave CobbleyRMTAB=~/.runqemu-sdk/rmtab$NFS_INSTANCE
53eb8dc403SDave CobbleyNFSPID=~/.runqemu-sdk/nfs$NFS_INSTANCE.pid
54eb8dc403SDave CobbleyMOUNTPID=~/.runqemu-sdk/mount$NFS_INSTANCE.pid
55eb8dc403SDave Cobbley
56eb8dc403SDave CobbleyPSEUDO_OPTS="-P $OECORE_NATIVE_SYSROOT/usr"
57eb8dc403SDave CobbleyPSEUDO_LOCALSTATEDIR="$NFS_EXPORT_DIR/../$(basename $NFS_EXPORT_DIR).pseudo_state"
58eb8dc403SDave Cobbleyexport PSEUDO_LOCALSTATEDIR
59eb8dc403SDave Cobbley
60eb8dc403SDave Cobbleyif [ ! -d "$PSEUDO_LOCALSTATEDIR" ]; then
61eb8dc403SDave Cobbley	echo "Error: $PSEUDO_LOCALSTATEDIR does not exist."
62eb8dc403SDave Cobbley	echo "Did you create the export directory using runqemu-extract-sdk?"
63eb8dc403SDave Cobbley	exit 1
64eb8dc403SDave Cobbleyfi
65eb8dc403SDave Cobbley
66eb8dc403SDave Cobbley# NFS server port number
67eb8dc403SDave CobbleyNFSD_PORT=${NFSD_PORT:=$[ 3049 + 2 * $NFS_INSTANCE ]}
68eb8dc403SDave Cobbley# mountd port number
69eb8dc403SDave CobbleyMOUNTD_PORT=${MOUNTD_PORT:=$[ 3048 + 2 * $NFS_INSTANCE ]}
70eb8dc403SDave Cobbley
71eb8dc403SDave Cobbley## For debugging you would additionally add
72eb8dc403SDave Cobbley## --debug all
73*517393d9SAndrew GeisslerUNFSD_OPTS="-p -i $NFSPID -e $EXPORTS -n $NFSD_PORT -m $MOUNTD_PORT"
74eb8dc403SDave Cobbley
75eb8dc403SDave Cobbley# See how we were called.
76eb8dc403SDave Cobbleycase "$1" in
77eb8dc403SDave Cobbley  start)
78eb8dc403SDave Cobbley	echo "Creating exports file..."
79eb8dc403SDave Cobbley	echo "$NFS_EXPORT_DIR (rw,no_root_squash,no_all_squash,insecure)" > $EXPORTS
80eb8dc403SDave Cobbley
81eb8dc403SDave Cobbley	echo "Starting User Mode nfsd"
82eb8dc403SDave Cobbley	echo "  $PSEUDO $PSEUDO_OPTS $OECORE_NATIVE_SYSROOT/usr/bin/unfsd $UNFSD_OPTS"
83eb8dc403SDave Cobbley	$PSEUDO $PSEUDO_OPTS $OECORE_NATIVE_SYSROOT/usr/bin/unfsd $UNFSD_OPTS
84eb8dc403SDave Cobbley	if [ ! $? = 0 ]; then
85eb8dc403SDave Cobbley		echo "Error starting nfsd"
86eb8dc403SDave Cobbley		exit 1
87eb8dc403SDave Cobbley	fi
88eb8dc403SDave Cobbley	# Check to make sure everything started ok.
89eb8dc403SDave Cobbley	if [ ! -f $NFSPID ]; then
90eb8dc403SDave Cobbley		echo "rpc.nfsd did not start correctly"
91eb8dc403SDave Cobbley		exit 1
92eb8dc403SDave Cobbley	fi
93eb8dc403SDave Cobbley	ps -fp `cat $NFSPID` > /dev/null 2> /dev/null
94eb8dc403SDave Cobbley	if [ ! $? = 0 ]; then
95eb8dc403SDave Cobbley		echo "rpc.nfsd did not start correctly"
96eb8dc403SDave Cobbley		exit 1
97eb8dc403SDave Cobbley	fi
98eb8dc403SDave Cobbley	echo " "
99eb8dc403SDave Cobbley	echo "On your target please remember to add the following options for NFS"
100eb8dc403SDave Cobbley	echo "nfsroot=IP_ADDRESS:$NFS_EXPORT_DIR,nfsvers=3,port=$NFSD_PORT,udp,mountport=$MOUNTD_PORT"
101eb8dc403SDave Cobbley	;;
102eb8dc403SDave Cobbley  stop)
103eb8dc403SDave Cobbley	if [ -f "$NFSPID" ]; then
104eb8dc403SDave Cobbley		echo "Stopping rpc.nfsd"
105eb8dc403SDave Cobbley		kill `cat $NFSPID`
106eb8dc403SDave Cobbley		rm -f $NFSPID
107eb8dc403SDave Cobbley	else
108eb8dc403SDave Cobbley		echo "No PID file, not stopping rpc.nfsd"
109eb8dc403SDave Cobbley	fi
110eb8dc403SDave Cobbley	if [ -f "$EXPORTS" ]; then
111eb8dc403SDave Cobbley		echo "Removing exports file"
112eb8dc403SDave Cobbley		rm -f $EXPORTS
113eb8dc403SDave Cobbley	fi
114eb8dc403SDave Cobbley	;;
115eb8dc403SDave Cobbley  restart)
116eb8dc403SDave Cobbley	$0 stop $NFS_EXPORT_DIR
117eb8dc403SDave Cobbley	$0 start $NFS_EXPORT_DIR
118eb8dc403SDave Cobbley	if [ ! $? = 0 ]; then
119eb8dc403SDave Cobbley		exit 1
120eb8dc403SDave Cobbley	fi
121eb8dc403SDave Cobbley	;;
122eb8dc403SDave Cobbley  *)
123eb8dc403SDave Cobbley	echo "$0 {start|stop|restart} <nfs-export-dir>"
124eb8dc403SDave Cobbley	;;
125eb8dc403SDave Cobbleyesac
126eb8dc403SDave Cobbley
127eb8dc403SDave Cobbleyexit 0
128