From d60e6a23a6c8996a23336a33d120fcbee56010f7 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Tue, 28 Jan 2014 15:04:59 +0000 Subject: [PATCH] examples: Update various examples to use new disk-create API. --- examples/create-disk.c | 20 +++++--------------- examples/mount-local.c | 22 +++++----------------- golang/examples/create-disk.go | 12 ++---------- ocaml/examples/create_disk.ml | 4 +--- perl/examples/create_disk.pl | 4 +--- python/examples/create_disk.py | 5 +---- ruby/examples/create_disk.rb | 4 +--- 7 files changed, 16 insertions(+), 55 deletions(-) diff --git a/examples/create-disk.c b/examples/create-disk.c index 620efedd9..4326a45e4 100644 --- a/examples/create-disk.c +++ b/examples/create-disk.c @@ -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 */ diff --git a/examples/mount-local.c b/examples/mount-local.c index 18970d0f4..291cb2680 100644 --- a/examples/mount-local.c +++ b/examples/mount-local.c @@ -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", diff --git a/golang/examples/create-disk.go b/golang/examples/create-disk.go index 336fd6ced..a59d3f45c 100644 --- a/golang/examples/create-disk.go +++ b/golang/examples/create-disk.go @@ -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. */ diff --git a/ocaml/examples/create_disk.ml b/ocaml/examples/create_disk.ml index 73d51e3f3..14eda4703 100644 --- a/ocaml/examples/create_disk.ml +++ b/ocaml/examples/create_disk.ml @@ -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; diff --git a/perl/examples/create_disk.pl b/perl/examples/create_disk.pl index 2186ce73b..0b9fa8db7 100755 --- a/perl/examples/create_disk.pl +++ b/perl/examples/create_disk.pl @@ -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); diff --git a/python/examples/create_disk.py b/python/examples/create_disk.py index 2f57f3ff0..ed42e706b 100644 --- a/python/examples/create_disk.py +++ b/python/examples/create_disk.py @@ -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) diff --git a/ruby/examples/create_disk.rb b/ruby/examples/create_disk.rb index fba85ff7e..00310ded2 100644 --- a/ruby/examples/create_disk.rb +++ b/ruby/examples/create_disk.rb @@ -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)