9
0
Fork 0

blspec: Add NFS support

With this barebox can start root filesystems containing bootloader
spec entries via NFS. It is used as:

boot nfs://<ipaddr>[:<port]//path/to/nfsroot

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Sascha Hauer 2014-02-13 10:38:23 +01:00
parent a162dfe503
commit 86f681abec
1 changed files with 112 additions and 2 deletions

View File

@ -10,6 +10,8 @@
* GNU General Public License for more details.
*
*/
#define pr_fmt(fmt) "blspec: " fmt
#include <environment.h>
#include <globalvar.h>
#include <readkey.h>
@ -22,6 +24,7 @@
#include <libbb.h>
#include <init.h>
#include <boot.h>
#include <net.h>
#include <fs.h>
#include <of.h>
#include <linux/stat.h>
@ -130,6 +133,107 @@ static int blspec_have_entry(struct blspec *blspec, const char *path)
return 0;
}
/*
* nfs_find_mountpath - Check if a given url is already mounted
*/
static const char *nfs_find_mountpath(const char *nfshostpath)
{
struct fs_device_d *fsdev;
for_each_fs_device(fsdev) {
if (fsdev->backingstore && !strcmp(fsdev->backingstore, nfshostpath))
return fsdev->path;
}
return NULL;
}
/*
* parse_nfs_url - check for nfs:// style url
*
* Check if the passed string is a NFS url and if yes, mount the
* NFS and return the path we have mounted to.
*/
static char *parse_nfs_url(const char *url)
{
char *sep, *str, *host, *port, *path;
char *mountpath = NULL, *hostpath = NULL, *options = NULL;
const char *prevpath;
IPaddr_t ip;
int ret;
if (!IS_ENABLED(CONFIG_FS_NFS))
return ERR_PTR(-ENOSYS);
if (strncmp(url, "nfs://", 6))
return ERR_PTR(-EINVAL);
url += 6;
str = xstrdup(url);
host = str;
sep = strchr(str, '/');
if (!sep) {
ret = -EINVAL;
goto out;
}
*sep++ = 0;
path = sep;
port = strchr(host, ':');
if (port)
*port++ = 0;
ret = ifup_all(0);
if (ret) {
pr_err("Failed to bring up networking\n");
goto out;
}
ip = resolv(host);
if (ip == 0)
goto out;
hostpath = asprintf("%s:%s", ip_to_string(ip), path);
prevpath = nfs_find_mountpath(hostpath);
if (prevpath) {
mountpath = xstrdup(prevpath);
} else {
mountpath = asprintf("/mnt/nfs-%s-blspec-%08x", host, rand());
if (port)
options = asprintf("mountport=%s,port=%s", port, port);
ret = make_directory(mountpath);
if (ret)
goto out;
pr_debug("host: %s port: %s path: %s\n", host, port, path);
pr_debug("hostpath: %s mountpath: %s options: %s\n", hostpath, mountpath, options);
ret = mount(hostpath, "nfs", mountpath, options);
if (ret)
goto out;
}
ret = 0;
out:
free(str);
free(hostpath);
free(options);
if (ret)
free(mountpath);
return ret ? ERR_PTR(ret) : mountpath;
}
/*
* blspec_scan_directory - scan over a directory
*
@ -145,9 +249,13 @@ int blspec_scan_directory(struct blspec *blspec, const char *root)
char *abspath;
int ret, found = 0;
const char *dirname = "loader/entries";
char *entry_default = NULL, *entry_once = NULL, *name;
char *entry_default = NULL, *entry_once = NULL, *name, *nfspath = NULL;
pr_debug("%s: %s %s\n", __func__, root, dirname);
nfspath = parse_nfs_url(root);
if (!IS_ERR(nfspath))
root = nfspath;
pr_info("%s: %s %s\n", __func__, root, dirname);
entry_default = read_file_line("%s/default", root);
entry_once = read_file_line("%s/once", root);
@ -239,6 +347,8 @@ int blspec_scan_directory(struct blspec *blspec, const char *root)
closedir(dir);
err_out:
if (!IS_ERR(nfspath))
free(nfspath);
free(abspath);
free(entry_default);
free(entry_once);