[Libguestfs] missing chdir before chroot in guestfsd

Olaf Hering olaf at aepfle.de
Sun Aug 18 13:26:49 UTC 2013


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




More information about the Libguestfs mailing list