blob: 6a8acd0d5ab61eaabfdb5708906d17aa76a0a6a6 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/bin/bash
2#
3# Copyright (c) 2005-2009 Wind River Systems, Inc.
4#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007
8usage() {
9 echo "Usage: $0 {start|stop|restart} <nfs-export-dir>"
10}
11
12if [ $# != 2 ]; then
13 usage
14 exit 1
15fi
16
17if [[ "$1" != "start" && "$1" != "stop" && "$1" != "restart" ]]; then
18 echo "Unknown command '$1'"
19 usage
20 exit 1
21fi
22
23if [ ! -d "$2" ]; then
24 echo "Error: '$2' does not exist"
25 usage
26 exit 1
27fi
28# Ensure the nfs-export-dir is an absolute path
29NFS_EXPORT_DIR=$(cd "$2" && pwd)
30
31SYSROOT_SETUP_SCRIPT=`which oe-find-native-sysroot 2> /dev/null`
32if [ -z "$SYSROOT_SETUP_SCRIPT" ]; then
33 echo "Error: Unable to find the oe-find-native-sysroot script"
34 echo "Did you forget to source your build environment setup script?"
35 exit 1
36fi
Andrew Geissler517393d2023-01-13 08:55:19 -060037. $SYSROOT_SETUP_SCRIPT qemu-helper-native
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038
Patrick Williamsf1e5d692016-03-30 15:21:19 -050039if [ ! -e "$OECORE_NATIVE_SYSROOT/usr/bin/unfsd" ]; then
40 echo "Error: Unable to find unfsd binary in $OECORE_NATIVE_SYSROOT/usr/bin/"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041
Andrew Geissler517393d2023-01-13 08:55:19 -060042 echo "This shouldn't happen - something is missing from your toolchain installation"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050043 exit 1
44fi
45
46if [ ! -d ~/.runqemu-sdk ]; then
47 mkdir -p ~/.runqemu-sdk
48fi
49
50NFS_INSTANCE=${NFS_INSTANCE:=0}
51EXPORTS=~/.runqemu-sdk/exports$NFS_INSTANCE
52RMTAB=~/.runqemu-sdk/rmtab$NFS_INSTANCE
53NFSPID=~/.runqemu-sdk/nfs$NFS_INSTANCE.pid
54MOUNTPID=~/.runqemu-sdk/mount$NFS_INSTANCE.pid
55
56PSEUDO_OPTS="-P $OECORE_NATIVE_SYSROOT/usr"
57PSEUDO_LOCALSTATEDIR="$NFS_EXPORT_DIR/../$(basename $NFS_EXPORT_DIR).pseudo_state"
58export PSEUDO_LOCALSTATEDIR
59
60if [ ! -d "$PSEUDO_LOCALSTATEDIR" ]; then
61 echo "Error: $PSEUDO_LOCALSTATEDIR does not exist."
62 echo "Did you create the export directory using runqemu-extract-sdk?"
63 exit 1
64fi
65
Brad Bishop37a0e4d2017-12-04 01:01:44 -050066# NFS server port number
67NFSD_PORT=${NFSD_PORT:=$[ 3049 + 2 * $NFS_INSTANCE ]}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068# mountd port number
Brad Bishop37a0e4d2017-12-04 01:01:44 -050069MOUNTD_PORT=${MOUNTD_PORT:=$[ 3048 + 2 * $NFS_INSTANCE ]}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070
71## For debugging you would additionally add
72## --debug all
Andrew Geissler517393d2023-01-13 08:55:19 -060073UNFSD_OPTS="-p -i $NFSPID -e $EXPORTS -n $NFSD_PORT -m $MOUNTD_PORT"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074
75# See how we were called.
76case "$1" in
77 start)
Brad Bishop37a0e4d2017-12-04 01:01:44 -050078 echo "Creating exports file..."
79 echo "$NFS_EXPORT_DIR (rw,no_root_squash,no_all_squash,insecure)" > $EXPORTS
80
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081 echo "Starting User Mode nfsd"
Patrick Williamsf1e5d692016-03-30 15:21:19 -050082 echo " $PSEUDO $PSEUDO_OPTS $OECORE_NATIVE_SYSROOT/usr/bin/unfsd $UNFSD_OPTS"
83 $PSEUDO $PSEUDO_OPTS $OECORE_NATIVE_SYSROOT/usr/bin/unfsd $UNFSD_OPTS
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084 if [ ! $? = 0 ]; then
85 echo "Error starting nfsd"
86 exit 1
87 fi
88 # Check to make sure everything started ok.
89 if [ ! -f $NFSPID ]; then
90 echo "rpc.nfsd did not start correctly"
91 exit 1
92 fi
93 ps -fp `cat $NFSPID` > /dev/null 2> /dev/null
94 if [ ! $? = 0 ]; then
95 echo "rpc.nfsd did not start correctly"
96 exit 1
97 fi
98 echo " "
99 echo "On your target please remember to add the following options for NFS"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500100 echo "nfsroot=IP_ADDRESS:$NFS_EXPORT_DIR,nfsvers=3,port=$NFSD_PORT,udp,mountport=$MOUNTD_PORT"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 ;;
102 stop)
103 if [ -f "$NFSPID" ]; then
104 echo "Stopping rpc.nfsd"
105 kill `cat $NFSPID`
106 rm -f $NFSPID
107 else
108 echo "No PID file, not stopping rpc.nfsd"
109 fi
110 if [ -f "$EXPORTS" ]; then
111 echo "Removing exports file"
112 rm -f $EXPORTS
113 fi
114 ;;
115 restart)
116 $0 stop $NFS_EXPORT_DIR
117 $0 start $NFS_EXPORT_DIR
118 if [ ! $? = 0 ]; then
119 exit 1
120 fi
121 ;;
122 *)
123 echo "$0 {start|stop|restart} <nfs-export-dir>"
124 ;;
125esac
126
127exit 0