.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Podwrapper::Man 1.54.0 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "guestfs-golang 3" .TH guestfs-golang 3 2024-10-08 libguestfs-1.54.0 "Virtualization Support" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME guestfs\-golang \- How to use libguestfs from Go .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& 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) \& } .Ve .SH DESCRIPTION .IX Header "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 \fBguestfs\fR\|(3). .SS "IMPORTING THE MODULE" .IX Subsection "IMPORTING THE MODULE" The module is called \f(CW\*(C`guestfs\*(C'\fR. The full package name to import is \&\f(CW\*(C`libguestfs.org/guestfs\*(C'\fR. .SS "CREATING AND CLOSING THE HANDLE" .IX Subsection "CREATING AND CLOSING THE HANDLE" Use either \f(CW\*(C`guestfs.Create\*(C'\fR or \f(CW\*(C`guestfs.Create_flags\*(C'\fR 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 \f(CW\*(C`g.Close\ ()\*(C'\fR or by deferring the same. .SS ERRORS .IX Subsection "ERRORS" \&\f(CW\*(C`guestfs.Create\*(C'\fR and \f(CW\*(C`guestfs.Create_flags\*(C'\fR return a simple \&\f(CW*error\fR, which is really just a C \f(CW\*(C`errno\*(C'\fR wrapped up in the appropriate golang struct. .PP All other calls return a \f(CW*GuestfsError\fR 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. .SS LIMITATIONS .IX Subsection "LIMITATIONS" .IP \(bu 4 No support for events (see "EVENTS" in \fBguestfs\fR\|(3)). .IP \(bu 4 UUIDs are not returned in structures. .SH "EXAMPLE 1: CREATE A DISK IMAGE" .IX Header "EXAMPLE 1: CREATE A DISK IMAGE" .Vb 1 \& /* 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\en") \& 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\*(Aqt 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)) \& } \& } .Ve .SH "EXAMPLE 2: INSPECT A VIRTUAL MACHINE DISK IMAGE" .IX Header "EXAMPLE 2: INSPECT A VIRTUAL MACHINE DISK IMAGE" .Vb 1 \& /* 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\en", root) \& \& /* Print basic information about the operating system. */ \& s, _ := g.Inspect_get_product_name (root) \& fmt.Printf (" Product name: %s\en", s) \& major, _ := g.Inspect_get_major_version (root) \& minor, _ := g.Inspect_get_minor_version (root) \& fmt.Printf (" Version: %d.%d\en", major, minor) \& s, _ = g.Inspect_get_type (root) \& fmt.Printf (" Type: %s\en", s) \& s, _ = g.Inspect_get_distro (root) \& fmt.Printf (" Distro: %s\en", s) \& \& /* XXX Incomplete example. Sorting the keys by length \& * is unnecessarily hard in golang. \& */ \& } \& } .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fBguestfs\fR\|(3), \&\fBguestfs\-examples\fR\|(3), \&\fBguestfs\-erlang\fR\|(3), \&\fBguestfs\-gobject\fR\|(3), \&\fBguestfs\-java\fR\|(3), \&\fBguestfs\-lua\fR\|(3), \&\fBguestfs\-ocaml\fR\|(3), \&\fBguestfs\-perl\fR\|(3), \&\fBguestfs\-python\fR\|(3), \&\fBguestfs\-recipes\fR\|(1), \&\fBguestfs\-ruby\fR\|(3), http://www.golang.org/, http://libguestfs.org/. .SH AUTHORS .IX Header "AUTHORS" Richard W.M. Jones (\f(CW\*(C`rjones at redhat dot com\*(C'\fR) .SH COPYRIGHT .IX Header "COPYRIGHT" Copyright (C) 2013 Red Hat Inc. .SH LICENSE .IX Header "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. .SH BUGS .IX Header "BUGS" To get a list of bugs against libguestfs, use this link: https://bugzilla.redhat.com/buglist.cgi?component=libguestfs&product=Virtualization+Tools .PP To report a new bug against libguestfs, use this link: https://bugzilla.redhat.com/enter_bug.cgi?component=libguestfs&product=Virtualization+Tools .PP When reporting a bug, please supply: .IP \(bu 4 The version of libguestfs. .IP \(bu 4 Where you got libguestfs (eg. which Linux distro, compiled from source, etc) .IP \(bu 4 Describe the bug accurately and give a way to reproduce it. .IP \(bu 4 Run \fBlibguestfs\-test\-tool\fR\|(1) and paste the \fBcomplete, unedited\fR output into the bug report.