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