1guestfs-ruby(3)             Virtualization Support             guestfs-ruby(3)
2
3
4

NAME

6       guestfs-ruby - How to use libguestfs from Ruby
7

SYNOPSIS

9        require 'guestfs'
10        g = Guestfs::Guestfs.new()
11        g.add_drive_opts("disk.img",
12                         :readonly => 1, :format => "raw")
13        g.launch()
14

DESCRIPTION

16       This manual page documents how to call libguestfs from the Ruby
17       programming language.  This page just documents the differences from
18       the C API and gives some examples.  If you are not familiar with using
19       libguestfs, you also need to read guestfs(3).
20
21   EXCEPTIONS
22       Errors from libguestfs functions are mapped into the "Error" exception.
23       This has a single parameter which is the error message (a string).
24

EXAMPLE 1: CREATE A DISK IMAGE

26        # Example showing how to create a disk image.
27
28        require 'guestfs'
29
30        output = "disk.img"
31
32        g = Guestfs::Guestfs.new()
33
34        # Create a raw-format sparse disk image, 512 MB in size.
35        File.open(output, "w") {
36          |f| f.truncate(512 * 1024 * 1024)
37        }
38
39        # Set the trace flag so that we can see each libguestfs call.
40        g.set_trace(1)
41
42        # Attach the disk image to libguestfs.
43        g.add_drive_opts(output, :format => "raw")
44
45        # Run the libguestfs back-end.
46        g.launch();
47
48        # Get the list of devices.  Because we only added one drive
49        # above, we expect that this list should contain a single
50        # element.
51        devices = g.list_devices()
52        if devices.length != 1 then
53          raise "error: expected a single device from list-devices"
54        end
55
56        # Partition the disk as one single MBR partition.
57        g.part_disk(devices[0], "mbr")
58
59        # Get the list of partitions.  We expect a single element, which
60        # is the partition we have just created.
61        partitions = g.list_partitions()
62        if partitions.length != 1 then
63          raise "error: expected a single partition from list-partitions"
64        end
65
66        # Create a filesystem on the partition.
67        g.mkfs("ext4", partitions[0])
68
69        # Now mount the filesystem so that we can add files.
70        g.mount(partitions[0], "/")
71
72        # Create some files and directories.
73        g.touch("/empty")
74        message = "Hello, world\n"
75        g.write("/hello", message)
76        g.mkdir("/foo")
77
78        # This one uploads the local file /etc/resolv.conf into
79        # the disk image.
80        g.upload("/etc/resolv.conf", "/foo/resolv.conf")
81
82        # Because we wrote to the disk and we want to detect write
83        # errors, call g.shutdown.  You don't need to do this:
84        # g.close will do it implicitly.
85        g.shutdown()
86
87        # Note also that handles are automatically closed if they are
88        # reaped by the garbage collector.  You only need to call close
89        # if you want to close the handle right away.
90        g.close()
91

EXAMPLE 2: INSPECT A VIRTUAL MACHINE DISK IMAGE

93        # Example showing how to inspect a virtual machine disk.
94
95        require 'guestfs'
96
97        if ARGV.length == 0
98          puts "usage: inspect_vm disk.img"
99          exit 1
100        end
101        disk = ARGV[0]
102
103        g = Guestfs::Guestfs.new()
104
105        # Attach the disk image read-only to libguestfs.
106        g.add_drive_opts(disk, :readonly => 1)
107
108        # Run the libguestfs back-end.
109        g.launch()
110
111        # Ask libguestfs to inspect for operating systems.
112        roots = g.inspect_os()
113        if roots.length == 0
114          puts "inspect_vm: no operating systems found"
115          exit 1
116        end
117
118        for root in roots do
119          printf("Root device: %s\n", root)
120
121          # Print basic information about the operating system.
122          printf("  Product name: %s\n", g.inspect_get_product_name(root))
123          printf("  Version:      %d.%d\n",
124                 g.inspect_get_major_version(root),
125                 g.inspect_get_minor_version(root))
126          printf("  Type:         %s\n", g.inspect_get_type(root))
127          printf("  Distro:       %s\n", g.inspect_get_distro(root))
128
129          # Mount up the disks, like guestfish -i.
130          #
131          # Sort keys by length, shortest first, so that we end up
132          # mounting the filesystems in the correct order.
133          mps = g.inspect_get_mountpoints(root)
134          mps = mps.sort {|a,b| a[0].length <=> b[0].length}
135          for mp in mps do
136            begin
137              g.mount_ro(mp[1], mp[0])
138            rescue Guestfs::Error => msg
139              printf("%s (ignored)\n", msg)
140            end
141          end
142
143          # If /etc/issue.net file exists, print up to 3 lines.
144          filename = "/etc/issue.net"
145          if g.is_file filename then
146            printf("--- %s ---\n", filename)
147            lines = g.head_n(3, filename)
148            for line in lines do
149              puts line
150            end
151          end
152
153          # Unmount everything.
154          g.umount_all()
155        end
156

SEE ALSO

158       guestfs(3), guestfs-examples(3), guestfs-erlang(3), guestfs-java(3),
159       guestfs-lua(3), guestfs-ocaml(3), guestfs-perl(3), guestfs-python(3),
160       guestfs-recipes(1), http://libguestfs.org/.
161

AUTHORS

163       Richard W.M. Jones ("rjones at redhat dot com")
164
166       Copyright (C) 2010-2012 Red Hat Inc.
167

LICENSE

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

BUGS

174       To get a list of bugs against libguestfs, use this link:
175       https://bugzilla.redhat.com/buglist.cgi?component=libguestfs&product=Virtualization+Tools
176
177       To report a new bug against libguestfs, use this link:
178       https://bugzilla.redhat.com/enter_bug.cgi?component=libguestfs&product=Virtualization+Tools
179
180       When reporting a bug, please supply:
181
182       ·   The version of libguestfs.
183
184       ·   Where you got libguestfs (eg. which Linux distro, compiled from
185           source, etc)
186
187       ·   Describe the bug accurately and give a way to reproduce it.
188
189       ·   Run libguestfs-test-tool(1) and paste the complete, unedited output
190           into the bug report.
191
192
193
194libguestfs-1.20.11                2013-08-27                   guestfs-ruby(3)
Impressum