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); } } }
I realized that this code is missing the call to shutdown the disk and detect write errors. (Sorry about that -- the code was adapted from a read-only example where this is not necessary).> // Unmount everything. > g.umount_all ();After these two lines, add: g.shutdown (); g.close (); Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into Xen guests. http://et.redhat.com/~rjones/virt-p2v
i try it in my environment?it allways throws the exception : com.redhat.et.libguestfs.LibGuestFSException: sh: /bin/sh: Exec format error then i try the following programs, "Exec format error" still happens: String cmd1 = "chkconfig --add tomcat"; g.sh(cmd1); String cmd2 = "/sbin/chkconfig --add tomcat"; g.sh(cmd2); String[] cmd = new String[] {"/sbin/chkconfig", "--list"}; g.command(cmd); but i can execute the command in system, could show me the reason, Thanks! ?? From: Richard W.M. Jones Date: 2012-08-28 00:39 To: libguestfs at redhat.com CC: tao.zhou at neusoft.com Subject: Re: libguestfs! help! 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 --------------------------------------------------------------------------------------------------- Confidentiality Notice: The information contained in this e-mail and any accompanying attachment(s) is intended only for the use of the intended recipient and may be confidential and/or privileged of Neusoft Corporation, its subsidiaries and/or its affiliates. If any reader of this communication is not the intended recipient, unauthorized use, forwarding, printing, storing, disclosure or copying is strictly prohibited, and may be unlawful.If you have received this communication in error,please immediately notify the sender by return e-mail, and delete the original message and all copies from your system. Thank you. --------------------------------------------------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://listman.redhat.com/archives/libguestfs/attachments/20120829/8d19915f/attachment.htm>
On Wed, Aug 29, 2012 at 11:34:51AM +0800, ?? wrote:> i try it in my environment?it allways throws the exception : > com.redhat.et.libguestfs.LibGuestFSException: sh: /bin/sh: Exec format error > then i try the following programs, "Exec format error" still happens: > String cmd1 = "chkconfig --add tomcat"; > g.sh(cmd1); > String cmd2 = "/sbin/chkconfig --add tomcat"; > g.sh(cmd2); > String[] cmd = new String[] {"/sbin/chkconfig", "--list"}; > g.command(cmd); > but i can execute the command in system, could show me the reason, Thanks!Well it's something to do with the guest being either a much older or much newer version of Linux than the host, or perhaps it's a different architecture (32 vs 64 bits?), or not even Linux at all. The way to solve this is to use a firstboot script:> From: Richard W.M. Jones > Date: 2012-08-28 00:39 > To: libguestfs at redhat.com[...]> 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 virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://et.redhat.com/~rjones/virt-df/
Reasonably Related Threads
- [PATCH v2 1/2] firstboot: rename systemd and sysvinit
- java.lang.UnsatisfiedLinkError
- libvirt0.8.7.tar.gz installation failure
- [PATCH v2 0/4] customize: firstboot: Install firstboot scripts in multi-user.target (RHBZ#1469655).
- [PATCH v2 00/11] Getting it work with SLES / openSUSE