Modified API: mkswap now takes optional arguments.

Add optional label and uuid arguments, and deprecate mkswap-L and
mkswap-U.

This also adds a call to udev_settle after creating the swap device.
This is an attempt to workaround the following problem seen in Koji
and Rawhide:

libguestfs: trace: mkswap_L "swapit" "/dev/sda1"
libguestfs: send_to_daemon: 72 bytes: 00 00 00 44 | 20 00 f5 f5 | 00 00 00 04 | 00 00 00 83 | 00 00 00 00 | ...
guestfsd: main_loop: proc 210 (part_disk) took 2.28 seconds
guestfsd: main_loop: new request, len 0x44
mkswap -f -L swapit /dev/sda1
libguestfs: recv_from_daemon: 40 bytes: 20 00 f5 f5 | 00 00 00 04 | 00 00 00 83 | 00 00 00 01 | 00 12 37 cf | ...
libguestfs: trace: mkswap_L = 0
libguestfs: trace: swapon_label "swapit"
libguestfs: send_to_daemon: 56 bytes: 00 00 00 34 | 20 00 f5 f5 | 00 00 00 04 | 00 00 00 ae | 00 00 00 00 | ...
guestfsd: main_loop: proc 131 (mkswap_L) took 0.77 seconds
guestfsd: main_loop: new request, len 0x34
swapon -L swapit
swapon: cannot find the device for swapit
guestfsd: error: swapit: swapon: cannot find the device for swapit
libguestfs: recv_from_daemon: 100 bytes: 20 00 f5 f5 | 00 00 00 04 | 00 00 00 ae | 00 00 00 01 | 00 12 37 d0 | ...
libguestfs: trace: swapon_label = -1 (error)
swapon_label: swapit: swapon: cannot find the device for swapit
test_swapon_label_0 FAILED
This commit is contained in:
Richard W.M. Jones
2012-08-28 04:00:52 -04:00
parent c2ac512f12
commit f23a5c468e
4 changed files with 73 additions and 31 deletions

View File

@@ -56,56 +56,71 @@ optgroup_linuxfsuuid_available (void)
return av;
}
static int
mkswap (const char *device, const char *flag, const char *value)
/* Takes optional arguments, consult optargs_bitmask. */
int
do_mkswap (const char *device, const char *label, const char *uuid)
{
char *err;
const size_t MAX_ARGS = 64;
const char *argv[MAX_ARGS];
size_t i = 0;
int r;
char *err;
if (!flag)
r = command (NULL, &err, "mkswap", "-f", device, NULL);
else
r = command (NULL, &err, "mkswap", "-f", flag, value, device, NULL);
ADD_ARG (argv, i, "mkswap");
ADD_ARG (argv, i, "-f");
if (optargs_bitmask & GUESTFS_MKSWAP_LABEL_BITMASK) {
if (strlen (label) > SWAP_LABEL_MAX) {
reply_with_error ("%s: Linux swap labels are limited to %d bytes",
label, SWAP_LABEL_MAX);
return -1;
}
ADD_ARG (argv, i, "-L");
ADD_ARG (argv, i, label);
}
if (optargs_bitmask & GUESTFS_MKSWAP_UUID_BITMASK) {
ADD_ARG (argv, i, "-U");
ADD_ARG (argv, i, uuid);
}
ADD_ARG (argv, i, device);
ADD_ARG (argv, i, NULL);
r = commandv (NULL, &err, argv);
if (r == -1) {
reply_with_error ("%s", err);
reply_with_error ("%s: %s", device, err);
free (err);
return -1;
}
free (err);
return 0;
}
udev_settle ();
int
do_mkswap (const char *device)
{
return mkswap (device, NULL, NULL);
return 0;
}
int
do_mkswap_L (const char *label, const char *device)
{
if (strlen (label) > SWAP_LABEL_MAX) {
reply_with_error ("%s: Linux swap labels are limited to %d bytes",
label, SWAP_LABEL_MAX);
return -1;
}
return mkswap (device, "-L", label);
optargs_bitmask = GUESTFS_MKSWAP_LABEL_BITMASK;
return do_mkswap (device, label, NULL);
}
int
do_mkswap_U (const char *uuid, const char *device)
{
return mkswap (device, "-U", uuid);
optargs_bitmask = GUESTFS_MKSWAP_UUID_BITMASK;
return do_mkswap (device, NULL, uuid);
}
int
do_mkswap_file (const char *path)
{
char *buf;
char *err;
int r;
buf = sysroot_path (path);
@@ -114,8 +129,17 @@ do_mkswap_file (const char *path)
return -1;
}
r = mkswap (buf, NULL, NULL);
r = command (NULL, &err, "mkswap", "-f", buf, NULL);
free (buf);
if (r == -1) {
reply_with_error ("%s: %s", path, err);
free (err);
return -1;
}
free (err);
return r;
}

View File

@@ -4908,21 +4908,35 @@ the command C<mount -o loop file mountpoint>." };
{ defaults with
name = "mkswap";
style = RErr, [Device "device"], [];
style = RErr, [Device "device"], [OString "label"; OString "uuid"];
proc_nr = Some 130;
tests = [
once_had_no_optargs = true;
tests = (let uuid = uuidgen () in [
InitEmpty, Always, TestRun (
[["part_disk"; "/dev/sda"; "mbr"];
["mkswap"; "/dev/sda1"]])
];
["mkswap"; "/dev/sda1"; "NOARG"; "NOARG"]]);
InitEmpty, Always, TestRun (
[["part_disk"; "/dev/sda"; "mbr"];
["mkswap"; "/dev/sda1"; "hello"; "NOARG"]]);
InitEmpty, Always, TestRun (
[["part_disk"; "/dev/sda"; "mbr"];
["mkswap"; "/dev/sda1"; "NOARG"; uuid]]);
InitEmpty, Always, TestRun (
[["part_disk"; "/dev/sda"; "mbr"];
["mkswap"; "/dev/sda1"; "hello"; uuid]])
]);
shortdesc = "create a swap partition";
longdesc = "\
Create a swap partition on C<device>." };
Create a Linux swap partition on C<device>.
The option arguments C<label> and C<uuid> allow you to set the
label and/or UUID of the new swap partition." };
{ defaults with
name = "mkswap_L";
style = RErr, [String "label"; Device "device"], [];
proc_nr = Some 131;
deprecated_by = Some "mkswap";
tests = [
InitEmpty, Always, TestRun (
[["part_disk"; "/dev/sda"; "mbr"];
@@ -4940,6 +4954,7 @@ a limitation of the kernel or swap tools." };
name = "mkswap_U";
style = RErr, [String "uuid"; Device "device"], [];
proc_nr = Some 132;
deprecated_by = Some "mkswap";
optional = Some "linuxfsuuid";
tests =
(let uuid = uuidgen () in [
@@ -5660,7 +5675,7 @@ attaches it as a device." };
proc_nr = Some 170;
tests = [
InitPartition, Always, TestRun (
[["mkswap"; "/dev/sda1"];
[["mkswap"; "/dev/sda1"; "NOARG"; "NOARG"];
["swapon_device"; "/dev/sda1"];
["swapoff_device"; "/dev/sda1"]])
];
@@ -5720,7 +5735,7 @@ This command disables the libguestfs appliance swap on file." };
tests = [
InitEmpty, Always, TestRun (
[["part_disk"; "/dev/sda"; "mbr"];
["mkswap_L"; "swapit"; "/dev/sda1"];
["mkswap"; "/dev/sda1"; "swapit"; "NOARG"];
["swapon_label"; "swapit"];
["swapoff_label"; "swapit"];
["zero"; "/dev/sda"];
@@ -5748,7 +5763,7 @@ labeled swap partition." };
tests =
(let uuid = uuidgen () in [
InitEmpty, Always, TestRun (
[["mkswap_U"; uuid; "/dev/sdc"];
[["mkswap"; "/dev/sdc"; "NOARG"; uuid];
["swapon_uuid"; uuid];
["swapoff_uuid"; uuid]])
]);

View File

@@ -49,6 +49,7 @@ guestfs_gobject_headers= \
include/guestfs-gobject/optargs-umount.h \
include/guestfs-gobject/optargs-tar_in.h \
include/guestfs-gobject/optargs-tar_out.h \
include/guestfs-gobject/optargs-mkswap.h \
include/guestfs-gobject/optargs-grep.h \
include/guestfs-gobject/optargs-mkfs.h \
include/guestfs-gobject/optargs-mount_9p.h \
@@ -104,6 +105,7 @@ guestfs_gobject_sources= \
src/optargs-umount.c \
src/optargs-tar_in.c \
src/optargs-tar_out.c \
src/optargs-mkswap.c \
src/optargs-grep.c \
src/optargs-mkfs.c \
src/optargs-mount_9p.c \

View File

@@ -153,6 +153,7 @@ gobject/src/optargs-internal_test.c
gobject/src/optargs-md_create.c
gobject/src/optargs-mkfs.c
gobject/src/optargs-mkfs_btrfs.c
gobject/src/optargs-mkswap.c
gobject/src/optargs-mount_9p.c
gobject/src/optargs-mount_local.c
gobject/src/optargs-ntfsclone_out.c