1

正在运行的 C 程序如何检测它是否在 illumos/solaris 上的“chroot”中运行?

有一个 Debian 实用程序 [1] 在 linux、freebsd、hurd 上使用了一些技巧。

如何在 illumos/solaris 上做到这一点?

[1] http://anonscm.debian.org/gitweb/?p=users/clint/debianutils.git;a=blob;f=ischroot.c;h=bed67f9f655d26906e3d65fd290698dce5402a89;hb=HEAD

4

2 回答 2

2

There's this thread on comp.unix.solaris which details various methods to find out whether Solaris runs inside a zone (container / chroot-on-lots-of-steroids) or a VM. Particularly extensive is the list in this posting (seems to have come from this blog post. I'll recreate some of the items relating to zones here - those that are available to non-root users.

  • The /usr/bin/zonename command tells you something different than "global"
  • The Solaris kernel "process" is called sched in a non-zoned (or inside the "global" / admin zone) environment but zsched inside a zone.
  • The Solaris prstat command (similar to top on other UN*Xes) has -z / -Z command line options to restrict reporting to specific zones (-z) or give statistics for all zones (-Z); inside a zone, these options don't work / only show you the zone you're running inside.

Hope that helps a little.

于 2012-07-13T09:11:47.313 回答
1

我用这种方式:

#elif defined (__sun__)

/* Similar to Linux
 * XXX: check zone?
 * XXX: illumos kernel automatically mounts /proc on boot
 */

static int ischroot()
{
  struct stat st1, st2;

  if (stat("/", &st1))
    return 2;
  if (stat("/proc/1/root", &st2))
    return 2;
  if ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino))
    return 1;
  return 0;
}

#else
于 2014-07-26T20:56:49.867 回答