PrintStream out = listener.getLogger();
File home = Jenkins.getInstance().getRootDir();
// do the migration
LibZFS zfs = new LibZFS();
ZFSFileSystem existing = zfs.getFileSystemByMountPoint(home);
if(existing!=null) {
out.println(home+" is already on ZFS. Doing nothing");
return true;
}
File tmpDir = Util.createTempDir();
// mount a new file system to a temporary location
out.println("Opening "+target);
ZFSFileSystem hudson = zfs.open(target, ZFSFileSystem.class);
hudson.setMountPoint(tmpDir);
hudson.setProperty("hudson:managed-by","hudson"); // mark this file system as "managed by Hudson"
hudson.mount();
// copy all the files
out.println("Copying all existing data files");
if(system(home,listener, "/usr/bin/cp","-pR",".", tmpDir.getAbsolutePath())!=0) {
out.println("Failed to copy "+home+" to "+tmpDir);
return false;
}
// unmount
out.println("Unmounting "+target);
hudson.unmount(MountFlags.MS_FORCE);
// move the original directory to the side
File backup = new File(home.getPath()+".backup");
out.println("Moving "+home+" to "+backup);
if(backup.exists())
Util.deleteRecursive(backup);
if(!home.renameTo(backup)) {
out.println("Failed to move your current data "+home+" out of the way");
}
// update the mount point
out.println("Creating a new mount point at "+home);
if(!home.mkdir())
throw new IOException("Failed to create mount point "+home);
out.println("Mounting "+target);
hudson.setMountPoint(home);
hudson.mount();
out.println("Sharing "+target);
try {
hudson.setProperty("sharesmb","on");
hudson.setProperty("sharenfs","on");
hudson.share();
} catch (ZFSException e) {
listener.error("Failed to share the file systems: "+e.getCode());
}
// delete back up