]> err.no Git - util-linux/commitdiff
mkswap: use libblkid to detect PT
authorKarel Zak <kzak@redhat.com>
Fri, 25 Sep 2009 14:27:27 +0000 (16:27 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 29 Sep 2009 11:30:36 +0000 (13:30 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
configure.ac
disk-utils/Makefile.am
disk-utils/mkswap.c

index 52985137c5123fbe1eea0c6138e2d8a1a45c6cf5..b421d0a19539d2afb5373a0ce8abd3c47b22b6aa 100644 (file)
@@ -353,6 +353,7 @@ else
   # internal library
   AC_DEFINE(HAVE_BLKID_H, 1, [Define to 1 if you have the <blkid.h> header file.])
   AC_DEFINE(HAVE_BLKID_EVALUATE_TAG, 1, [Define to 1 if you have the blkid_evaluate_tag().])
+  AC_DEFINE(HAVE_BLKID_PROBE_ENABLE_PARTITIONS, 1, [Define to 1 if you have the blkid_probe_enable_partitions().])
 fi
 
 if test "x$have_blkid" = xyes; then
index 6bd22bc5168580326ba0bf724ab299192d4162a8..852ae8a0bcb33f943cef215397dabffaf49802a4 100644 (file)
@@ -13,7 +13,7 @@ fsck_minix_SOURCES = fsck.minix.c minix.h
 mkfs_minix_SOURCES = mkfs.minix.c minix.h $(utils_common)
 mkfs_bfs_SOURCES = mkfs.bfs.c $(utils_common)
 
-mkswap_SOURCES = mkswap.c $(utils_common) ../lib/wholedisk.c ../lib/pttype.c
+mkswap_SOURCES = mkswap.c $(utils_common) ../lib/wholedisk.c
 mkswap_LDADD =
 mkswap_CFLAGS =
 
@@ -59,6 +59,12 @@ mkswap_CFLAGS += $(UUID_CFLAGS)
 endif
 endif
 
+if BUILD_LIBBLKID
+# only in-tree libblkid has partitions parsing support
+mkswap_LDADD += $(ul_libblkid_la)
+mkswap_CFLAGS += -I$(ul_libblkid_srcdir)
+endif
+
 if HAVE_SELINUX
 mkswap_LDADD += -lselinux
 endif
index 9feb2111d3430e5cf1968ac07270b839bf3c840a..1205acdb83d73442a98e8c0894d6e10f2a3f1ad7 100644 (file)
 #include "nls.h"
 #include "blkdev.h"
 #include "pathnames.h"
-#include "pttype.h"
 #include "wholedisk.h"
 
 #ifdef HAVE_LIBUUID
 # ifdef HAVE_UUID_UUID_H
 #  include <uuid/uuid.h>
-#else
-# include <uuid.h>
+# else
+#  include <uuid.h>
 # endif
 #endif
 
+#ifdef HAVE_BLKID_PROBE_ENABLE_PARTITIONS
+# include <blkid.h>
+#endif
+
 static char * program_name = "mkswap";
 static char * device_name = NULL;
 static int DEV = -1;
@@ -391,21 +394,43 @@ write_all(int fd, const void *buf, size_t count) {
 static void
 zap_bootbits(int fd, const char *devname, int force)
 {
-       const char *type = NULL;
+       char *type = NULL;
+       int whole = 0;
        int zap = 1;
 
        if (!force) {
                if (lseek(fd, 0, SEEK_SET) != 0)
                        die(_("unable to rewind swap-device"));
 
-               if (is_whole_disk_fd(fd, devname))
+               if (is_whole_disk_fd(fd, devname)) {
                        /* don't zap bootbits on whole disk -- we know nothing
                         * about bootloaders on the device */
+                       whole = 1;
                        zap = 0;
-
-               else if ((type = get_pt_type_fd(fd)))
-                       /* don't zap partition table */
+               } else {
+#ifdef HAVE_BLKID_PROBE_ENABLE_PARTITIONS
+                       blkid_probe pr = blkid_new_probe();
+                       if (!pr)
+                               die(_("unable to alloc new libblkid probe"));
+                       if (blkid_probe_set_device(pr, fd, 0, 0))
+                               die(_("unable to assign device to liblkid probe"));
+
+                       blkid_probe_enable_partitions(pr, 1);
+                       blkid_probe_enable_superblocks(pr, 0);
+
+                       if (blkid_do_fullprobe(pr) == 0)
+                               blkid_probe_lookup_value(pr, "PTTYPE",
+                                               (const char **) &type, NULL);
+                       if (type) {
+                               type = strdup(type);
+                               zap = 0;
+                       }
+                       blkid_free_probe(pr);
+#else
+                       /* don't zap if compiled without libblkid */
                        zap = 0;
+#endif
+               }
        }
 
        if (zap) {
@@ -424,8 +449,10 @@ zap_bootbits(int fd, const char *devname, int force)
                program_name, devname);
        if (type)
                fprintf(stderr, _("        (%s partition table detected). "), type);
-       else
+       else if (whole)
                fprintf(stderr, _("        on whole disk. "));
+       else
+               fprintf(stderr, _("        (compiled without libblkid). "));
        fprintf(stderr, "Use -f to force.\n");
 }