| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/bash | 
 | 2 | # | 
 | 3 | # Copyright (c) 2005-2009 Wind River Systems, Inc. | 
 | 4 | # | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 5 | # SPDX-License-Identifier: GPL-2.0-only | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | # | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 |  | 
 | 8 | usage() { | 
 | 9 | 	echo "Usage: $0 {start|stop|restart} <nfs-export-dir>" | 
 | 10 | } | 
 | 11 |  | 
 | 12 | if [ $# != 2 ]; then | 
 | 13 | 	usage | 
 | 14 | 	exit 1 | 
 | 15 | fi | 
 | 16 |  | 
 | 17 | if [[ "$1" != "start" && "$1" != "stop" && "$1" != "restart" ]]; then | 
 | 18 | 	echo "Unknown command '$1'" | 
 | 19 | 	usage | 
 | 20 | 	exit 1 | 
 | 21 | fi | 
 | 22 |  | 
 | 23 | if [ ! -d "$2" ]; then | 
 | 24 | 	echo "Error: '$2' does not exist" | 
 | 25 | 	usage | 
 | 26 | 	exit 1 | 
 | 27 | fi | 
 | 28 | # Ensure the nfs-export-dir is an absolute path | 
 | 29 | NFS_EXPORT_DIR=$(cd "$2" && pwd) | 
 | 30 |  | 
 | 31 | SYSROOT_SETUP_SCRIPT=`which oe-find-native-sysroot 2> /dev/null` | 
 | 32 | if [ -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 | 
 | 36 | fi | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 37 | . $SYSROOT_SETUP_SCRIPT meta-ide-support | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 |  | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 39 | if [ ! -e "$OECORE_NATIVE_SYSROOT/usr/bin/unfsd" ]; then | 
 | 40 | 	echo "Error: Unable to find unfsd binary in $OECORE_NATIVE_SYSROOT/usr/bin/" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 |  | 
 | 42 | 	if [ "x$OECORE_DISTRO_VERSION" = "x" ]; then | 
 | 43 | 		echo "Have you run 'bitbake meta-ide-support'?" | 
 | 44 | 	else | 
 | 45 | 		echo "This shouldn't happen - something is missing from your toolchain installation" | 
 | 46 | 	fi | 
 | 47 | 	exit 1 | 
 | 48 | fi | 
 | 49 |  | 
 | 50 | if [ ! -d ~/.runqemu-sdk ]; then | 
 | 51 | 	mkdir -p ~/.runqemu-sdk | 
 | 52 | fi | 
 | 53 |  | 
 | 54 | NFS_INSTANCE=${NFS_INSTANCE:=0} | 
 | 55 | EXPORTS=~/.runqemu-sdk/exports$NFS_INSTANCE | 
 | 56 | RMTAB=~/.runqemu-sdk/rmtab$NFS_INSTANCE | 
 | 57 | NFSPID=~/.runqemu-sdk/nfs$NFS_INSTANCE.pid | 
 | 58 | MOUNTPID=~/.runqemu-sdk/mount$NFS_INSTANCE.pid | 
 | 59 |  | 
 | 60 | PSEUDO_OPTS="-P $OECORE_NATIVE_SYSROOT/usr" | 
 | 61 | PSEUDO_LOCALSTATEDIR="$NFS_EXPORT_DIR/../$(basename $NFS_EXPORT_DIR).pseudo_state" | 
 | 62 | export PSEUDO_LOCALSTATEDIR | 
 | 63 |  | 
 | 64 | if [ ! -d "$PSEUDO_LOCALSTATEDIR" ]; then | 
 | 65 | 	echo "Error: $PSEUDO_LOCALSTATEDIR does not exist." | 
 | 66 | 	echo "Did you create the export directory using runqemu-extract-sdk?" | 
 | 67 | 	exit 1	 | 
 | 68 | fi | 
 | 69 |  | 
| Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 70 | # NFS server port number | 
 | 71 | NFSD_PORT=${NFSD_PORT:=$[ 3049 + 2 * $NFS_INSTANCE ]} | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 72 | # mountd port number | 
| Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 73 | MOUNTD_PORT=${MOUNTD_PORT:=$[ 3048 + 2 * $NFS_INSTANCE ]} | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 |  | 
 | 75 | ## For debugging you would additionally add | 
 | 76 | ## --debug all | 
| Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 77 | UNFSD_OPTS="-p -N -i $NFSPID -e $EXPORTS -n $NFSD_PORT -m $MOUNTD_PORT" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 |  | 
 | 79 | # See how we were called. | 
 | 80 | case "$1" in | 
 | 81 |   start) | 
 | 82 | 	PORTMAP_RUNNING=`ps -ef | grep portmap | grep -v grep` | 
 | 83 | 	RPCBIND_RUNNING=`ps -ef | grep rpcbind | grep -v grep` | 
 | 84 | 	if [[ "x$PORTMAP_RUNNING" = "x" && "x$RPCBIND_RUNNING" = "x" ]]; then | 
 | 85 | 		echo "=======================================================" | 
 | 86 | 		echo "Error: neither rpcbind nor portmap appear to be running" | 
 | 87 | 		echo "Please install and start one of these services first" | 
 | 88 | 		echo "=======================================================" | 
 | 89 | 		echo "Tip: for recent Ubuntu hosts, run:" | 
 | 90 | 		echo "  sudo apt-get install rpcbind" | 
 | 91 | 		echo "Then add OPTIONS=\"-i -w\"  to /etc/default/rpcbind and run" | 
 | 92 | 		echo "  sudo service portmap restart" | 
 | 93 |  | 
 | 94 | 		exit 1 | 
 | 95 | 	fi | 
 | 96 |  | 
| Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 97 | 	echo "Creating exports file..." | 
 | 98 | 	echo "$NFS_EXPORT_DIR (rw,no_root_squash,no_all_squash,insecure)" > $EXPORTS | 
 | 99 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | 	echo "Starting User Mode nfsd" | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 101 | 	echo "  $PSEUDO $PSEUDO_OPTS $OECORE_NATIVE_SYSROOT/usr/bin/unfsd $UNFSD_OPTS" | 
 | 102 | 	$PSEUDO $PSEUDO_OPTS $OECORE_NATIVE_SYSROOT/usr/bin/unfsd $UNFSD_OPTS | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | 	if [ ! $? = 0 ]; then | 
 | 104 | 		echo "Error starting nfsd" | 
 | 105 | 		exit 1 | 
 | 106 | 	fi | 
 | 107 | 	# Check to make sure everything started ok. | 
 | 108 | 	if [ ! -f $NFSPID ]; then | 
 | 109 | 		echo "rpc.nfsd did not start correctly" | 
 | 110 | 		exit 1 | 
 | 111 | 	fi | 
 | 112 | 	ps -fp `cat $NFSPID` > /dev/null 2> /dev/null | 
 | 113 | 	if [ ! $? = 0 ]; then | 
 | 114 | 		echo "rpc.nfsd did not start correctly" | 
 | 115 | 		exit 1 | 
 | 116 | 	fi | 
 | 117 | 	echo " " | 
 | 118 | 	echo "On your target please remember to add the following options for NFS" | 
| Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 119 | 	echo "nfsroot=IP_ADDRESS:$NFS_EXPORT_DIR,nfsvers=3,port=$NFSD_PORT,udp,mountport=$MOUNTD_PORT" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 120 | 	;; | 
 | 121 |   stop) | 
 | 122 | 	if [ -f "$NFSPID" ]; then | 
 | 123 | 		echo "Stopping rpc.nfsd" | 
 | 124 | 		kill `cat $NFSPID` | 
 | 125 | 		rm -f $NFSPID | 
 | 126 | 	else | 
 | 127 | 		echo "No PID file, not stopping rpc.nfsd" | 
 | 128 | 	fi | 
 | 129 | 	if [ -f "$EXPORTS" ]; then | 
 | 130 | 		echo "Removing exports file" | 
 | 131 | 		rm -f $EXPORTS | 
 | 132 | 	fi | 
 | 133 | 	;; | 
 | 134 |   restart) | 
 | 135 | 	$0 stop $NFS_EXPORT_DIR | 
 | 136 | 	$0 start $NFS_EXPORT_DIR  | 
 | 137 | 	if [ ! $? = 0 ]; then | 
 | 138 | 		exit 1 | 
 | 139 | 	fi | 
 | 140 | 	;; | 
 | 141 |   *) | 
 | 142 | 	echo "$0 {start|stop|restart} <nfs-export-dir>" | 
 | 143 | 	;; | 
 | 144 | esac | 
 | 145 |  | 
 | 146 | exit 0 |