daemon.c does just a chroot, without chdir. The result is that pwd does not work correctly (it causes fs/dcache.c:prepend_unreachable() to add the unreachable string). A workaround is to add "cd /" before each sh command.><fs> mount /dev/sda2 / ><fs> sh "cd / ; chroot / ; /bin/pwd"/><fs> sh "/bin/pwd"(unreachable)/><fs> sh "cd / ; /bin/pwd"/ This untested change may fix it. ==================--- libguestfs-1.20.10.orig/daemon/guestfsd.c +++ libguestfs-1.20.10/daemon/guestfsd.c @@ -879,7 +879,11 @@ commandrvf (char **stdoutput, char **std close (stdin_fd[PIPE_READ]); close (stdin_fd[PIPE_WRITE]); - if (chroot (sysroot) == -1) { + if (chdir (sysroot) == -1) { + perror ("chdir"); + _exit (EXIT_FAILURE); + } + if (chroot (".") == -1) { perror ("chroot"); _exit (EXIT_FAILURE); } Olaf
On Sun, Aug 18, Olaf Hering wrote:> > daemon.c does just a chroot, without chdir. The result is that pwd does not > work correctly (it causes fs/dcache.c:prepend_unreachable() to add the > unreachable string). A workaround is to add "cd /" before each sh command. > > ><fs> mount /dev/sda2 / > ><fs> sh "cd / ; chroot / ; /bin/pwd" > / > > ><fs> sh "/bin/pwd" > (unreachable)/ > > ><fs> sh "cd / ; /bin/pwd" > /This change fixes the "pwd" output for me. guestfish add-drive /dev/shm/olaf/vdisk-sles11sp2_full_fate310510-disk0 : launch : mount /dev/sda2 / : sh pwd : quit / Olaf --- daemon/command.c | 11 +++++++++++ 1 file changed, 11 insertions(+) Index: libguestfs-1.20.10/daemon/command.c ==================================================================--- libguestfs-1.20.10.orig/daemon/command.c +++ libguestfs-1.20.10/daemon/command.c @@ -21,6 +21,9 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> #include "guestfs_protocol.h" #include "daemon.h" @@ -46,6 +49,7 @@ do_command (char *const *argv) CLEANUP_FREE char *sysroot_dev = NULL, *sysroot_dev_pts = NULL, *sysroot_proc = NULL, *sysroot_selinux = NULL, *sysroot_sys = NULL; int dev_ok, dev_pts_ok, proc_ok, sys_ok; + int cwd_fd; /* We need a root filesystem mounted to do this. */ NEED_ROOT (, return NULL); @@ -89,8 +93,15 @@ do_command (char *const *argv) r = command (NULL, NULL, str_mount, "--bind", "/sys", sysroot_sys, NULL); sys_ok = r != -1; + cwd_fd = open(".", O_DIRECTORY); CHROOT_IN; + if (cwd_fd >= 0) + chdir("/"); r = commandv (&out, &err, (const char * const *) argv); + if (cwd_fd >= 0) { + fchdir(cwd_fd); + close(cwd_fd); + } CHROOT_OUT; if (sys_ok) umount_ignore_fail (sysroot_sys);
Richard W.M. Jones
2013-Aug-19 09:07 UTC
Re: [Libguestfs] missing chdir before chroot in guestfsd
On Sun, Aug 18, 2013 at 02:29:15PM +0200, Olaf Hering wrote:> > daemon.c does just a chroot, without chdir. The result is that pwd does not > work correctly (it causes fs/dcache.c:prepend_unreachable() to add the > unreachable string). A workaround is to add "cd /" before each sh command. > > ><fs> mount /dev/sda2 / > ><fs> sh "cd / ; chroot / ; /bin/pwd" > / > > ><fs> sh "/bin/pwd" > (unreachable)/ > > ><fs> sh "cd / ; /bin/pwd" > / > > This untested change may fix it. > > ==================> --- libguestfs-1.20.10.orig/daemon/guestfsd.c > +++ libguestfs-1.20.10/daemon/guestfsd.c > @@ -879,7 +879,11 @@ commandrvf (char **stdoutput, char **std > close (stdin_fd[PIPE_READ]); > close (stdin_fd[PIPE_WRITE]); > > - if (chroot (sysroot) == -1) { > + if (chdir (sysroot) == -1) { > + perror ("chdir"); > + _exit (EXIT_FAILURE); > + } > + if (chroot (".") == -1) { > perror ("chroot"); > _exit (EXIT_FAILURE); > }If you look at the upstream code, this has been fixed, or at least, the code is quite different. See: commit 2aa731e7c441581f0d72bcd06e9431b92931466a Author: Matthew Booth <mbooth@redhat.com> Date: Thu Dec 13 15:22:39 2012 +0000 daemon: Remove redundant fork in commandrvf Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
Olaf Hering
2013-Aug-19 18:08 UTC
Re: [Libguestfs] missing chdir before chroot in guestfsd
On Mon, Aug 19, Richard W.M. Jones wrote:> If you look at the upstream code, this has been fixed, or at > least, the code is quite different. See: > > commit 2aa731e7c441581f0d72bcd06e9431b92931466aIt does not happen with 1.23.17. Thanks. Olaf
Seemingly Similar Threads
- missing chdir before chroot in guestfsd
- [PATCH 1/2] daemon: NFC Use symbolic names in commandrvf
- [PATCH v3 2/6] daemon: Split out command() functions and CLEANUP_* macros into separate files.
- [PATCH] daemon: improve debugging for "stdout on stderr" flag
- Re: [PATCH] daemon: improve internal commandrvf