Calling libguestfs from Javascript

In libguestfs 1.16 we added experimental GObject bindings and support for GObject Introspection. These are experimental because we may change them a little in future. They do allow you to access libguestfs from Javascript, specifically from gjs.

Here is an example program (fixed and updated):

const Guestfs =;

function inspect (filename)
    var g = new Guestfs.Session ();

    //g.set_trace (true);

    var optargs = new Guestfs.AddDriveOpts ({readonly: true});
    g.add_drive_opts (filename, optargs);

    g.launch ();

    var roots = g.inspect_os ()
    if (roots.length == 0)
        printerr ("inspection: no operating systems found in", filename);
    else {
        for (var i = 0; i < roots.length; ++i) {
            inspect_root (g, roots[i]);

function inspect_root (g, root)
    print ("inspecting operating system root", root);

    print ("  product name:", g.inspect_get_product_name (root));
    print ("  version:",
           g.inspect_get_major_version (root),
           g.inspect_get_minor_version (root));
    //print ("  type:", g.inspect_get_type (root));
    print ("  distro:", g.inspect_get_distro (root));

    // Mount up the disks like guestfish -i
    var mps = g.inspect_get_mountpoints (root);
    var keys = [];
    for (var key in mps) { keys.push (key); }
    function compare (a, b) {
        if (a.length > b.length) return 1;
        else if (a.length == b.length) return 0;
        else return -1;
    keys.sort (compare);

    for (var i = 0; i < keys.length; ++i) {
        g.mount_ro (mps[keys[i]], keys[i]);

    // Get the list of applications.
    print ("  applications:");
    apps = g.inspect_list_applications (root);
    for (var i = 0; i < apps.length; ++i) {
        print ("    ", apps[i].app_name,
               apps[i].app_version, apps[i].app_release);

    g.umount_all ();

if (ARGV.length != 1) {
    printerr ("Usage: gjs test.js disk.img");
} else {
    inspect (ARGV[0]);

Leave a comment

Filed under Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.