user933161
user933161

Reputation:

How to detect chroot on illumos/solaris?

How can a running C-program detect if it is running in "chroot" on illumos/solaris?

There is a Debian utility [1] which uses some tricks on linux, freebsd, hurd.

How to do it on illumos/solaris?

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

Upvotes: 1

Views: 291

Answers (2)

user933161
user933161

Reputation:

I used this way:

#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

Upvotes: 1

FrankH.
FrankH.

Reputation: 18227

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.

Upvotes: 2

Related Questions