[klibc] Add swap support to fstype

David Härdeman david at 2gen.com
Sat Feb 4 17:04:54 PST 2006


This patch adds support for swap detection to fstype (to be applied on 
top of the previous luks patch).

Signed-off-by: David Härdeman <david at 2gen.com>

-- 

 fstype.c  |   43 ++++++++++++++++++++++++++++++-------------
 swap_fs.h |   18 ++++++++++++++++++
 2 files changed, 48 insertions(+), 13 deletions(-)


-------------- next part --------------
diff -Nur klibc-1.2-hack/utils/fstype.c klibc-1.2-swap/utils/fstype.c
--- klibc-1.2-hack/utils/fstype.c	2006-02-04 18:01:24.000000000 +0100
+++ klibc-1.2-swap/utils/fstype.c	2006-02-05 01:57:32.000000000 +0100
@@ -7,7 +7,7 @@
  *  FSSIZE - filesystem size (if known)
  *
  * We currently detect (in order):
- *  gzip, cramfs, romfs, xfs, luks, minix, ext3, ext2, reiserfs, jfs
+ *  gzip, cramfs, romfs, xfs, luks, minix, ext3, ext2, reiserfs, jfs, swap
  *
  * MINIX, ext3 and Reiserfs bits are currently untested.
  */
@@ -20,7 +20,7 @@
 #include <endian.h>
 #include <netinet/in.h>
 #include <sys/vfs.h>
-
+#include <asm/page.h>
 #define cpu_to_be32(x) __cpu_to_be32(x)	/* Needed by romfs_fs.h */
 
 #include "romfs_fs.h"
@@ -51,6 +51,9 @@
 #define BLOCK_SIZE	(1 << BLOCK_SIZE_BITS)
 #define BYTES_TO_BLOCKS(x) (((x) + (BLOCK_SIZE - 1)) >> BLOCK_SIZE_BITS)
 
+/* Swap needs the definition of block size */
+#include "swap_fs.h"
+
 static int fstype(const char *type, unsigned long size)
 {
 	fprintf(stdout, "FSTYPE=%s\nFSSIZE=%lu\n", type, size);
@@ -209,6 +212,19 @@
 	return 0;
 }
 
+static int swap_image(const unsigned char *buf, unsigned long *blocks)
+{
+	const struct swap_super_block *ssb =
+		(const struct swap_super_block *)buf;
+
+	if (!memcmp(ssb->magic, SWAP_MAGIC_1, SWAP_MAGIC_L) ||
+	    !memcmp(ssb->magic, SWAP_MAGIC_2, SWAP_MAGIC_L)) {
+		*blocks = 0;
+		return 1;
+	}
+	return 0;
+}
+
 struct imagetype {
 	off_t		block;
 	const char	name[12];
@@ -216,17 +232,18 @@
 };
 
 static struct imagetype images[] = {
-	{ 0,	"gzip",		gzip_image	},
-	{ 0,	"cramfs",	cramfs_image	},
-	{ 0,	"romfs",	romfs_image	},
-	{ 0,	"xfs",		xfs_image	},
-	{ 0,	"luks",		luks_image	},
-	{ 1,	"minix",	minix_image	},
-	{ 1,	"ext3",		ext3_image	},
-	{ 1,	"ext2",		ext2_image	},
-	{ 8,	"reiserfs",	reiserfs_image	},
-	{ 64,	"reiserfs",	reiserfs_image	},
-	{ 32,	"jfs",		jfs_image	}
+	{ 0,		"gzip",		gzip_image	},
+	{ 0,		"cramfs",	cramfs_image	},
+	{ 0,		"romfs",	romfs_image	},
+	{ 0,		"xfs",		xfs_image	},
+	{ 0,		"luks",		luks_image	},
+	{ 1,		"minix",	minix_image	},
+	{ 1,		"ext3",		ext3_image	},
+	{ 1,		"ext2",		ext2_image	},
+	{ 8,		"reiserfs",	reiserfs_image	},
+	{ 64,		"reiserfs",	reiserfs_image	},
+	{ 32,		"jfs",		jfs_image	},
+	{ SWAP_OFFSET,	"swap",		swap_image	}
 };
 
 int main(int argc, char *argv[])
diff -Nur klibc-1.2-hack/utils/swap_fs.h klibc-1.2-swap/utils/swap_fs.h
--- klibc-1.2-hack/utils/swap_fs.h	1970-01-01 01:00:00.000000000 +0100
+++ klibc-1.2-swap/utils/swap_fs.h	2006-02-05 01:57:36.000000000 +0100
@@ -0,0 +1,18 @@
+#ifndef __LINUX_SWAP_FS_H
+#define __LINUX_SWAP_FS_H
+
+/* The basic structures of the swap super block */
+#define SWAP_RESERVED_L		BLOCK_SIZE - 10
+#define SWAP_MAGIC_L		10
+#define SWAP_MAGIC_1		"SWAP-SPACE"
+#define SWAP_MAGIC_2		"SWAPSPACE2"
+/* The super block is the last block in the first page */
+#define SWAP_OFFSET		((PAGE_SIZE - BLOCK_SIZE) / BLOCK_SIZE)
+
+/* On-disk "super block" */
+struct swap_super_block {
+	char reserved[SWAP_RESERVED_L];
+	char magic[SWAP_MAGIC_L];
+};
+
+#endif


More information about the klibc mailing list