Magellan Linux

Contents of /trunk/mkinitrd-magellan/klibc/usr/kinit/nfsroot.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (show annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 2499 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

1 #include <arpa/inet.h>
2 #include <sys/mount.h>
3 #include <sys/stat.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <assert.h>
8
9 #include "ipconfig.h"
10 #include "kinit.h"
11 #include "netdev.h"
12 #include "nfsmount.h"
13
14 static char *sub_client(__u32 client, char *path, size_t len)
15 {
16 struct in_addr addr = { client };
17 char buf[len];
18
19 if (strstr(path, "%s") != NULL) {
20 if (client == INADDR_NONE) {
21 fprintf(stderr, "Root-NFS: no client address\n");
22 exit(1);
23 }
24
25 snprintf(buf, len, path, inet_ntoa(addr));
26 strcpy(path, buf);
27 }
28
29 return path;
30 }
31
32 #define NFS_ARGC 6
33 #define MOUNT_POINT "/root"
34
35 int mount_nfs_root(int argc, char *argv[], int flags)
36 {
37 (void)flags; /* FIXME - don't ignore this */
38
39 struct in_addr addr = { INADDR_NONE };
40 __u32 client = INADDR_NONE;
41 const int len = 1024;
42 struct netdev *dev;
43 char *mtpt = MOUNT_POINT;
44 char *path = NULL;
45 char *dev_bootpath = NULL;
46 char root[len];
47 char *x, *opts;
48 int ret = 0;
49 int a = 1;
50 char *nfs_argv[NFS_ARGC + 1] = { "NFS-Mount" };
51
52 for (dev = ifaces; dev; dev = dev->next) {
53 if (dev->ip_server != INADDR_NONE &&
54 dev->ip_server != INADDR_ANY) {
55 addr.s_addr = dev->ip_server;
56 client = dev->ip_addr;
57 dev_bootpath = dev->bootpath;
58 break;
59 }
60 if (dev->ip_addr != INADDR_NONE && dev->ip_addr != INADDR_ANY) {
61 client = dev->ip_addr;
62 }
63 }
64
65 /*
66 * if the "nfsroot" option is set then it overrides
67 * bootpath supplied by the boot server.
68 */
69 if ((path = get_arg(argc, argv, "nfsroot=")) == NULL) {
70 if ((path = dev_bootpath) == NULL || path[0] == '\0')
71 /* no path - set a default */
72 path = (char *)"/tftpboot/%s";
73 } else if (dev_bootpath && dev_bootpath[0] != '\0')
74 fprintf(stderr,
75 "nfsroot=%s overrides boot server bootpath %s\n",
76 path, dev_bootpath);
77
78 if ((opts = strchr(path, ',')) != NULL) {
79 *opts++ = '\0';
80 nfs_argv[a++] = (char *)"-o";
81 nfs_argv[a++] = opts;
82 }
83
84 if ((x = strchr(path, ':')) == NULL) {
85 if (addr.s_addr == INADDR_NONE) {
86 fprintf(stderr, "Root-NFS: no server defined\n");
87 exit(1);
88 }
89
90 snprintf(root, len, "%s:%s", inet_ntoa(addr), path);
91 } else {
92 strcpy(root, path);
93 }
94
95 nfs_argv[a++] = sub_client(client, root, len);
96
97 DEBUG(("NFS-Root: mounting %s on %s with options \"%s\"\n",
98 nfs_argv[a-1], mtpt, opts ? opts : ""));
99
100 nfs_argv[a++] = mtpt;
101 nfs_argv[a] = NULL;
102 assert(a <= NFS_ARGC);
103
104 dump_args(a, nfs_argv);
105
106 if ((ret = nfsmount_main(a, nfs_argv)) != 0) {
107 ret = -1;
108 goto done;
109 }
110
111 done:
112 return ret;
113 }