mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-22 07:03:38 +00:00
examples: Update various examples to use new disk-create API.
This commit is contained in:
@@ -17,26 +17,16 @@ main (int argc, char *argv[])
|
||||
if (g == NULL) {
|
||||
perror ("failed to create libguestfs handle");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Create a raw-format sparse disk image, 512 MB in size. */
|
||||
int fd = open ("disk.img", O_CREAT|O_WRONLY|O_TRUNC|O_NOCTTY, 0666);
|
||||
if (fd == -1) {
|
||||
perror ("disk.img");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (ftruncate (fd, 512 * 1024 * 1024) == -1) {
|
||||
perror ("disk.img: truncate");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (close (fd) == -1) {
|
||||
perror ("disk.img: close");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Set the trace flag so that we can see each libguestfs call. */
|
||||
guestfs_set_trace (g, 1);
|
||||
|
||||
/* Create a raw-format sparse disk image, 512 MB in size. */
|
||||
if (guestfs_disk_create (g, "disk.img", "raw", UINT64_C(512)*1024*1024,
|
||||
-1) == -1)
|
||||
exit (EXIT_FAILURE);
|
||||
|
||||
/* Add the disk image to libguestfs. */
|
||||
if (guestfs_add_drive_opts (g, "disk.img",
|
||||
GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", /* raw format */
|
||||
|
||||
@@ -43,7 +43,7 @@ int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
guestfs_h *g;
|
||||
int fd, r;
|
||||
int r;
|
||||
char tempdir[] = "/tmp/mlXXXXXX";
|
||||
pid_t pid;
|
||||
char *shell, *p;
|
||||
@@ -65,22 +65,6 @@ main (int argc, char *argv[])
|
||||
"Creating and formatting the disk image, please wait a moment ...\n");
|
||||
fflush (stdout);
|
||||
|
||||
/* Create the output disk image: raw sparse. */
|
||||
fd = open (argv[1], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644);
|
||||
if (fd == -1) {
|
||||
perror (argv[1]);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (ftruncate (fd, SIZE_MB * 1024 * 1024) == -1) {
|
||||
perror ("truncate");
|
||||
close (fd);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (close (fd) == -1) {
|
||||
perror ("close");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Guestfs handle. */
|
||||
g = guestfs_create ();
|
||||
if (g == NULL) {
|
||||
@@ -88,6 +72,10 @@ main (int argc, char *argv[])
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Create the output disk image: raw sparse. */
|
||||
if (guestfs_disk_create (g, argv[1], "raw", SIZE_MB * 1024 * 1024, -1) == -1)
|
||||
exit (EXIT_FAILURE);
|
||||
|
||||
/* Create the disk image and format it with a partition and a filesystem. */
|
||||
if (guestfs_add_drive_opts (g, argv[1],
|
||||
GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
|
||||
|
||||
@@ -4,7 +4,6 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"libguestfs.org/guestfs"
|
||||
)
|
||||
|
||||
@@ -18,15 +17,8 @@ func main() {
|
||||
defer g.Close ()
|
||||
|
||||
/* Create a raw-format sparse disk image, 512 MB in size. */
|
||||
f, ferr := os.Create (output)
|
||||
if ferr != nil {
|
||||
panic (fmt.Sprintf ("could not create file: %s: %s",
|
||||
output, ferr))
|
||||
}
|
||||
defer f.Close ()
|
||||
|
||||
if ferr := f.Truncate (512 * 1024 * 1024); ferr != nil {
|
||||
panic (fmt.Sprintf ("could not truncate file: %s", ferr))
|
||||
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. */
|
||||
|
||||
@@ -9,9 +9,7 @@ let () =
|
||||
let g = new Guestfs.guestfs () in
|
||||
|
||||
(* Create a raw-format sparse disk image, 512 MB in size. *)
|
||||
let fd = openfile output [O_WRONLY;O_CREAT;O_TRUNC;O_NOCTTY] 0o666 in
|
||||
ftruncate fd (512 * 1024 * 1024);
|
||||
close fd;
|
||||
g#disk_create output "raw" (Int64.of_int (512 * 1024 * 1024));
|
||||
|
||||
(* Set the trace flag so that we can see each libguestfs call. *)
|
||||
g#set_trace true;
|
||||
|
||||
@@ -10,9 +10,7 @@ my $output = "disk.img";
|
||||
my $g = new Sys::Guestfs ();
|
||||
|
||||
# Create a raw-format sparse disk image, 512 MB in size.
|
||||
open FILE, ">$output" or die "$output: $!";
|
||||
truncate FILE, 512 * 1024 * 1024 or die "$output: truncate: $!";
|
||||
close FILE or die "$output: $!";
|
||||
$g->disk_create ($output, "raw", 512 * 1024 * 1024);
|
||||
|
||||
# Set the trace flag so that we can see each libguestfs call.
|
||||
$g->set_trace (1);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
# Example showing how to create a disk image.
|
||||
|
||||
import os
|
||||
import guestfs
|
||||
|
||||
output = "disk.img"
|
||||
@@ -12,9 +11,7 @@ output = "disk.img"
|
||||
g = guestfs.GuestFS (python_return_dict=True)
|
||||
|
||||
# Create a raw-format sparse disk image, 512 MB in size.
|
||||
f = open (output, "w")
|
||||
f.truncate (512 * 1024 * 1024)
|
||||
f.close ()
|
||||
g.disk_create (output, "raw", 512 * 1024 * 1024);
|
||||
|
||||
# Set the trace flag so that we can see each libguestfs call.
|
||||
g.set_trace (1)
|
||||
|
||||
@@ -7,9 +7,7 @@ output = "disk.img"
|
||||
g = Guestfs::Guestfs.new()
|
||||
|
||||
# Create a raw-format sparse disk image, 512 MB in size.
|
||||
File.open(output, "w") {
|
||||
|f| f.truncate(512 * 1024 * 1024)
|
||||
}
|
||||
g.disk_create (output, "raw", 512 * 1024 * 1024)
|
||||
|
||||
# Set the trace flag so that we can see each libguestfs call.
|
||||
g.set_trace(1)
|
||||
|
||||
Reference in New Issue
Block a user