File: create-disk.go

package info (click to toggle)
libguestfs 1%3A1.54.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 98,892 kB
  • sloc: ansic: 379,443; ml: 38,771; sh: 10,329; java: 9,631; cs: 6,377; haskell: 5,729; makefile: 5,178; python: 3,821; perl: 2,467; erlang: 2,461; ruby: 349; xml: 275; pascal: 257; javascript: 157; cpp: 10
file content (114 lines) | stat: -rw-r--r-- 2,465 bytes parent folder | download | duplicates (7)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* Example showing how to create a disk image. */

package main

import (
	"fmt"
	"libguestfs.org/guestfs"
)

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))
	}
}