From nobody Tue Apr 23 21:50:14 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VPG4l06WWz5J8hK; Tue, 23 Apr 2024 21:50:15 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VPG4k6llTz4y91; Tue, 23 Apr 2024 21:50:14 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713909014; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ZnEQbRYB8LDmv7+uRBGeXKhNJBYoP6K2VYUfk6Acrkc=; b=mbXLsMI6d4paitzsi7u1WbHfA+icBRoadcWw+m0yb8P1bYlIZMDOtAxnknVqFb8ifOSXPO f0nB5rL541P01NOTZBy2UXrQufwQveDXqrR/VWzbwLuV5IIAoKCEKJmtiqiAIsACl1rMs0 0voCGBWCFE3jPsIqgRzheeWsHHQyZ9ogzYi1SpDNFhw5GNXg0yGsoDOzoHs6lHoKvKYx+H c1G4t9l0hTVRXO4gRXvH1jWQHFvqCjLS/HowX8C9EN2Ru8REJlvstJd3r32zWDB5CvWn1A cuAJsPAgRNge+sSgFOqyCBdAieeRda/gzD+UrBZQWjljb+fbRTpUUxFQeat1WA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1713909014; a=rsa-sha256; cv=none; b=lYWz53Lk2raRsVi+9bU0XMBH6dhxgTxTVwqtbWw9ivH7wclE3f+F8HkE9a/dz3W3kO6IWR aQACebTR81ghJD0HnWPgA+kAiB5/vv9xo/GpkYmis0GrT7fmydgTo+cHz5SA49AvNGx5p4 XighCfzaHlMsHmgznySMB/BXcR9wDN9/CXi2jMXp/7n5HEEAmfj2/kZtDuR9nkdvh6WIHg evFZcXk9s7vDSwgZw3xOMyc2wc0V2xSQ0VlPIxhexoXpq6y2jO5tV8DeA3QGVAI4HQy0Ex NlsR9DrmpvGyTyle7qjTFhXOMRlMSSZOQ/mYAGxdiBDdIE0LwtCdPfH2Z54W6Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713909014; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ZnEQbRYB8LDmv7+uRBGeXKhNJBYoP6K2VYUfk6Acrkc=; b=MvftF+LwsKa+zouDJvUznxZdCG5ihxwfEOXjsZICvDUCzupUgbkTmZFlZzdAbxP5CPOLNx 4g54041J1Oeox6GklZIDpTEKsiQdDZJ6Y0Ply0cL2kEcf99E3h1ZrgjR9bODihpgTSUl/k DMGROgK2RByBMNv5NKJVCXIxQ/AWdbrVGdXLlDUnA0UChBW5l0/wYZD/Nvs6D1iCjJIzor oMy1aeg4/hA8MHhjqZWUJ6ejunyFb5LEg4oKEIuKO2nOFJLSG+sftQIaC/fPuIiMoCPjBa zY1CdWTD3fvw/5WkKPAbnlMskG+CoywtB5804hQ4kJM6O91BAKTEP4Ze3kJsLA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VPG4k616tzntS; Tue, 23 Apr 2024 21:50:14 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 43NLoEPJ087801; Tue, 23 Apr 2024 21:50:14 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43NLoEsI087796; Tue, 23 Apr 2024 21:50:14 GMT (envelope-from git) Date: Tue, 23 Apr 2024 21:50:14 GMT Message-Id: <202404232150.43NLoEsI087796@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: cf04a7775a4e - main - swapon: Do not overwrite Linux swap header List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: cf04a7775a4e8ff6fd28c768be9daa3d83dd382e Auto-Submitted: auto-generated The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=cf04a7775a4e8ff6fd28c768be9daa3d83dd382e commit cf04a7775a4e8ff6fd28c768be9daa3d83dd382e Author: Ricardo Branco AuthorDate: 2024-04-23 21:47:56 +0000 Commit: Warner Losh CommitDate: 2024-04-23 21:48:01 +0000 swapon: Do not overwrite Linux swap header Reviewed by: imp, jhb Pull Request: https://github.com/freebsd/freebsd-src/pull/1084 --- sbin/swapon/Makefile | 2 ++ sbin/swapon/extern.h | 5 ++++ sbin/swapon/swaplinux.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ sbin/swapon/swapon.c | 54 +++++++++++++++++++++++++++++++++++++++- 4 files changed, 126 insertions(+), 1 deletion(-) diff --git a/sbin/swapon/Makefile b/sbin/swapon/Makefile index 27808aed5857..930e938e5b05 100644 --- a/sbin/swapon/Makefile +++ b/sbin/swapon/Makefile @@ -6,6 +6,8 @@ LINKS= ${BINDIR}/swapon ${BINDIR}/swapoff LINKS+= ${BINDIR}/swapon ${BINDIR}/swapctl MLINKS= swapon.8 swapoff.8 MLINKS+=swapon.8 swapctl.8 +SRCS= swapon.c swaplinux.c +HDRS+= extern.h LIBADD= util diff --git a/sbin/swapon/extern.h b/sbin/swapon/extern.h new file mode 100644 index 000000000000..cb33dbbee953 --- /dev/null +++ b/sbin/swapon/extern.h @@ -0,0 +1,5 @@ +/*- + * SPDX-License-Identifier: BSD-3-Clause + */ + +int is_linux_swap(const char *); diff --git a/sbin/swapon/swaplinux.c b/sbin/swapon/swaplinux.c new file mode 100644 index 000000000000..d01a94345e68 --- /dev/null +++ b/sbin/swapon/swaplinux.c @@ -0,0 +1,66 @@ +/*- + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include +#include + +#include "extern.h" + +/* + * Definitions and structure taken from + * https://github.com/util-linux/util-linux/blob/master/include/swapheader.h + */ + +#define SWAP_VERSION 1 +#define SWAP_UUID_LENGTH 16 +#define SWAP_LABEL_LENGTH 16 +#define SWAP_SIGNATURE "SWAPSPACE2" +#define SWAP_SIGNATURE_SZ (sizeof(SWAP_SIGNATURE) - 1) + +struct swap_header_v1_2 { + char bootbits[1024]; /* Space for disklabel etc. */ + uint32_t version; + uint32_t last_page; + uint32_t nr_badpages; + unsigned char uuid[SWAP_UUID_LENGTH]; + char volume_name[SWAP_LABEL_LENGTH]; + uint32_t padding[117]; + uint32_t badpages[1]; +}; + +typedef union { + struct swap_header_v1_2 header; + struct { + uint8_t reserved[4096 - SWAP_SIGNATURE_SZ]; + char signature[SWAP_SIGNATURE_SZ]; + } tail; +} swhdr_t; + +#define sw_version header.version +#define sw_volume_name header.volume_name +#define sw_signature tail.signature + +int +is_linux_swap(const char *name) +{ + uint8_t buf[4096]; + swhdr_t *hdr = (swhdr_t *) buf; + int fd; + + fd = open(name, O_RDONLY); + if (fd == -1) + return (-1); + + if (read(fd, buf, 4096) != 4096) { + close(fd); + return (-1); + } + close(fd); + + return (hdr->sw_version == SWAP_VERSION && + !memcmp(hdr->sw_signature, SWAP_SIGNATURE, SWAP_SIGNATURE_SZ)); +} diff --git a/sbin/swapon/swapon.c b/sbin/swapon/swapon.c index 26a7dc22654a..0f2ad1f5e213 100644 --- a/sbin/swapon/swapon.c +++ b/sbin/swapon/swapon.c @@ -54,10 +54,15 @@ #include #include +#include "extern.h" + +#define _PATH_GNOP "/sbin/gnop" + static void usage(void) __dead2; static const char *swap_on_off(const char *, int, char *); static const char *swap_on_off_gbde(const char *, int); static const char *swap_on_off_geli(const char *, char *, int); +static const char *swap_on_off_linux(const char *, int); static const char *swap_on_off_md(const char *, char *, int); static const char *swap_on_off_sfile(const char *, int); static void swaplist(int, int, int); @@ -250,8 +255,13 @@ swap_on_off(const char *name, int doingall, char *mntops) return (swap_on_off_geli(name, mntops, doingall)); } - /* Swap on special file. */ free(basebuf); + + /* Linux swap */ + if (is_linux_swap(name)) + return (swap_on_off_linux(name, doingall)); + + /* Swap on special file. */ return (swap_on_off_sfile(name, doingall)); } @@ -466,6 +476,48 @@ swap_on_off_geli(const char *name, char *mntops, int doingall) return (swap_on_off_sfile(name, doingall)); } +static const char * +swap_on_off_linux(const char *name, int doingall) +{ + const char *ret; + char *nopname; + size_t nopnamelen; + int error; + + if (which_prog == SWAPON) { + /* Skip the header for Linux swap partitions */ + error = run_cmd(NULL, "%s create -o 4096 %s", _PATH_GNOP, + name); + if (error) { + warnx("gnop (create) error: %s", name); + return (NULL); + } + } + + /* Append ".nop" to name */ + nopnamelen = strlen(name) + sizeof(".nop"); + nopname = (char *) malloc(nopnamelen); + if (nopname == NULL) + err(1, "malloc()"); + (void)strlcpy(nopname, name, nopnamelen); + (void)strlcat(nopname, ".nop", nopnamelen); + + ret = swap_on_off_sfile(nopname, doingall); + + if (which_prog == SWAPOFF) { + error = run_cmd(NULL, "%s destroy %s", _PATH_GNOP, nopname); + if (error) { + warnx("gnop (destroy) error: %s", name); + free(nopname); + return (NULL); + } + } + + free(nopname); + + return (ret); +} + static const char * swap_on_off_md(const char *name, char *mntops, int doingall) {