Пример #1
0
func main() {
	if len(os.Args) < 2 {
		panic("usage: inspect-vm disk.img")
	}
	disk := os.Args[1]

	g, errno := guestfs.Create()
	if errno != nil {
		panic(fmt.Sprintf("could not create handle: %s", errno))
	}

	/* Attach the disk image read-only to libguestfs. */
	optargs := guestfs.OptargsAdd_drive{
		Format_is_set:   true,
		Format:          "raw",
		Readonly_is_set: true,
		Readonly:        true,
	}
	if err := g.Add_drive(disk, &optargs); err != nil {
		panic(err)
	}

	/* Run the libguestfs back-end. */
	if err := g.Launch(); err != nil {
		panic(err)
	}

	/* Ask libguestfs to inspect for operating systems. */
	roots, err := g.Inspect_os()
	if err != nil {
		panic(err)
	}
	if len(roots) == 0 {
		panic("inspect-vm: no operating systems found")
	}

	for _, root := range roots {
		fmt.Printf("Root device: %s\n", root)

		/* Print basic information about the operating system. */
		s, _ := g.Inspect_get_product_name(root)
		fmt.Printf("  Product name: %s\n", s)
		major, _ := g.Inspect_get_major_version(root)
		minor, _ := g.Inspect_get_minor_version(root)
		fmt.Printf("  Version:      %d.%d\n", major, minor)
		s, _ = g.Inspect_get_type(root)
		fmt.Printf("  Type:         %s\n", s)
		s, _ = g.Inspect_get_distro(root)
		fmt.Printf("  Distro:       %s\n", s)

		/* XXX Incomplete example.  Sorting the keys by length
		 * is unnecessarily hard in golang.
		 */
	}
}
Пример #2
0
func (d *Driver) generateBlankDiskImage(count int64) error {
	output := d.ResolveStorePath(d.MachineName + ".img")

	g, errno := guestfs.Create()
	if errno != nil {
		panic(errno)
	}
	defer g.Close()

	/* Set $LIBGUESTFS_PATH(libguestfs appliance path) to root user */
	p := toPtr("/usr/local/lib/guestfs")
	g.Set_path(p)

	/* Set the trace flag so that we can see each libguestfs call. */
	if log.IsDebug == true {
		g.Set_trace(true)
	}

	/* Create the disk image to libguestfs. */
	optargsDiskCreate := guestfs.OptargsDisk_create{
		Backingfile_is_set:   false,
		Backingformat_is_set: false,
		Preallocation_is_set: false,
		Compat_is_set:        false,
		Clustersize_is_set:   false,
	}

	/* Create a raw-format sparse disk image, d.DiskSize MB in size. */
	if err := g.Disk_create(output, "raw", int64(d.DiskSize*1024*1024), &optargsDiskCreate); err != nil {
		panic(err)
	}

	/* Attach the disk image to libguestfs. */
	optargsAdd_drive := guestfs.OptargsAdd_drive{
		Format_is_set:   true,
		Format:          "raw",
		Readonly_is_set: true,
		Readonly:        false,
	}

	if err := g.Add_drive(output, &optargsAdd_drive); err != nil {
		panic(err)
	}

	/* Run the libguestfs back-end. */
	if err := g.Launch(); err != nil {
		panic(err)
	}

	/* Get the list of devices.  Because we only added one drive
	 * above, we expect that this list should contain a single
	 * element.
	 */
	devices, err := g.List_devices()
	if err != nil {
		panic(err)
	}
	if len(devices) != 1 {
		panic("expected a single device from list-devices")
	}

	/* Partition the disk as one single MBR partition. */
	err = g.Part_disk(devices[0], "mbr")
	if err != nil {
		panic(err)
	}

	/* Get the list of partitions.  We expect a single element, which
	 * is the partition we have just created.
	 */
	partitions, err := g.List_partitions()
	if err != nil {
		panic(err)
	}
	if len(partitions) != 1 {
		panic("expected a single partition from list-partitions")
	}

	/* Create a filesystem on the partition. */
	err = g.Mkfs("ext4", partitions[0], nil)
	if err != nil {
		panic(err)
	}

	/* Now mount the filesystem so that we can add files. */
	err = g.Mount(partitions[0], "/")
	if err != nil {
		panic(err)
	}

	/* Mkdir -p place of userdata.tar */
	err = g.Mkdir_p("/var/lib/boot2docker")
	if err != nil {
		panic(err)
	}

	/* Uploads the local userdata.tar file into /var/lib/boot2docker */
	err = g.Upload(d.ResolveStorePath("userdata.tar"), "/var/lib/boot2docker/userdata.tar")
	if err != nil {
		panic(err)
	}

	/* Because we wrote to the disk and we want to detect write
	 * errors, call g:shutdown.  You don't need to do this:
	 * g.Close will do it implicitly.
	 */
	if err = g.Shutdown(); err != nil {
		panic(fmt.Sprintf("write to disk failed: %s", err))
	}

	return nil
}
Пример #3
0
func main() {
	output := "disk.img"

	g, errno := guestfs.Create()
	if errno != nil {
		panic(errno)
	}
	defer g.Close()

	/* Create a raw-format sparse disk image, 512 MB in size. */
	if err := g.Disk_create(output, "raw", 512*1024*1024); err != nil {
		panic(err)
	}

	/* Set the trace flag so that we can see each libguestfs call. */
	g.Set_trace(true)

	/* Attach the disk image to libguestfs. */
	optargs := guestfs.OptargsAdd_drive{
		Format_is_set:   true,
		Format:          "raw",
		Readonly_is_set: true,
		Readonly:        false,
	}
	if err := g.Add_drive(output, &optargs); err != nil {
		panic(err)
	}

	/* Run the libguestfs back-end. */
	if err := g.Launch(); err != nil {
		panic(err)
	}

	/* Get the list of devices.  Because we only added one drive
	 * above, we expect that this list should contain a single
	 * element.
	 */
	devices, err := g.List_devices()
	if err != nil {
		panic(err)
	}
	if len(devices) != 1 {
		panic("expected a single device from list-devices")
	}

	/* Partition the disk as one single MBR partition. */
	err = g.Part_disk(devices[0], "mbr")
	if err != nil {
		panic(err)
	}

	/* Get the list of partitions.  We expect a single element, which
	 * is the partition we have just created.
	 */
	partitions, err := g.List_partitions()
	if err != nil {
		panic(err)
	}
	if len(partitions) != 1 {
		panic("expected a single partition from list-partitions")
	}

	/* Create a filesystem on the partition. */
	err = g.Mkfs("ext4", partitions[0], nil)
	if err != nil {
		panic(err)
	}

	/* Now mount the filesystem so that we can add files. */
	err = g.Mount(partitions[0], "/")
	if err != nil {
		panic(err)
	}

	/* Create some files and directories. */
	err = g.Touch("/empty")
	if err != nil {
		panic(err)
	}
	message := []byte("Hello, world\n")
	err = g.Write("/hello", message)
	if err != nil {
		panic(err)
	}
	err = g.Mkdir("/foo")
	if err != nil {
		panic(err)
	}

	/* This one uploads the local file /etc/resolv.conf into
	 * the disk image.
	 */
	err = g.Upload("/etc/resolv.conf", "/foo/resolv.conf")
	if err != nil {
		panic(err)
	}

	/* Because we wrote to the disk and we want to detect write
	 * errors, call g:shutdown.  You don't need to do this:
	 * g.Close will do it implicitly.
	 */
	if err = g.Shutdown(); err != nil {
		panic(fmt.Sprintf("write to disk failed: %s", err))
	}
}