summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2012-03-15 15:25:06 +0000
committerRichard W.M. Jones <rjones@redhat.com>2012-03-15 16:15:45 +0000
commit31c26be91f855d661ccdfc3eeb7d4626f4085a12 (patch)
tree2a5dbce90289393e88d1d818077a658d81568a79
parent8c9f6a64be9840a955e481e932f13d005c0de0de (diff)
downloadlibguestfs-31c26be91f855d661ccdfc3eeb7d4626f4085a12.tar.gz
libguestfs-31c26be91f855d661ccdfc3eeb7d4626f4085a12.tar.xz
libguestfs-31c26be91f855d661ccdfc3eeb7d4626f4085a12.zip
sparsify: Support sparsifying Linux swap partitions.
-rw-r--r--sparsify/sparsify.ml29
-rw-r--r--sparsify/virt-sparsify.pod6
2 files changed, 28 insertions, 7 deletions
diff --git a/sparsify/sparsify.ml b/sparsify/sparsify.ml
index 501540c6..bafaee57 100644
--- a/sparsify/sparsify.ml
+++ b/sparsify/sparsify.ml
@@ -99,6 +99,7 @@ read the man page virt-sparsify(1).
*)
if !disks = [] && machine_readable then (
printf "virt-sparsify\n";
+ printf "linux-swap\n";
let g = new G.guestfs () in
g#add_drive_opts "/dev/null";
g#launch ();
@@ -183,7 +184,9 @@ let g =
(* Get the size in bytes of the input disk. *)
let insize = g#blockdev_getsize64 "/dev/sda"
-(* Write zeroes for non-ignored filesystems that we are able to mount. *)
+(* Write zeroes for non-ignored filesystems that we are able to mount,
+ * and selected swap partitions.
+ *)
let () =
let filesystems = g#list_filesystems () in
let filesystems = List.map fst filesystems in
@@ -206,6 +209,30 @@ let () =
printf "Fill free space in %s with zero ...\n%!" fs;
g#zero_free_space "/"
+ ) else (
+ let is_linux_x86_swap =
+ (* Look for the signature for Linux swap on i386.
+ * Location depends on page size, so it definitely won't
+ * work on non-x86 architectures (eg. on PPC, page size is
+ * 64K). Also this avoids hibernated swap space: in those,
+ * the signature is moved to a different location.
+ *)
+ try g#pread_device fs 10 4086L = "SWAPSPACE2"
+ with _ -> false in
+
+ if is_linux_x86_swap then (
+ if not quiet then
+ printf "Clearing Linux swap on %s ...\n%!" fs;
+
+ (* Don't use mkswap. Just preserve the header containing
+ * the label, UUID and swap format version (libguestfs
+ * mkswap may differ from guest's own).
+ *)
+ let header = g#pread_device fs 4096 0L in
+ g#zero_device fs;
+ if g#pwrite_device fs header 0L <> 4096 then
+ error "pwrite: short write restoring swap partition header"
+ )
);
g#umount_all ()
diff --git a/sparsify/virt-sparsify.pod b/sparsify/virt-sparsify.pod
index 0894cbc1..114405cb 100644
--- a/sparsify/virt-sparsify.pod
+++ b/sparsify/virt-sparsify.pod
@@ -61,12 +61,6 @@ Virt-sparsify cannot yet sparsify the space between partitions. Note
that this space is often used for critical items like bootloaders so
it's not really unused.
-=item *
-
-Virt-sparsify does not yet know how to sparsify swapspace. It is not
-safe to do this unless we can be sure there is no hibernation data, so
-at the moment swap partitions are ignored.
-
=back
You may also want to read the manual pages for the associated tools