daemon: debug segv correct use of dereferencing NULL.
[libguestfs.git] / ruby / examples / inspect_vm.rb
1 # Example showing how to inspect a virtual machine disk.
2
3 require 'guestfs'
4
5 if ARGV.length == 0
6   puts "usage: inspect_vm disk.img"
7   exit 1
8 end
9 disk = ARGV[0]
10
11 g = Guestfs::Guestfs.new()
12
13 # Attach the disk image read-only to libguestfs.
14 g.add_drive_opts(disk, :readonly => 1)
15
16 # Run the libguestfs back-end.
17 g.launch()
18
19 # Ask libguestfs to inspect for operating systems.
20 roots = g.inspect_os()
21 if roots.length == 0
22   puts "inspect_vm: no operating systems found"
23   exit 1
24 end
25
26 for root in roots do
27   printf("Root device: %s\n", root)
28
29   # Print basic information about the operating system.
30   printf("  Product name: %s\n", g.inspect_get_product_name(root))
31   printf("  Version:      %d.%d\n",
32          g.inspect_get_major_version(root),
33          g.inspect_get_minor_version(root))
34   printf("  Type:         %s\n", g.inspect_get_type(root))
35   printf("  Distro:       %s\n", g.inspect_get_distro(root))
36
37   # Mount up the disks, like guestfish -i.
38   #
39   # Sort keys by length, shortest first, so that we end up
40   # mounting the filesystems in the correct order.
41   mps = g.inspect_get_mountpoints(root)
42   mps = mps.sort {|a,b| a[0].length <=> b[0].length}
43   for mp in mps do
44     begin
45       g.mount_ro(mp[1], mp[0])
46     rescue Guestfs::Error => msg
47       printf("%s (ignored)\n", msg)
48     end
49   end
50
51   # If /etc/issue.net file exists, print up to 3 lines.
52   filename = "/etc/issue.net"
53   if g.is_file filename then
54     printf("--- %s ---\n", filename)
55     lines = g.head_n(3, filename)
56     for line in lines do
57       puts line
58     end
59   end
60
61   # Unmount everything.
62   g.umount_all()
63 end