summaryrefslogtreecommitdiffstats
path: root/lua/examples/inspect_vm.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/examples/inspect_vm.lua')
-rw-r--r--lua/examples/inspect_vm.lua62
1 files changed, 62 insertions, 0 deletions
diff --git a/lua/examples/inspect_vm.lua b/lua/examples/inspect_vm.lua
new file mode 100644
index 00000000..1bee10b3
--- /dev/null
+++ b/lua/examples/inspect_vm.lua
@@ -0,0 +1,62 @@
+-- Example showing how to inspect a virtual machine disk.
+
+require "guestfs"
+
+if table.getn (arg) == 1 then
+ disk = arg[1]
+else
+ error ("usage: inspect_vm disk.img")
+end
+
+g = Guestfs.create ()
+
+-- Attach the disk image read-only to libguestfs.
+g:add_drive (disk, { -- format:"raw"
+ readonly = true })
+
+-- Run the libguestfs back-end.
+g:launch ()
+
+-- Ask libguestfs to inspect for operating systems.
+roots = g:inspect_os ()
+if table.getn (roots) == 0 then
+ error ("inspect_vm: no operating systems found")
+end
+
+for _, root in ipairs (roots) do
+ print ("Root device: ", root)
+
+ -- Print basic information about the operating system.
+ 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.
+ --
+ -- Sort keys by length, shortest first, so that we end up
+ -- mounting the filesystems in the correct order.
+ mps = g:inspect_get_mountpoints (root)
+ table.sort (mps,
+ function (a, b)
+ return string.len (a) < string.len (b)
+ end)
+ for mp,dev in pairs (mps) do
+ pcall (function () g:mount_ro (dev, mp) end)
+ end
+
+ -- If /etc/issue.net file exists, print up to 3 lines.
+ filename = "/etc/issue.net"
+ if g:is_file (filename) then
+ print ("--- ", filename, " ---")
+ lines = g:head_n (3, filename)
+ for _, line in ipairs (lines) do
+ print (line)
+ end
+ end
+
+ -- Unmount everything.
+ g:umount_all ()
+end