From ff3bed806863d1c2075d0efda70b39ea6af9ecba Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 12 Mar 2009 16:01:59 +0100 Subject: mkswap: zap bootbits /dev/sdb1 originally initialized by cryptsetup: and OLD mkswap: # vol_id /dev/sdb1 | grep TYPE ID_FS_TYPE=swap # blkid -s TYPE /dev/sdb1 /dev/sdb1: TYPE="crypt_LUKS" So, we have two different *valid* signatures on the device now! NEW mkswap: # blkid -s TYPE /dev/sdb1 /dev/sdb1: TYPE="swap" # /lib/udev/vol_id /dev/sdb1 | grep TYPE ID_FS_TYPE=swap the bootbits (first 1024 bytes) was erased. We shouldn't zap disk labels (BSD, SUN, ...) and boot loaders (on whole disk): # mkswap /dev/sdb2 mkswap: /dev/sdb2: warning: don't erase bootbits sectors (BSD partition table detected). Use -f to force. Setting up swapspace version 1, size = 4348 KiB no label, UUID=69d87cef-71ac-4fb0-a689-ce3e930dea17 # mkswap /dev/sdb mkswap: /dev/sdb: warning: don't erase bootbits sectors on whole disk. Use -f to force. Setting up swapspace version 1, size = 8188 KiB no label, UUID=97757ad7-8a84-43d9-bcb4-16fefd93a2ac Signed-off-by: Karel Zak --- disk-utils/mkswap.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'disk-utils/mkswap.c') diff --git a/disk-utils/mkswap.c b/disk-utils/mkswap.c index 6e151af38..4acc73b23 100644 --- a/disk-utils/mkswap.c +++ b/disk-utils/mkswap.c @@ -53,6 +53,8 @@ #include "nls.h" #include "blkdev.h" #include "pathnames.h" +#include "pttype.h" +#include "wholedisk.h" #ifdef HAVE_LIBUUID #include @@ -379,6 +381,47 @@ write_all(int fd, const void *buf, size_t count) { return 0; } +static void +zap_bootbits(int fd, const char *devname, int force) +{ + const char *type = NULL; + 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)) + /* don't zap bootbits on whole disk -- we know nothing + * about bootloaders on the device */ + zap = 0; + + else if ((type = get_pt_type_fd(fd))) + /* don't zap partition table */ + zap = 0; + } + + if (zap) { + char buf[1024]; + + if (lseek(fd, 0, SEEK_SET) != 0) + die(_("unable to rewind swap-device")); + + memset(buf, 0, sizeof(buf)); + if (write_all(fd, buf, sizeof(buf))) + die(_("unable to erase bootbits sectors")); + return; + } + + fprintf(stderr, _("%s: %s: warning: don't erase bootbits sectors\n"), + program_name, devname); + if (type) + fprintf(stderr, _(" (%s partition table detected). "), type); + else + fprintf(stderr, _(" on whole disk. ")); + fprintf(stderr, "Use -f to force.\n"); +} + int main(int argc, char ** argv) { struct stat statbuf; @@ -561,6 +604,8 @@ main(int argc, char ** argv) { if (check) check_blocks(); + zap_bootbits(DEV, device_name, force); + p->version = 1; p->last_page = PAGES-1; p->nr_badpages = badpages; -- cgit v1.2.3-55-g7522