Bump to version 1.22.1
[platform/upstream/busybox.git] / util-linux / mkswap.c
index 3f736fa..b5d2c49 100644 (file)
@@ -5,6 +5,13 @@
  *
  * Licensed under GPLv2, see file LICENSE in this source tree.
  */
+
+//usage:#define mkswap_trivial_usage
+//usage:       "[-L LBL] BLOCKDEV [KBYTES]"
+//usage:#define mkswap_full_usage "\n\n"
+//usage:       "Prepare BLOCKDEV to be used as swap partition\n"
+//usage:     "\n       -L LBL  Label"
+
 #include "libbb.h"
 
 #if ENABLE_SELINUX
@@ -15,8 +22,7 @@ static void mkswap_selinux_setcontext(int fd, const char *path)
        if (!is_selinux_enabled())
                return;
 
-       if (fstat(fd, &stbuf) < 0)
-               bb_perror_msg_and_die("fstat failed");
+       xfstat(fd, &stbuf, path);
        if (S_ISREG(stbuf.st_mode)) {
                security_context_t newcon;
                security_context_t oldcon = NULL;
@@ -102,7 +108,15 @@ int mkswap_main(int argc UNUSED_PARAM, char **argv)
        printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", len);
        mkswap_selinux_setcontext(fd, argv[0]);
 
-       /* Make a header. hdr is zero-filled so far... */
+       /* hdr is zero-filled so far. Clear the first kbyte, or else
+        * mkswap-ing former FAT partition does NOT erase its signature.
+        *
+        * util-linux-ng 2.17.2 claims to erase it only if it does not see
+        * a partition table and is not run on whole disk. -f forces it.
+        */
+       xwrite(fd, hdr, 1024);
+
+       /* Fill the header. */
        hdr->version = 1;
        hdr->last_page = (uoff_t)len / pagesize;
 
@@ -123,7 +137,6 @@ int mkswap_main(int argc UNUSED_PARAM, char **argv)
 
        /* Write the header.  Sync to disk because some kernel versions check
         * signature on disk (not in cache) during swapon. */
-       xlseek(fd, 1024, SEEK_SET);
        xwrite(fd, hdr, NWORDS * 4);
        xlseek(fd, pagesize - 10, SEEK_SET);
        xwrite(fd, SWAPSPACE2, 10);