[Libguestfs] libguestfs! help!

Richard W.M. Jones rjones at redhat.com
Mon Aug 27 16:39:02 UTC 2012


On Sun, Aug 26, 2012 at 09:52:23PM -0400, tao zhou wrote:
> hello everyone!
>
> first i use libguestfs API to upload a file into my linux VM , and
> then i want to execute some command(for example : tar xzvf XXX、
> chkconfig --add XXX) to config my application by java libguestfs
> API,what should i do? could you show me an example in java?
> Thanks!

Attached is a simple example in Java.

However to run the command it may be better to run the command using a
"firstboot" script.  See:

  http://libguestfs.org/guestfs.3.html#running-commands
  http://libguestfs.org/virt-sysprep.1.html#firstboot-

(Note that the ability to use firstboot scripts was only added very
recently to virt-sysprep).

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
New in Fedora 11: Fedora Windows cross-compiler. Compile Windows
programs, test, and build Windows installers. Over 70 libraries supprt'd
http://fedoraproject.org/wiki/MinGW http://www.annexia.org/fedora_mingw
-------------- next part --------------
// Example showing how to upload file(s) and run commands in a VM.
// javac Example.java -classpath /usr/share/java/libguestfs-*.jar

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.redhat.et.libguestfs.*;

public class Example
{
    static final Comparator<String> COMPARE_KEYS_LEN =
        new Comparator<String>() {
        public int compare (String k1, String k2) {
            return k1.length() - k2.length();
        }
    };

    public static void main (String[] argv)
    {
        try {
            if (argv.length != 2)
                throw new Error ("usage: Example disk.img content.tar.gz");

            String disk = argv[0];
            String content_tgz = argv[1];

            GuestFS g = new GuestFS ();

            g.add_drive_opts (disk, null);

            // Run the libguestfs back-end.
            g.launch ();

            // Ask libguestfs to inspect for operating systems.
            String roots[] = g.inspect_os ();
            if (roots.length == 0)
                throw new Error ("no operating systems found");
            if (roots.length > 1)
                throw new Error ("multi-boot VM is not supported");

            String root = roots[0];

            // Mount up the disks, like guestfish -i.
            //
            // Sort keys by length, shortest first, so that we end up
            // mounting the filesystems in the correct order.
            Map<String,String> mps = g.inspect_get_mountpoints (root);
            List<String> mps_keys = new ArrayList (mps.keySet ());
            Collections.sort (mps_keys, COMPARE_KEYS_LEN);

            for (String mp : mps_keys) {
                String dev = mps.get (mp);
                try {
                    g.mount (dev, mp);
                }
                catch (Exception exn) {
                    System.err.println (exn + " (ignored)");
                }
            }

            // Upload content.
            g.tgz_in (content_tgz, "/");

            // Run a command.
            String cmd = "chkconfig --add myservice";
            g.sh (cmd);

            // Unmount everything.
            g.umount_all ();
        }
        catch (Exception exn) {
            System.err.println (exn);
            System.exit (1);
        }
    }
}


More information about the Libguestfs mailing list