From 566f35bc81d992bfec0a41485eea073854f54b0e Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 25 Sep 2009 16:27:27 +0200 Subject: [PATCH] mkswap: use libblkid to detect PT Signed-off-by: Karel Zak --- configure.ac | 1 + disk-utils/Makefile.am | 8 +++++++- disk-utils/mkswap.c | 45 +++++++++++++++++++++++++++++++++--------- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/configure.ac b/configure.ac index 52985137..b421d0a1 100644 --- a/configure.ac +++ b/configure.ac @@ -353,6 +353,7 @@ else # internal library AC_DEFINE(HAVE_BLKID_H, 1, [Define to 1 if you have the 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 diff --git a/disk-utils/Makefile.am b/disk-utils/Makefile.am index 6bd22bc5..852ae8a0 100644 --- a/disk-utils/Makefile.am +++ b/disk-utils/Makefile.am @@ -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 diff --git a/disk-utils/mkswap.c b/disk-utils/mkswap.c index 9feb2111..1205acdb 100644 --- a/disk-utils/mkswap.c +++ b/disk-utils/mkswap.c @@ -53,17 +53,20 @@ #include "nls.h" #include "blkdev.h" #include "pathnames.h" -#include "pttype.h" #include "wholedisk.h" #ifdef HAVE_LIBUUID # ifdef HAVE_UUID_UUID_H # include -#else -# include +# else +# include # endif #endif +#ifdef HAVE_BLKID_PROBE_ENABLE_PARTITIONS +# include +#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"); } -- 2.39.5