1guestfs-ruby(3) Virtualization Support guestfs-ruby(3)
2
3
4
6 guestfs-ruby - How to use libguestfs from Ruby
7
9 require 'guestfs'
10 g = Guestfs::Guestfs.new()
11 g.add_drive_opts("disk.img",
12 :readonly => 1, :format => "raw")
13 g.launch()
14
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
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 g.disk_create (output, "raw", 512 * 1024 * 1024)
36
37 # Set the trace flag so that we can see each libguestfs call.
38 g.set_trace(1)
39
40 # Attach the disk image to libguestfs.
41 g.add_drive_opts(output, :format => "raw")
42
43 # Run the libguestfs back-end.
44 g.launch();
45
46 # Get the list of devices. Because we only added one drive
47 # above, we expect that this list should contain a single
48 # element.
49 devices = g.list_devices()
50 if devices.length != 1 then
51 raise "error: expected a single device from list-devices"
52 end
53
54 # Partition the disk as one single MBR partition.
55 g.part_disk(devices[0], "mbr")
56
57 # Get the list of partitions. We expect a single element, which
58 # is the partition we have just created.
59 partitions = g.list_partitions()
60 if partitions.length != 1 then
61 raise "error: expected a single partition from list-partitions"
62 end
63
64 # Create a filesystem on the partition.
65 g.mkfs("ext4", partitions[0])
66
67 # Now mount the filesystem so that we can add files.
68 g.mount(partitions[0], "/")
69
70 # Create some files and directories.
71 g.touch("/empty")
72 message = "Hello, world\n"
73 g.write("/hello", message)
74 g.mkdir("/foo")
75
76 # This one uploads the local file /etc/resolv.conf into
77 # the disk image.
78 g.upload("/etc/resolv.conf", "/foo/resolv.conf")
79
80 # Because we wrote to the disk and we want to detect write
81 # errors, call g.shutdown. You don't need to do this:
82 # g.close will do it implicitly.
83 g.shutdown()
84
85 # Note also that handles are automatically closed if they are
86 # reaped by the garbage collector. You only need to call close
87 # if you want to close the handle right away.
88 g.close()
89
91 # Example showing how to inspect a virtual machine disk.
92
93 require 'guestfs'
94
95 if ARGV.length == 0
96 puts "usage: inspect_vm disk.img"
97 exit 1
98 end
99 disk = ARGV[0]
100
101 g = Guestfs::Guestfs.new()
102
103 # Attach the disk image read-only to libguestfs.
104 g.add_drive_opts(disk, :readonly => 1)
105
106 # Run the libguestfs back-end.
107 g.launch()
108
109 # Ask libguestfs to inspect for operating systems.
110 roots = g.inspect_os()
111 if roots.length == 0
112 puts "inspect_vm: no operating systems found"
113 exit 1
114 end
115
116 for root in roots do
117 printf("Root device: %s\n", root)
118
119 # Print basic information about the operating system.
120 printf(" Product name: %s\n", g.inspect_get_product_name(root))
121 printf(" Version: %d.%d\n",
122 g.inspect_get_major_version(root),
123 g.inspect_get_minor_version(root))
124 printf(" Type: %s\n", g.inspect_get_type(root))
125 printf(" Distro: %s\n", g.inspect_get_distro(root))
126
127 # Mount up the disks, like guestfish -i.
128 #
129 # Sort keys by length, shortest first, so that we end up
130 # mounting the filesystems in the correct order.
131 mps = g.inspect_get_mountpoints(root)
132 mps = mps.sort {|a,b| a[0].length <=> b[0].length}
133 for mp in mps do
134 begin
135 g.mount_ro(mp[1], mp[0])
136 rescue Guestfs::Error => msg
137 printf("%s (ignored)\n", msg)
138 end
139 end
140
141 # If /etc/issue.net file exists, print up to 3 lines.
142 filename = "/etc/issue.net"
143 if g.is_file filename then
144 printf("--- %s ---\n", filename)
145 lines = g.head_n(3, filename)
146 for line in lines do
147 puts line
148 end
149 end
150
151 # Unmount everything.
152 g.umount_all()
153 end
154
156 guestfs(3), guestfs-examples(3), guestfs-erlang(3), guestfs-gobject(3),
157 guestfs-golang(3), guestfs-java(3), guestfs-lua(3), guestfs-ocaml(3),
158 guestfs-perl(3), guestfs-python(3), guestfs-recipes(1),
159 http://libguestfs.org/.
160
162 Richard W.M. Jones ("rjones at redhat dot com")
163
165 Copyright (C) 2010-2012 Red Hat Inc.
166
168 This manual page contains examples which we hope you will use in your
169 programs. The examples may be freely copied, modified and distributed
170 for any purpose without any restrictions.
171
173 To get a list of bugs against libguestfs, use this link:
174 https://bugzilla.redhat.com/buglist.cgi?component=libguestfs&product=Virtualization+Tools
175
176 To report a new bug against libguestfs, use this link:
177 https://bugzilla.redhat.com/enter_bug.cgi?component=libguestfs&product=Virtualization+Tools
178
179 When reporting a bug, please supply:
180
181 · The version of libguestfs.
182
183 · Where you got libguestfs (eg. which Linux distro, compiled from
184 source, etc)
185
186 · Describe the bug accurately and give a way to reproduce it.
187
188 · Run libguestfs-test-tool(1) and paste the complete, unedited output
189 into the bug report.
190
191
192
193libguestfs-1.40.2 2019-02-07 guestfs-ruby(3)