Re: [Nbd] [w@...112...: Re: nbd-client started at boot (for root device) is not persistent]
- To: Wouter Verhelst <w@...112...>
- Cc: nbd-general@lists.sourceforge.net
- Subject: Re: [Nbd] [w@...112...: Re: nbd-client started at boot (for root device) is not persistent]
- From: Victor <vixtor@...17...>
- Date: Fri, 25 Nov 2016 13:23:04 +0200
- Message-id: <CAMOh2pLvgGXJ2YV4_W1bUo6VR3Mtemh8m-WYzNhhmQBt8QW3tw@...18...>
- In-reply-to: <20161125080636.f6dsb6227ksazolh@...3...>
- References: <20161125080636.f6dsb6227ksazolh@...3...>
I have found an idea that seems to work fine - at least for me: if the process cannot find /dev and /sys anymore, it means it was started from initramfs. So i recreate /sys and /dev and mount sysfs and devtmpfs again. The VFS tree of nbd-client will get a duplicate mount of /dev and /sys but i don't think there's any harm in that. What do you think (see attached patch).
--- nbd-3.13/nbd-client.c 2016-01-02 21:34:16.000000000 +0200
+++ nbd-3.13-modified/nbd-client.c 2016-11-25 13:22:11.884886541 +0200
@@ -43,6 +43,8 @@
#include <stdbool.h>
#include <time.h>
+#include <sys/stat.h>
+
#include <linux/ioctl.h>
#define MY_NAME "nbd_client"
#include "cliserv.h"
@@ -800,8 +802,32 @@
sleep (1);
}
nbd = open(nbddev, O_RDWR);
- if (nbd < 0)
- err("Cannot open NBD: %m");
+ if (nbd < 0) {
+ fprintf(stderr, "Maybe we were started from initramfs, trying to fix missing paths\n");
+ struct stat sb;
+ if (stat("/dev", &sb) == 0 && S_ISDIR(sb.st_mode))
+ err("/dev is still there, initramfs workaround will not help\n");
+ else {
+ fprintf(stderr, "/dev is not there anymore, we were in initramfs\n");
+ if(mkdir("/dev", 0755) < 0)
+ err("Cannot re-create /dev");
+ if(mount("udev", "/dev", "devtmpfs", MS_NOSUID|MS_RELATIME, "size=3772884k,nr_inodes=943221,mode=755") < 0)
+ err("Failed to mount udev");
+ }
+ if (stat("/sys", &sb) == 0 && S_ISDIR(sb.st_mode))
+ err("/sys is still there, initramfs workaround will not help\n");
+ else {
+ fprintf(stderr, "/sys is not there anymore, we were in initramfs\n");
+ if(mkdir("/sys", 0555) < 0)
+ err("Cannot re-create /sys");
+ if(mount("sysfs", "/sys", "sysfs", MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_RELATIME, "") < 0)
+ err("Failed to mount udev");
+ }
+
+ nbd = open(nbddev, O_RDWR);
+ if (nbd < 0)
+ err("Cannot open NBD: %m");
+ }
negotiate(sock, &new_size, &new_flags, name, needed_flags, cflags, opts);
if (size64 != new_size) {
err("Size of the device changed. Bye");
Reply to: