private static boolean migrate(TaskListener listener, String target) throws IOException, InterruptedException {
        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
        out.println("Deleting "+backup);
        if(system(new File("/"),listener,"/usr/bin/rm","-rf",backup.getAbsolutePath())!=0) {
            out.println("Failed to delete "+backup.getAbsolutePath());
            return false;
        }

        out.println("Migration completed");
        return true;
    }
private static boolean migrate ( TaskListener listener , String target ) throws IOException , InterruptedException { PrintStream out = listener . getLogger ( ) ; File home = Jenkins . getInstance ( ) . getRootDir ( ) ; 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 ( ) ; out . println ( "Opening " + target ) ; ZFSFileSystem hudson = zfs . open ( target , ZFSFileSystem . class ) ; hudson . setMountPoint ( tmpDir ) ; hudson . setProperty ( "hudson:managed-by" , "hudson" ) ; hudson . mount ( ) ; 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 ; } out . println ( "Unmounting " + target ) ; hudson . unmount ( MountFlags . MS_FORCE ) ; 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" ) ; } 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 ( ) ) ; } out . println ( "Deleting " + backup ) ; if ( system ( new File ( "/" ) , listener , "/usr/bin/rm" , "-rf" , backup . getAbsolutePath ( ) ) != 0 ) { out . println ( "Failed to delete " + backup . getAbsolutePath ( ) ) ; return false ; } out . println ( "Migration completed" ) ; return true ; }
Migrates $JENKINS_HOME to a new ZFS file system.

TODO: do this in a separate JVM to elevate the privilege.

@param listener
Log of migration goes here.
@param target
Dataset to move the data to.
@return
false if a migration failed.