summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2009-07-31 11:55:38 +0100
committerRichard W.M. Jones <rjones@redhat.com>2009-07-31 11:55:38 +0100
commit77b2275dfcebce16ceea17ddf77a7f9d0a41c082 (patch)
tree5be0875ed858cd522a2d5b0f700986ffb9e1cf7c
parent0e6e32025885286e2de39efca34ced2aaf26b3a3 (diff)
downloadlibguestfs-77b2275dfcebce16ceea17ddf77a7f9d0a41c082.tar.gz
libguestfs-77b2275dfcebce16ceea17ddf77a7f9d0a41c082.tar.xz
libguestfs-77b2275dfcebce16ceea17ddf77a7f9d0a41c082.zip
New commands: swapon-*, swapoff-*, mkswap-file.
swapon-device swapoff-device swapon-file swapoff-file swapon-label swapoff-label swapon-uuid swapoff-uuid mkswap-file
-rw-r--r--TODO5
-rw-r--r--capitests/Makefile.am1
-rw-r--r--daemon/swap.c130
-rw-r--r--src/MAX_PROC_NR2
-rwxr-xr-xsrc/generator.ml96
5 files changed, 225 insertions, 9 deletions
diff --git a/TODO b/TODO
index 0326ad9d..28b884f0 100644
--- a/TODO
+++ b/TODO
@@ -172,11 +172,6 @@ Ideas for extra commands
pivot_root
fts(3) / ftw(3)
-Swap space
-----------
-
-Allow swap space from the guest to be used. Is it a good idea?
-
Other initrd-* commands
-----------------------
diff --git a/capitests/Makefile.am b/capitests/Makefile.am
index bb931e6a..71928ec1 100644
--- a/capitests/Makefile.am
+++ b/capitests/Makefile.am
@@ -35,6 +35,7 @@ TESTS_ENVIRONMENT = \
SKIP_TEST_NTFS_3G_PROBE=$(shell test -x ../initramfs/bin/ntfs-3g.probe || echo 1) \
SKIP_TEST_CHECKSUM_8=$(shell if test `find ../initramfs -name squashfs.ko | wc -l` -eq 0; then echo 1; fi) \
SKIP_TEST_MKSWAP_U=$(shell r=1; if test -x ../initramfs/sbin/mkswap; then if ../initramfs/sbin/mkswap --help 2>&1 | grep -sq -- -U; then r=0; fi; fi; echo $$r) \
+ SKIP_TEST_SWAPON_UUID=$$SKIP_TEST_MKSWAP_U \
SKIP_TEST_SCRUB_FILE=$(shell test -x ../initramfs/usr/bin/scrub || echo 1) \
SKIP_TEST_SCRUB_DEVICE=$(shell test -x ../initramfs/usr/bin/scrub || echo 1) \
$(VG)
diff --git a/daemon/swap.c b/daemon/swap.c
index faed7f68..db93e573 100644
--- a/daemon/swap.c
+++ b/daemon/swap.c
@@ -33,8 +33,6 @@ mkswap (char *device, const char *flag, const char *value)
char *err;
int r;
- IS_DEVICE (device, -1);
-
if (!flag)
r = command (NULL, &err, "/sbin/mkswap", device, NULL);
else
@@ -54,17 +52,145 @@ mkswap (char *device, const char *flag, const char *value)
int
do_mkswap (char *device)
{
+ IS_DEVICE (device, -1);
+
return mkswap (device, NULL, NULL);
}
int
do_mkswap_L (char *label, char *device)
{
+ IS_DEVICE (device, -1);
+
return mkswap (device, "-L", label);
}
int
do_mkswap_U (char *uuid, char *device)
{
+ IS_DEVICE (device, -1);
+
return mkswap (device, "-U", uuid);
}
+
+int
+do_mkswap_file (char *path)
+{
+ char *buf;
+ int r;
+
+ NEED_ROOT (-1);
+ ABS_PATH (path, -1);
+
+ buf = sysroot_path (path);
+ if (!buf) {
+ reply_with_perror ("malloc");
+ return -1;
+ }
+
+ r = mkswap (buf, NULL, NULL);
+ free (buf);
+ return r;
+}
+
+static int
+swaponoff (const char *cmd, const char *flag, const char *value)
+{
+ char *err;
+ int r;
+
+ if (!flag)
+ r = command (NULL, &err, cmd, value, NULL);
+ else
+ r = command (NULL, &err, cmd, flag, value, NULL);
+
+ if (r == -1) {
+ reply_with_error ("%s: %s: %s", cmd, value, err);
+ free (err);
+ return -1;
+ }
+
+ free (err);
+
+ return 0;
+}
+
+int
+do_swapon_device (char *device)
+{
+ IS_DEVICE (device, -1);
+
+ return swaponoff ("/sbin/swapon", NULL, device);
+}
+
+int
+do_swapoff_device (char *device)
+{
+ IS_DEVICE (device, -1);
+
+ return swaponoff ("/sbin/swapoff", NULL, device);
+}
+
+int
+do_swapon_file (char *path)
+{
+ char *buf;
+ int r;
+
+ NEED_ROOT (-1);
+ ABS_PATH (path, -1);
+
+ buf = sysroot_path (path);
+ if (!buf) {
+ reply_with_perror ("malloc");
+ return -1;
+ }
+
+ r = swaponoff ("/sbin/swapon", NULL, buf);
+ free (buf);
+ return r;
+}
+
+int
+do_swapoff_file (char *path)
+{
+ char *buf;
+ int r;
+
+ NEED_ROOT (-1);
+ ABS_PATH (path, -1);
+
+ buf = sysroot_path (path);
+ if (!buf) {
+ reply_with_perror ("malloc");
+ return -1;
+ }
+
+ r = swaponoff ("/sbin/swapoff", NULL, buf);
+ free (buf);
+ return r;
+}
+
+int
+do_swapon_label (char *label)
+{
+ return swaponoff ("/sbin/swapon", "-L", label);
+}
+
+int
+do_swapoff_label (char *label)
+{
+ return swaponoff ("/sbin/swapoff", "-L", label);
+}
+
+int
+do_swapon_uuid (char *uuid)
+{
+ return swaponoff ("/sbin/swapon", "-U", uuid);
+}
+
+int
+do_swapoff_uuid (char *uuid)
+{
+ return swaponoff ("/sbin/swapoff", "-U", uuid);
+}
diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR
index fb402ef6..f84d24e5 100644
--- a/src/MAX_PROC_NR
+++ b/src/MAX_PROC_NR
@@ -1 +1 @@
-169
+178
diff --git a/src/generator.ml b/src/generator.ml
index e30f5ebf..b0b3f066 100755
--- a/src/generator.ml
+++ b/src/generator.ml
@@ -2757,7 +2757,11 @@ Create a swap partition on C<device>.");
["mkswap_L"; "hello"; "/dev/sda1"]])],
"create a swap partition with a label",
"\
-Create a swap partition on C<device> with label C<label>.");
+Create a swap partition on C<device> with label C<label>.
+
+Note that you cannot attach a swap label to a block device
+(eg. C</dev/sda>), just to a partition. This appears to be
+a limitation of the kernel or swap tools.");
("mkswap_U", (RErr, [String "uuid"; String "device"]), 132, [],
[InitEmpty, Always, TestRun (
@@ -3201,6 +3205,96 @@ Do not confuse this with the guestfish-specific
C<alloc> command which allocates a file in the host and
attaches it as a device.");
+ ("swapon_device", (RErr, [String "device"]), 170, [],
+ [InitNone, Always, TestRun (
+ [["mkswap"; "/dev/sdb"];
+ ["swapon_device"; "/dev/sdb"];
+ ["swapoff_device"; "/dev/sdb"]])],
+ "enable swap on device",
+ "\
+This command enables the libguestfs appliance to use the
+swap device or partition named C<device>. The increased
+memory is made available for all commands, for example
+those run using C<guestfs_command> or C<guestfs_sh>.
+
+Note that you should not swap to existing guest swap
+partitions unless you know what you are doing. They may
+contain hibernation information, or other information that
+the guest doesn't want you to trash. You also risk leaking
+information about the host to the guest this way. Instead,
+attach a new host device to the guest and swap on that.");
+
+ ("swapoff_device", (RErr, [String "device"]), 171, [],
+ [], (* XXX tested by swapon_device *)
+ "disable swap on device",
+ "\
+This command disables the libguestfs appliance swap
+device or partition named C<device>.
+See C<guestfs_swapon_device>.");
+
+ ("swapon_file", (RErr, [String "file"]), 172, [],
+ [InitBasicFS, Always, TestRun (
+ [["fallocate"; "/swap"; "8388608"];
+ ["mkswap_file"; "/swap"];
+ ["swapon_file"; "/swap"];
+ ["swapoff_file"; "/swap"]])],
+ "enable swap on file",
+ "\
+This command enables swap to a file.
+See C<guestfs_swapon_device> for other notes.");
+
+ ("swapoff_file", (RErr, [String "file"]), 173, [],
+ [], (* XXX tested by swapon_file *)
+ "disable swap on file",
+ "\
+This command disables the libguestfs appliance swap on file.");
+
+ ("swapon_label", (RErr, [String "label"]), 174, [],
+ [InitEmpty, Always, TestRun (
+ [["sfdiskM"; "/dev/sdb"; ","];
+ ["mkswap_L"; "swapit"; "/dev/sdb1"];
+ ["swapon_label"; "swapit"];
+ ["swapoff_label"; "swapit"]])],
+ "enable swap on labelled swap partition",
+ "\
+This command enables swap to a labelled swap partition.
+See C<guestfs_swapon_device> for other notes.");
+
+ ("swapoff_label", (RErr, [String "label"]), 175, [],
+ [], (* XXX tested by swapon_label *)
+ "disable swap on labelled swap partition",
+ "\
+This command disables the libguestfs appliance swap on
+labelled swap partition.");
+
+ ("swapon_uuid", (RErr, [String "uuid"]), 176, [],
+ [InitEmpty, Always, TestRun (
+ [["mkswap_U"; "a3a61220-882b-4f61-89f4-cf24dcc7297d"; "/dev/sdb"];
+ ["swapon_uuid"; "a3a61220-882b-4f61-89f4-cf24dcc7297d"];
+ ["swapoff_uuid"; "a3a61220-882b-4f61-89f4-cf24dcc7297d"]])],
+ "enable swap on swap partition by UUID",
+ "\
+This command enables swap to a swap partition with the given UUID.
+See C<guestfs_swapon_device> for other notes.");
+
+ ("swapoff_uuid", (RErr, [String "uuid"]), 177, [],
+ [], (* XXX tested by swapon_uuid *)
+ "disable swap on swap partition by UUID",
+ "\
+This command disables the libguestfs appliance swap partition
+with the given UUID.");
+
+ ("mkswap_file", (RErr, [String "path"]), 178, [],
+ [InitBasicFS, Always, TestRun (
+ [["fallocate"; "/swap"; "8388608"];
+ ["mkswap_file"; "/swap"]])],
+ "create a swap file",
+ "\
+Create a swap file.
+
+This command just writes a swap file signature to an existing
+file. To create the file itself, use something like C<guestfs_fallocate>.");
+
]
let all_functions = non_daemon_functions @ daemon_functions