Index: sbin/swapon/swapon.c =================================================================== --- sbin/swapon/swapon.c +++ sbin/swapon/swapon.c @@ -756,6 +756,24 @@ fd = open(name, O_WRONLY); if (fd < 0) errx(1, "Cannot open %s", name); + + /* + * Hack: see if the file is already in use as a swap device. If so, + * don't attempt to erase its blocks since we may end up clearing + * swapped-out data. Currently there is no surefire way to determine + * whether a file is a swap device other than by attempting to enable it + * as such, and observing EBUSY. + */ + error = swapon(name); + if (error != 0) { + error = errno; + (void)close(fd); + errno = error; + return (-1); + } + if (swapoff(name, SWAPOFF_FORCE) != 0) + err(1, "swapoff(%s)", name); + /* Find the device size. */ if (fstat(fd, &sb) < 0) errx(1, "Cannot stat %s", name);