summaryrefslogtreecommitdiffstats
path: root/lua/examples/inspect_vm.lua
blob: 1bee10b306436fee23002e0fd73815b21ac1a50a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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