Search in sources :

Example 1 with ACLBuilder

use of org.jvnet.solaris.libzfs.ACLBuilder in project hudson-2.x by hudson.

the class ZFSInstaller method createZfsFileSystem.

/**
     * Creates a ZFS file system to migrate the data to.
     *
     * <p>
     * This has to be done while we still have an interactive access with the user, since it involves the password.
     *
     * <p>
     * An exception will be thrown if the operation fails. A normal completion means a success.
     *
     * @return
     *      The ZFS dataset name to migrate the data to.
     */
private String createZfsFileSystem(final TaskListener listener, String rootUsername, String rootPassword) throws IOException, InterruptedException, ZFSException {
    // capture the UID that Hudson runs under
    // so that we can allow this user to do everything on this new partition
    final int uid = LIBC.geteuid();
    final int gid = LIBC.getegid();
    passwd pwd = LIBC.getpwuid(uid);
    if (pwd == null)
        throw new IOException("Failed to obtain the current user information for " + uid);
    final String userName = pwd.pw_name;
    final File home = Hudson.getInstance().getRootDir();
    // return true indicating a success
    return SU.execute(listener, rootUsername, rootPassword, new Callable<String, IOException>() {

        public String call() throws IOException {
            PrintStream out = listener.getLogger();
            LibZFS zfs = new LibZFS();
            ZFSFileSystem existing = zfs.getFileSystemByMountPoint(home);
            if (existing != null) {
                // no need for migration
                out.println(home + " is already on ZFS. Doing nothing");
                return existing.getName();
            }
            String name = computeHudsonFileSystemName(zfs, zfs.roots().get(0));
            out.println("Creating " + name);
            ZFSFileSystem hudson = zfs.create(name, ZFSFileSystem.class);
            // mount temporarily to set the owner right
            File dir = Util.createTempDir();
            hudson.setMountPoint(dir);
            hudson.mount();
            if (LIBC.chown(dir.getPath(), uid, gid) != 0)
                throw new IOException("Failed to chown " + dir);
            hudson.unmount();
            try {
                // mark this file system as "managed by Hudson"
                hudson.setProperty("hudson:managed-by", "hudson");
                ACLBuilder acl = new ACLBuilder();
                acl.user(userName).withEverything();
                hudson.allow(acl);
            } catch (ZFSException e) {
                // revert the file system creation
                try {
                    hudson.destory();
                } catch (Exception _) {
                // but ignore the error and let the original error thrown
                }
                throw e;
            }
            return hudson.getName();
        }
    });
}
Also used : PrintStream(java.io.PrintStream) CLibrary.passwd(org.jvnet.libpam.impl.CLibrary.passwd) ACLBuilder(org.jvnet.solaris.libzfs.ACLBuilder) ZFSException(org.jvnet.solaris.libzfs.ZFSException) LibZFS(org.jvnet.solaris.libzfs.LibZFS) ZFSFileSystem(org.jvnet.solaris.libzfs.ZFSFileSystem) IOException(java.io.IOException) File(java.io.File) ServletException(javax.servlet.ServletException) ZFSException(org.jvnet.solaris.libzfs.ZFSException) IOException(java.io.IOException)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 ServletException (javax.servlet.ServletException)1 CLibrary.passwd (org.jvnet.libpam.impl.CLibrary.passwd)1 ACLBuilder (org.jvnet.solaris.libzfs.ACLBuilder)1 LibZFS (org.jvnet.solaris.libzfs.LibZFS)1 ZFSException (org.jvnet.solaris.libzfs.ZFSException)1 ZFSFileSystem (org.jvnet.solaris.libzfs.ZFSFileSystem)1