guestfs-golang(3)

πŸ’– guestfs-golang(3) β€” Virtualization Support ✨

okay bestie, gather round, because this is the page where we learn how to call libguestfs from Go πŸΉπŸ’… (and yes we're in section 3 of the manual, under Virtualization Support, in case you were wondering where you wandered off to 🧚)


🌸 NAME 🌸

guestfs-golang β€” literally how to use libguestfs from Go, no cap πŸ¦‹


✨ SYNOPSIS ✨

import "libguestfs.org/guestfs"

g, errno := guestfs.Create ()
if errno != nil {
    panic (fmt.Sprintf ("could not create handle: %s", errno))
}
defer g.Close ()
if err := g.Add_drive ("test.img"); err != nil {
    panic (err)
}
if err := g.Launch (); err != nil {
    panic (err)
}
if err := g.Shutdown (); err != nil {
    panic (err)
}

like… that's the whole vibe in 15 lines, we love a concise queen πŸ‘‘


🫢 DESCRIPTION 🫢

This manual page documents how to call libguestfs from the Go programming language. This page just documents the differences from the C API and gives some examples. If you are not familiar with using libguestfs, you also need to read guestfs(3) β€” fr fr, go read it, it's the prequel πŸ“–βœ¨

πŸŽ€ IMPORTING THE MODULE πŸŽ€

The module is called guestfs. The full package name to import is libguestfs.org/guestfs.

that's it. that's the subsection. iconic brevity πŸ’…

πŸ¦‹ CREATING AND CLOSING THE HANDLE πŸ¦‹

Use either guestfs.Create or guestfs.Create_flags to create the handle. The handle is closed implicitly if it is garbage collected. However it is probably a good idea to close it explicitly, either by calling g.Close () or by deferring the same.

like, the garbage collector will eventually handle it, but don't be that person, bestie 😭 call g.Close () or defer it and move on with your life 🫢

😭 ERRORS 😭

guestfs.Create and guestfs.Create_flags return a simple *error, which is really just a C errno wrapped up in the appropriate golang struct.

All other calls return a *GuestfsError which (if non-nil) is a richer struct that contains the error string from libguestfs, the errno (if available) and the operation which failed. This can also be converted to a string for display.

so like: Create gives you the little baby error, everything else gives you the deluxe rich-text error with ✨lore✨ β€” the message, the errno if it's around, and which operation flopped πŸ’€

πŸ™ˆ LIMITATIONS πŸ™ˆ

  • No support for events (see EVENTS in guestfs(3)). not me being sad about it but okay πŸ˜”
  • UUIDs are not returned in structures.

πŸ”₯ EXAMPLE 1: CREATE A DISK IMAGE πŸ”₯

/* Example showing how to create a disk image. */

package main

import (
       "fmt"
       "libguestfs.org/guestfs"
)

func main() {
       output := "disk.img"

       g, errno := guestfs.Create ()
       if errno != nil {
               panic (errno)
       }
       defer g.Close ()

       /* Create a raw-format sparse disk image, 512 MB in size. */
       if err := g.Disk_create (output, "raw", 512 * 1024 * 1024); err != nil {
               panic (err)
       }

       /* Set the trace flag so that we can see each libguestfs call. */
       g.Set_trace (true)

       /* Attach the disk image to libguestfs. */
       optargs := guestfs.OptargsAdd_drive{
               Format_is_set: true,
               Format: "raw",
               Readonly_is_set: true,
               Readonly: false,
       }
       if err := g.Add_drive (output, &optargs); err != nil {
               panic (err)
       }

       /* Run the libguestfs back-end. */
       if err := g.Launch (); err != nil {
               panic (err)
       }

       /* Get the list of devices.  Because we only added one drive
        * above, we expect that this list should contain a single
        * element.
        */
       devices, err := g.List_devices ()
       if err != nil {
               panic (err)
       }
       if len(devices) != 1 {
               panic ("expected a single device from list-devices")
       }

       /* Partition the disk as one single MBR partition. */
       err = g.Part_disk (devices[0], "mbr")
       if err != nil {
               panic (err)
       }

       /* Get the list of partitions.  We expect a single element, which
        * is the partition we have just created.
        */
       partitions, err := g.List_partitions ()
       if err != nil {
               panic (err)
       }
       if len(partitions) != 1 {
               panic ("expected a single partition from list-partitions")
       }

       /* Create a filesystem on the partition. */
       err = g.Mkfs ("ext4", partitions[0], nil)
       if err != nil {
               panic (err)
       }

       /* Now mount the filesystem so that we can add files. */
       err = g.Mount (partitions[0], "/")
       if err != nil {
               panic (err)
       }

       /* Create some files and directories. */
       err = g.Touch ("/empty")
       if err != nil {
               panic (err)
       }
       message := []byte("Hello, world\n")
       err = g.Write ("/hello", message)
       if err != nil {
               panic (err)
       }
       err = g.Mkdir ("/foo")
       if err != nil {
               panic (err)
       }

       /* This one uploads the local file /etc/resolv.conf into
        * the disk image.
        */
       err = g.Upload ("/etc/resolv.conf", "/foo/resolv.conf")
       if err != nil {
               panic (err)
       }

       /* Because we wrote to the disk and we want to detect write
        * errors, call g:shutdown.  You don't need to do this:
        * g.Close will do it implicitly.
        */
       if err = g.Shutdown (); err != nil {
               panic (fmt.Sprintf ("write to disk failed: %s", err))
       }
}
  • creates a raw sparse disk image called disk.img sized 512 * 1024 * 1024 (that's 512 MB bestie πŸ’Ύ)
  • flips on tracing with g.Set_trace (true) so you can watch every single call like it's a livestream πŸ‘€
  • attaches the drive with guestfs.OptargsAdd_drive, hitting Format_is_set: true, Format: "raw", Readonly_is_set: true, Readonly: false
  • launches the back-end, then checks that g.List_devices () gives back exactly one device (it panics with "expected a single device from list-devices" otherwise!)
  • partitions it as one MBR partition with g.Part_disk (devices[0], "mbr") and checks g.List_partitions () returns exactly one
  • makes an ext4 filesystem with g.Mkfs ("ext4", partitions[0], nil), mounts it at /
  • touches /empty, writes []byte("Hello, world\n") into /hello, makes the directory /foo πŸ“
  • uploads your local /etc/resolv.conf into /foo/resolv.conf
  • finally calls g.Shutdown () to catch write errors β€” but the comment says you don't actually need to do this, because g.Close will do it implicitly ✨

πŸ‘‘ EXAMPLE 2: INSPECT A VIRTUAL MACHINE DISK IMAGE πŸ‘‘

/* Example showing how to inspect a virtual machine disk. */

package main

import (
       "fmt"
       "os"
       "libguestfs.org/guestfs"
)

func main() {
       if len(os.Args) < 2 {
               panic ("usage: inspect-vm disk.img")
       }
       disk := os.Args[1]

       g, errno := guestfs.Create ()
       if errno != nil {
               panic (fmt.Sprintf ("could not create handle: %s", errno))
       }

       /* Attach the disk image read-only to libguestfs. */
       optargs := guestfs.OptargsAdd_drive{
               Format_is_set: true,
               Format: "raw",
               Readonly_is_set: true,
               Readonly: true,
       }
       if err := g.Add_drive (disk, &optargs); err != nil {
               panic (err)
       }

       /* Run the libguestfs back-end. */
       if err := g.Launch (); err != nil {
               panic (err)
       }

       /* Ask libguestfs to inspect for operating systems. */
       roots, err := g.Inspect_os ()
       if err != nil {
               panic (err)
       }
       if len(roots) == 0 {
               panic ("inspect-vm: no operating systems found")
       }

       for _, root := range roots {
               fmt.Printf ("Root device: %s\n", root)

               /* Print basic information about the operating system. */
               s, _ := g.Inspect_get_product_name (root)
               fmt.Printf ("  Product name: %s\n", s)
               major, _ := g.Inspect_get_major_version (root)
               minor, _ := g.Inspect_get_minor_version (root)
               fmt.Printf ("  Version:      %d.%d\n", major, minor)
               s, _ = g.Inspect_get_type (root)
               fmt.Printf ("  Type:         %s\n", s)
               s, _ = g.Inspect_get_distro (root)
               fmt.Printf ("  Distro:       %s\n", s)

               /* XXX Incomplete example.  Sorting the keys by length
                * is unnecessarily hard in golang.
                */
       }
}
  • takes the disk image path off os.Args[1], and if you didn't give it one it panics with "usage: inspect-vm disk.img" β€” read the instructions, bestie πŸ™ˆ
  • attaches the disk read-only this time: Format_is_set: true, Format: "raw", Readonly_is_set: true, Readonly: true πŸ”’
  • launches, then calls g.Inspect_os (); if len(roots) == 0 it panics with "inspect-vm: no operating systems found" πŸ’€
  • loops over every root device and prints the root device, the product name (g.Inspect_get_product_name), the version as %d.%d from g.Inspect_get_major_version and g.Inspect_get_minor_version, the type (g.Inspect_get_type) and the distro (g.Inspect_get_distro)
  • and then there's a lil XXX Incomplete example. Sorting the keys by length is unnecessarily hard in golang. comment just vibing at the bottom, because honesty is key 🫢

πŸ’œ SEE ALSO πŸ’œ

guestfs(3), guestfs-examples(3), guestfs-erlang(3), guestfs-java(3), guestfs-lua(3), guestfs-ocaml(3), guestfs-perl(3), guestfs-python(3), guestfs-recipes(1), guestfs-ruby(3), http://www.golang.org/, http://libguestfs.org/.

the whole polyglot friend group showed up πŸŒˆπŸ’–


🌟 AUTHORS 🌟

Richard W.M. Jones (rjones at redhat dot com) β€” the icon, the legend, the moment πŸ‘‘


Copyright (C) 2013 Red Hat Inc.


✨ LICENSE ✨

This manual page contains examples which we hope you will use in your programs. The examples may be freely copied, modified and distributed for any purpose without any restrictions.

no restrictions, bestie!! copy it, remix it, ship it, slay πŸ’…βœ¨


πŸ’€ BUGS πŸ’€

To get a list of bugs against libguestfs, use this link: https://bugzilla.redhat.com/buglist.cgi?component=libguestfs&product=Virtualization+Tools

To report a new bug against libguestfs, use this link: https://bugzilla.redhat.com/enter_bug.cgi?component=libguestfs&product=Virtualization+Tools

When reporting a bug, please supply:

  • The version of libguestfs.
  • Where you got libguestfs (eg. which Linux distro, compiled from source, etc)
  • Describe the bug accurately and give a way to reproduce it.
  • Run libguestfs-test-tool(1) and paste the complete, unedited output into the bug report.
bestie please don't paste an edited output 😭 the whole point is the complete, unedited thing. give them the raw footage, not the highlight reel 🎬

okay that's the whole page!! this was libguestfs-1.60.1, dated 2026-07-14 β€” and yeah, it's the guestfs-golang(3) page from the Virtualization Support manual, mwah πŸ’‹ go write some Go, you're gonna eat πŸ½οΈπŸ’–βœ¨

πŸ“¦ where this page comes from