Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 26 Jul 2018 04:07:37 +0000 (UTC)
From:      Kyle Evans <kevans@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-projects@freebsd.org
Subject:   svn commit: r336731 - projects/bectl/sbin/bectl
Message-ID:  <201807260407.w6Q47biK033951@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kevans
Date: Thu Jul 26 04:07:36 2018
New Revision: 336731
URL: https://svnweb.freebsd.org/changeset/base/336731

Log:
  bectl(8): Redo jail using jail(3) API
  
  The jail is created with allow.mount, allow.mount.devfs, and
  enforce_statfs=1. Upon creation, we immediately attach, chdir to "/", and
  drop the user into a shell inside the jail.
  
  The default IP for this is arbitrarily 10.20.30.40.

Modified:
  projects/bectl/sbin/bectl/bectl.c

Modified: projects/bectl/sbin/bectl/bectl.c
==============================================================================
--- projects/bectl/sbin/bectl/bectl.c	Thu Jul 26 03:14:58 2018	(r336730)
+++ projects/bectl/sbin/bectl/bectl.c	Thu Jul 26 04:07:36 2018	(r336731)
@@ -356,8 +356,7 @@ bectl_cmd_jail(int argc, char *argv[])
 {
 	char *bootenv;
 	char mnt_loc[BE_MAXPATHLEN];
-	char buf[BE_MAXPATHLEN*2];
-	int err;
+	int err, jid;
 
 	/* struct jail be_jail = { 0 }; */
 
@@ -376,39 +375,30 @@ bectl_cmd_jail(int argc, char *argv[])
 	 * XXX TODO: if its already mounted, perhaps there should be a flag to
 	 * indicate its okay to proceed??
 	 */
-	if ((err = be_mount(be, bootenv, NULL, 0, mnt_loc)) != BE_ERR_SUCCESS)
+	if ((err = be_mount(be, bootenv, NULL, 0, mnt_loc)) != BE_ERR_SUCCESS) {
 		fprintf(stderr, "could not mount bootenv\n");
+		return (1);
+	}
 
-	/*
-	 * NOTE: this is not quite functional:
-	 * see https://github.com/vermaden/beadm/blob/master/HOWTO.htm on
-	 * neccesary modifications to correctly boot the jail
-	 */
+	/* XXX TODO: Make the IP/hostname configurable? */
+	jid = jail_setv(JAIL_CREATE | JAIL_ATTACH,
+	    "name", bootenv,
+	    "path", mnt_loc,
+	    "host.hostname", bootenv,
+	    "persist", "true",
+	    "ip4.addr", "10.20.30.40",
+	    "allow.mount", "true",
+	    "allow.mount.devfs", "true",
+	    "enforce_statfs", "1",
+	    NULL);
+	if (jid == -1) {
+		fprintf(stderr, "unable to create jail.  error: %d\n", errno);
+		return (1);
+	}
 
-	/*
-	 * snprintf(buf, BE_MAXPATHLEN*2, "jail %s %s %s /bin/sh /etc/rc",
-	 *    mnt_loc, bootenv, "192.168.1.123");
-	 */
-	snprintf(buf, BE_MAXPATHLEN*2, "jail %s %s %s /bin/sh", mnt_loc,
-	    bootenv, "192.168.1.123");
-	system(buf);
-
-	unmount(mnt_loc, 0);
-
-	/*
-	 * be_jail.version = JAIL_API_VERSION;
-	 * be_jail.path = "/tmp/be_mount.hCCk";
-	 * be_jail.jailname = "sdfs";
-	 *
-	 * if ((jid = jail(&be_jail)) != -1) {
-	 *      printf("jail %d created at %s\n", jid, mnt_loc);
-	 *      err = 0;
-	 * } else {
-	 *      fprintf(stderr, "unable to create jail.  error: %d\n", errno);
-	 *      err = errno;
-	 * }
-	 */
-
+	/* We're attached within the jail... good bye! */
+	chdir("/");
+	execl("/bin/sh", "/bin/sh", NULL);
 	return (0);
 }
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201807260407.w6Q47biK033951>