1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/* trymount.c: try activating interfaces and mount a future root
Copyright (C) 2005, Erik van Konijnenburg
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <alloca.h>
#include <stdio.h>
#include <stdarg.h> /* for va_args */
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* for getopt */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <config.h>
#include "ipconfig/netdev.h"
/*
* ipconfig returns linked list of working devices here.
*/
extern int ipconfig_main (int argc, char *argv[]);
extern struct netdev *ifaces;
/* arguably, this should be in netdev.h */
static inline const char *my_inet_ntoa(uint32_t addr)
{
struct in_addr a;
a.s_addr = addr;
return inet_ntoa(a);
}
extern int nfsmount_main (int argc, char *argv[]);
extern int optint;
extern int opterr;
extern int optopt;
extern char * optarg;
static char * progname = "trynfs";
static char * version = VERSION;
static void
message (char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
fprintf (stderr, "%s: ", progname);
vfprintf (stderr, fmt, ap);
fprintf (stderr, "\n");
va_end (ap);
}
static void
fatal (char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
fprintf (stderr, "%s: ", progname);
vfprintf (stderr, fmt, ap);
fprintf (stderr, " (fatal)\n");
va_end (ap);
exit (1);
}
static void
usage (void)
{
printf ("\
%s [ -hv ] ip=... [ nfsroot=... ] mount-point \n\
Activate network interfaces, then do NFS mount.\n\
ip= and nfsroot= as in linux/Documentation/nfsroot.txt\n\
If DHCP provides a root path, nfsroot server:path is overridden,\n\
but NFS mount options still apply.\n\
If no NFS options are given, mount is attempted first\n\
without options, then with option 'udp', then with option 'v2'.\n\
\n\
-h: usage\n\
-v: version\n\
",
progname);
}
int
do_ipconfig (int argc, char *argv[]) {
int i, a = 0;
char **args = alloca ((argc + 1) * sizeof (char *));
if (! args) {
/* behaviour undefined on stack overflow,
* so the following may be useless ...
*/
fatal ("out of memory");
}
args[a++] = "IP-Config";
for (i = 1; i < argc; i++) {
if (strncmp ("ip=", argv[i], 3) == 0
|| strncmp ("nfsaddrs=", argv[i], 9) == 0)
{
args[a++] = argv[i];
}
}
args[a] = NULL;
return ipconfig_main (a, args);
}
int
do_nfsmount (char *nfsserver, char *nfspath, char *nfsopts, char *mountpoint)
{
int a = 0;
char * args[6];
char * fullpath = NULL;
fullpath = alloca (strlen(nfsserver) + strlen(nfspath) + 7);
if (!fullpath) {
fatal ("out of memory");
}
sprintf (fullpath, "%s:%s", nfsserver, nfspath);
args[a++] = "NFS-Mount";
if (nfsopts) {
args[a++] = "-o";
args[a++] = nfsopts;
}
args[a++] = fullpath;
args[a++] = mountpoint;
args[a] = NULL;
if (nfsopts) {
message ("starting nfsmount -o %s %s %s",
nfsopts, fullpath, mountpoint);
}
else {
message ("starting nfsmount %s %s", fullpath, mountpoint);
}
return nfsmount_main (a, args);
}
int
main (int argc, char *argv[])
{
int i;
char * nfsroot = NULL; /* from the nfsroot= argument */
char * mountpoint = NULL; /* mount here on client */
char * nfsserver = NULL; /* server as dotted decimal */
char buf[40];
char * nfspath = NULL; /* path on server to mount */
char * nfsopts = NULL; /* nfs options */
int rc; /* exist status */
while ((i = getopt (argc, argv, "hv")) >= 0) {
switch (i) {
case 'h':
usage ();
return 0;
case 'v':
message ("version %s", version);
return 0;
default:
fatal ("unknown argument, -h for help");
}
}
for (i = optind; i < argc; i++) {
if (strncmp (argv[i], "ip=", 3) == 0) {
continue;
}
else if (strncmp (argv[i], "nfsaddrs=", 9) == 0) {
continue;
}
else if (strncmp (argv[i], "nfsroot=", 8) == 0) {
if (nfsroot) {
fatal ("duplicate nfsroot= argument");
}
nfsroot = argv[i]+8;
}
else {
if (mountpoint) {
fatal ("too many arguments");
}
mountpoint = argv[i];
}
}
if (mountpoint == NULL) {
fatal ("no mointpoint specified");
}
if (mountpoint[0] != '/') {
fatal ("mointpoint is not an absolute path");
}
/*
* Get a working interface, plus any provided DHCP values.
*/
do_ipconfig (argc, argv);
if (ifaces == NULL) {
fatal ("no working network interfaces");
}
/*
* Good. We have a working interface, now to
* mount it.
* First do options and choose between nfsroot= and dhcp reply
*/
if (nfsroot) {
char *p = strchr (nfsroot, ',');
if (p) {
*p++ = '\0';
if (p) {
nfsopts = p;
}
}
}
if (strlen (ifaces->bootpath) > 0) {
strcpy (buf, my_inet_ntoa(ifaces->ip_server));
nfsserver = buf;
nfspath = ifaces->bootpath;
}
else if (nfsroot) {
char *p = strchr (nfsroot, ':');
if (p) {
*p++ = '\0';
nfsserver = nfsroot;
nfspath = p;
}
else {
strcpy (buf, my_inet_ntoa(ifaces->ip_server));
nfsserver = buf;
nfspath = nfsroot;
}
}
else {
fatal ("no nfsroot on command line or DHCP reply");
}
/*
* Now try mounting. If no options are given,
* we try some fallback options.
*/
if (nfsopts) {
rc = do_nfsmount (nfsserver, nfspath, nfsopts, mountpoint);
}
else {
rc = do_nfsmount (nfsserver, nfspath, NULL, mountpoint);
if (rc != 0) {
rc = do_nfsmount(nfsserver,nfspath,"udp",mountpoint);
}
if (rc != 0) {
rc = do_nfsmount(nfsserver,nfspath,"v2",mountpoint);
}
}
return rc;
}
|