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();
}
});
}
Aggregations