正在运行的 C 程序如何检测它是否在 illumos/solaris 上的“chroot”中运行?
有一个 Debian 实用程序 [1] 在 linux、freebsd、hurd 上使用了一些技巧。
如何在 illumos/solaris 上做到这一点?
正在运行的 C 程序如何检测它是否在 illumos/solaris 上的“chroot”中运行?
有一个 Debian 实用程序 [1] 在 linux、freebsd、hurd 上使用了一些技巧。
如何在 illumos/solaris 上做到这一点?
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.
/usr/bin/zonename
command tells you something different than "global"sched
in a non-zoned (or inside the "global" / admin zone) environment but zsched
inside a zone. 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.
我用这种方式:
#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