<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Aug 14, 2014 at 1:01 PM, Pavel Emelyanov <span dir="ltr"><<a href="mailto:xemul@parallels.com" target="_blank">xemul@parallels.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">On 08/14/2014 11:22 PM, Saied Kazemi wrote:<br>
<div><div class="h5">><br>
><br>
><br>
> On Wed, Aug 13, 2014 at 1:13 AM, Pavel Emelyanov <<a href="mailto:xemul@parallels.com">xemul@parallels.com</a> <mailto:<a href="mailto:xemul@parallels.com">xemul@parallels.com</a>>> wrote:<br>
><br>
> On 08/13/2014 04:29 AM, Saied Kazemi wrote:<br>
><br>
> Hi, Saied.<br>
><br>
> > Here is a new patch. I rebased to the head (commit ded04267f8) and cleaned up the code a<br>
> > bit more. Please use the attached patch instead of the one I sent yesterday.<br>
> ><br>
> > Note that I had to delete 3 lines from cgroup.c for properties that I don't have on my<br>
> > Ubuntu 14.04. Also, please note that you have to add --manage-cgroups to criu command<br>
> > line for both dump and restore.<br>
><br>
> Thank you for looking into this. I'm not familiar with AUFS at all, thus I<br>
> only have comments about the way AUFS parsing/fixing code is integrated into<br>
> the rest of the criu code.<br>
><br>
><br>
> I am not familiar with AUFS internals either. I just looked at how it's set up and what information it passes through /proc.<br>
><br>
><br>
><br>
><br>
> Please, see my comments inline.<br>
><br>
> > @@ -197,6 +199,19 @@ int fill_fdlink(int lfd, const struct fd_parms *p, struct fd_link *link)<br>
> > return -1;<br>
> > }<br>
> ><br>
> > + /*<br>
> > + * For AUFS support, we need to replace absolute<br>
> > + * branch pathnames with relative pathnames from root.<br>
> > + */<br>
> > + if (opts.aufs) {<br>
><br>
> At this point we have the statfs() information. Can we rely on the<br>
> fs_type being aufs magic to check that the file is aufs one?<br>
><br>
><br>
> No, because depending on how we end up in fill_fdlink(), p->fstype may be NULL.<br>
> Besides, even when set, its fstyp isn't aufs. It'd be something like ext4 because<br>
> the branch is in ext4.<br>
<br>
</div></div>Damn :( So it's not a "real" filesystem, it just redirects all calls to some<br>
other, underlying one, right? And if I opened a file which is on AUFS mount,<br>
I will end up in some other FS.<br></blockquote><div><br></div><div>Yes, it's not a "real" filesystem like ext4 but this can actually be a feature because you can stack different layers from different filesystems to present a consolidated fs view to the user.</div>
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
BTW, can we re-use the mnt_id for that? If I call open() on some path that<br>
is on AUFS mount point, would the mnt_id of a file be the AUFS's one?<br></blockquote><div><br></div><div>No, we cannot use mnt_id because it's uninitialized (-1). Even if it were initialized, I am not sure mnt_id alone would suffice to identify AUFS pathnames that should be replaced.</div>
<div><br></div><div>Also, please note that we're handling the files that the process is executing (e.g., /bin/busybox, /lib/<a href="http://libuClibc-0.9.33.2.so">libuClibc-0.9.33.2.so</a>, /lib/<a href="http://ld64-uClibc-0.9.33.2.so">ld64-uClibc-0.9.33.2.so</a>), not the files that the process has opened. When we get to fill_fdlink() for these files, mnt_id is -1.</div>
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div><div class="h5"><br>
> > + int n = sizeof link->name - 1;<br>
> > + n = fixup_aufs_path(&link->name[1], n, true);<br>
> > + if (n < 0)<br>
> > + return -1;<br>
> > + if (n > 0)<br>
> > + len = n;<br>
> > + }<br>
> > +<br>
> > link->len = len + 1;<br>
> > return 0;<br>
><br>
> > @@ -450,12 +453,24 @@ int parse_smaps(pid_t pid, struct vm_area_list *vma_area_list, bool use_map_file<br>
> > vma_area->st = prev->st;<br>
> > } else if (vma_area->vm_file_fd >= 0) {<br>
> > struct stat *st_buf;<br>
> > + char *f;<br>
> ><br>
> > st_buf = vma_area->st = xmalloc(sizeof(*st_buf));<br>
> > if (!st_buf)<br>
> > goto err;<br>
> ><br>
> > - if (fstat(vma_area->vm_file_fd, st_buf) < 0) {<br>
> > + /*<br>
> > + * For AUFS support, we cannot fstat() file a descriptor that<br>
> > + * is a symbolic link to a branch. Instead, we obtain the<br>
> > + * pathname of the file from the root and use stat().<br>
> > + */<br>
> > + if (opts.aufs && (f = fixup_aufs_fd_path(vma_area->vm_file_fd))) {<br>
><br>
> Would this work if the vm_file_fd sits in another mount namespace? Or<br>
> the path reported by fixup_aufs_fd_path() is always in the criu's one?<br>
><br>
><br>
> I think it should work, although I don't know how to force it in a different mount namespace in<br>
> the tests that I am running. Can you whip up an example?<br>
<br>
</div></div>If you start a Docker container, all tasks in it will automatically happen<br>
in a mount namespace. You can check this by comparing where the /proc/pid/ns/mnt<br>
links point for a task in container and for e.g. your shell outside of it.<br></blockquote><div><br></div><div>Yes, when you do "docker run -i busybox:latest /bin/sh -i", /bin/sh comes from the container's mount namespace. I am not sure how to get /bin/sh from a mount namespace other than the container's.</div>
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class=""><br>
> And another question -- do we need the fixed stat buffer that early?<br>
><br>
><br>
> I am doing this exactly at the same time that CRIU is doing it (i.e., the fstat() is original code).<br>
> I'm just using stat() instead of fstat(), so effectively it's the same logic/time as before.<br>
<br>
</div>OK. BTW, you say, that /proc/pid/map_files/ links point to "branches" paths.<br>
How about the /proc/pid/fd/ ones? Do they show paths from branches too?<br></blockquote><div><br></div><div>No, symlinks in /proc/pid/fd/ point to files relative to the root.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class=""><br>
> The vma fd is dumped in dump_filemap() and in that place we call<br>
> get_fd_mntid() for missing mount ID. Can we fixup the device there too?<br>
><br>
><br>
> The way I handle it is like this: If the link file descriptor points to an AUFS branch name,<br>
> we replace it with a pathname from root. Here is the call sequence from dump_filemap():<br>
><br>
> dump_filemap()<br>
> dump_one_reg_file(lfd)<br>
> fill_fdlink(lfd)<br>
> read_fd_link(lfd)<br>
> readlink(lfd) // returns path in branch<br>
> fixup_aufs_brnach() // replaces path in branch with path from root<br>
> check_path_remap()<br>
><br>
> In other words, when we see a pathname in a branch, we replace it with a pathname from root<br>
> as if we never saw the branch pathname. When using a link file descriptor in fstat(), because<br>
> the kernel returns the stat info of the pathname in branch, we use stat() with a pathname<br>
> from the root instead of fstat(). If we didn't do this, we'd get different device/inode<br>
> values and CRIU fails with an error message like "Unaccessible path opened 33:23, need 2049:53764".<br>
<br>
</div>So if you stat() an fd opened on AUFS you get dev:inode pair from AUFS, not from the<br>
underlying ext4?<br></blockquote><div><br></div><div>Exactly. That's why we need to always stat the file either from the AUFS root or from the branch where it really lives. We cannot stat it from one path and expect to see the same dev:ino from a different path, although it's really the same file!</div>
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class=""><br>
> > + if (stat(f, st_buf) < 0) {<br>
> > + pr_perror("Failed fstat on %d's map %lu (%s)",<br>
> > + pid, start, f);<br>
> > + goto err;<br>
> > + }<br>
> > + } else if (fstat(vma_area->vm_file_fd, st_buf) < 0) {<br>
> > pr_perror("Failed fstat on %d's map %lu", pid, start);<br>
> > goto err;<br>
> > }<br>
><br>
> > @@ -921,6 +942,16 @@ static int parse_mountinfo_ent(char *str, struct mount_info *new)<br>
> > if (ret != 3)<br>
> > return -1;<br>
> ><br>
> > + /* see comments in sysfs_parse.c */<br>
> > + if (opts.aufs && !strcmp(new->mountpoint, "./")) {<br>
> > + if (strcmp(fstype, "aufs")) {<br>
><br>
> Can we reuse the fstype->parse() callback for this? This one gets called<br>
> in parse_mountinfo() in a loop after parse_mountinfo_ent().<br>
><br>
><br>
> Yes, we can and I changed the code accordingly (added aufs to fstypes[]). Please use the new patch.<br>
<br>
</div>OK.<br>
<div><div class="h5"><br>
> > + pr_err("Expected fstype aufs got %s\n", fstype);<br>
> > + return -1;<br>
> > + }<br>
> > + if (fixup_src_opt(&new->source, &opt) < 0)<br>
> > + return -1;<br>
> > + }<br>
> > +<br>
> > ret = -1;<br>
> > new->fstype = find_fstype_by_name(fstype);<br>
> ><br>
><br>
> > +/*<br>
> > + * Copy the line in process's mountinfo file that corresponds<br>
> > + * to the mountpoint specified by the mntpoint argument. Return<br>
> > + * the number of characters parsed in the line, or -1 on error.<br>
> > + */<br>
> > +int get_mountinfo_by_mountpoint(pid_t pid, char *mntpoint, char *line, int linelen)<br>
><br>
> I do not fully understand why this mountpoint->something-else parser<br>
> is required. Can we put all the code parsing aufs-specific stuff into<br>
> the fstype->parse() callback I mentioned above? When criu starts<br>
> parsing mount namespaces (it does this before doing anything else)<br>
> this info will be parsed and stored into mount_info->private. Later,<br>
> when we need to e.g. fixup file paths we can get file->mnt_id, get<br>
> mount_info by it, then check the fs being aufs and fix the path<br>
> according to the mount_info->private data we have. Would this work?<br>
><br>
><br>
> I am now using the parse callback that you suggested but we need the "reference" values<br>
> *before* parse_mountinfo() is called. Otherwise, when we call the parse callback, we<br>
> won't know what values to replace with.<br>
<br>
</div></div>Ah, I see (I think). So by the time you get to parsing the AUFS's entry,<br>
you need to know the mount infos from other mountpoints which may not yet<br>
be parsed, am I right with that?<br></blockquote><div><br></div><div>You're right. That's why we call parse_aufs_reference() in collect_mntinfo() before calling parse_mountinfo().</div><div><br></div><div> </div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class=""><br>
> That said, I noticed that with my latest rebase there is apparently no longer a need to<br>
> specify --aufs-ref. This is great as we can get rid of the code fixing up the mountinfo<br>
> root entry (note that we'd still need to replace / with the --aufs-root).<br>
><br>
> Since I am not familiar with the internals of CRIU as to how it builds its bind mount<br>
> list, could you please review this part of the code? As mentioned before, it used to<br>
> be that without --aufs-ref, mounts_equal() would return false, in my test cases, for<br>
> /etc/hosts, /etc/hostname, and /etc/resolv.conf because their dev, fstype, source, and<br>
> options were different.<br>
<br>
</div>So, the /etc/hosts is a mountpoint, whose root (the original file) is somewhere<br>
outside of the container, right? And the /etc/hosts file itself (i.e. -- before<br>
someone bind-mounts the external file on it) resides on AUFS, am I correct?<br></blockquote><div><br></div><div>Yes to both questions. You can see where /etc/hosts lives from the --ext-mount-map option.</div><div><br></div>
<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class=""><br>
> As a result, these files would not be added to the mnt_bind<br>
> list in collect_shared() and restore would fail with the error message "A few mount<br>
> points can't be mounted." I am puzzled that restore is now successful without --aufs-ref.<br>
> I need to look into this deeper myself but would really appreciate your thoughts and feedback.<br>
<br>
</div>I'll try :) Can you shed more light on the comment where you say that you<br>
replace device numbers, sources and options. AFAIU once you have AUFS's<br>
device, source and opt, you replace it with the underlying "branch"'s<br>
device, source and opt, do I get it correctly?<br></blockquote><div><br></div><div>When I investigated the cause of "A few mount points can't be mounted" during restore, I noticed that external bind mounts were not added to the mnt_bind list in collect_shared() because mounts_equal() was never true for these files when their mountinfo entry was compared with the root mountinfo entry.</div>
<div><br></div><div><div><div> /* Search bind-mounts */</div><div> if (list_empty(&m->mnt_bind)) {</div><div> /*</div><div> * A first mounted point will be set up as a source point</div>
<div> * for others. Look at propagate_mount()</div><div> */</div><div> for (t = m->next; t; t = t->next) {</div><div> if (mounts_equal(m, t, true))</div>
<div> list_add(&t->mnt_bind, &m->mnt_bind);</div><div> }</div><div> }</div></div></div><div><br></div><div><div>So, I "fixed up" the root mountinfo entry to have the same values as external bind mounts, effectively forcing a match in mounts_equal(), and restore worked.</div>
<div><div></div></div></div><div><br></div><div>That said, this is not a good solution at all as it only works when all external bind mounts come from the same device and fiilesystem. But you can potentially have multiple external bind mounts each coming from a different device and filesystem, with different options. I need your help in addressing this issue.</div>
<div><br></div><div>Finally, to complicate (or simplify?) things, after my latest rebase, I noticed that I don't have to fix up the root mountinfo entry anymore to get the external bind mounts mounted!! Somehow, all external bind mounts make it to the mnt_bind list. I do not know how to explain this because obviously mounts_equal() returns false if any of devices, filesystems, sources, or options are different.</div>
<div><br></div><div><div><div> if (mi->s_dev != c->s_dev ||</div><div> c->fstype != mi->fstype ||</div><div> strcmp(c->source, mi->source) ||</div><div> strcmp(c->options, mi->options))</div>
<div> return false;</div></div></div><div><br></div><div>Could it be related to some of the recent mount-related commits? For example, "mnt: Don't delay external mount points" 8b019e0bb4?</div>
<div><br></div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
I got the idea with paths fixups, now I'm trying to understand what's going<br>
on with device numbers and others :)<br>
<div><div class="h5"><br>
><br>
><br>
> > +{<br>
> > + char buf[PATH_MAX];<br>
> > + int n, ret;<br>
> > + FILE *f;<br>
><br>
> > +<br>
> > + snprintf(buf, sizeof buf, "/proc/%d/mountinfo", pid);<br>
> > + f = fopen(buf, "r");<br>
> > + if (!f) {<br>
> > + pr_perror("Cannot fopen %s", buf);<br>
> > + return -1;<br>
> > + }<br>
> > +<br>
> > + do {<br>
> > + n = linelen - 2;<br>
> > + line[n] = '\0'; /* detect long input */<br>
> > + if (fgets(line, linelen, f) == NULL) {<br>
> > + ret = 0;<br>
> > + break;<br>
> > + }<br>
> > + if (line[n] && line[n] != '\n') {<br>
> > + pr_err("Line in mountinfo too long\n");<br>
> > + ret = -1;<br>
> > + goto out;<br>
> > + }<br>
> > +<br>
> > + ret = sscanf(line, "%*i %*i %*u:%*u %*s %s %*s - %n", buf, &n);<br>
><br>
> Wow. Doesn't gcc prints a warning about "too few arguments for format"?<br>
><br>
><br>
> No, because * means discard what's scanned.<br>
<br>
</div></div>Wow :)<br>
<div class=""><div class="h5"><br>
> > + if (ret != 1) {<br>
> > + pr_err("Cannot parse mountpoint (%s)\n", line);<br>
> > + ret = -1;<br>
> > + goto out;<br>
> > + }<br>
> > + } while (strcmp(buf, mntpoint));<br>
> > +<br>
> > + if (!ret) {<br>
> > + pr_err("Did not find %s in mountinfo\n", mntpoint);<br>
> > + ret = -1;<br>
> > + } else<br>
> > + ret = n;<br>
> > +<br>
> > +out:<br>
> > + fclose(f);<br>
> > + return ret;<br>
> > +}<br>
><br>
> Thanks,<br>
> Pavel<br>
><br>
><br>
> Cheers!<br>
><br>
> --Saied<br>
><br>
<br>
</div></div></blockquote></div></div><br></div>