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).

On Fri, Nov 25, 2016 at 10:06 AM, Wouter Verhelst <[email protected]> wrote:

> Only after I'd sent this did I notice the Cc was gone...
>
> ----- Forwarded message from Wouter Verhelst <[email protected]> -----
>
> Date: Fri, 25 Nov 2016 09:04:32 +0100
> From: Wouter Verhelst <[email protected]>
> To: Victor <[email protected]>
> Subject: Re: [Nbd] nbd-client started at boot (for root device) is not
> persistent
> Message-ID: <[email protected]>
> In-Reply-To: <CAMOh2p+2SFjck=z2C5ynyT0SseKPPJQ7z+RGb2SbocFP65vn-Q@mail.
> gmail.com>
> Organization: none
> User-Agent: NeoMutt/20161104 (1.7.1)
>
> On Fri, Nov 25, 2016 at 09:00:20AM +0200, Victor wrote:
> > I am wondering - is there any way for a process to re-read the VFS after
> /dev
> > and /sys were moved to /root/dev and /root/sys and then root was moved
> with
> > MS_MOVE to /, and finally a chroot was performed and init started?
> >
> > If not, what would you think about the following: create a new run flag
> for
> > nbd-client (let's call it --replace). If this command line flag is used,
> > nbd-client first kills an existing nbd-client providing the same
> resource then
> > starts normally.
>
> Can't do that, because then the shutdown protection that systemd provides
> is
> gone (that's only valid for processes which have been started from
> initramfs).
>
> --
> < ron> I mean, the main *practical* problem with C++, is there's like a
> dozen
>        people in the world who think they really understand all of its
> rules,
>        and pretty much all of them are just lying to themselves too.
>  -- #debian-devel, OFTC, 2016-02-12
>
> ----- End forwarded message -----
>
> --
> < ron> I mean, the main *practical* problem with C++, is there's like a
> dozen
>        people in the world who think they really understand all of its
> rules,
>        and pretty much all of them are just lying to themselves too.
>  -- #debian-devel, OFTC, 2016-02-12
>
> ------------------------------------------------------------
> ------------------
> _______________________________________________
> Nbd-general mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/nbd-general
>
--- 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");
------------------------------------------------------------------------------
_______________________________________________
Nbd-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nbd-general

Reply via email to