[comp.unix.questions] S5 file system magic number

guy%gorodish@Sun.COM (Guy Harris) (12/29/86)

> ...but I noted later that sVr2 has a magic number in the superblock.
> ...My question:  if there is a magic number there, why doesn't mount() look
> for it and reject file systems with bad magic numbers?

Because the magic number wasn't in the original V7 file system, used until
S5.  S5 added a magic number to the V7 superblock, but only to distinguish
512-byte-block file systems from 1024-byte-block file systems.  Since the
original V7 file system used 512-byte blocks, any file system with an
unknown magic number was treated as a 512-byte-block file system.
Requiring a valid magic number would have required that the root file
system be fixed before an S5 kernel was booted on it, unless the initial
mount of the root file system was treated as a special case, and required
that a file system be fixed before it was mounted by that kernel, unless a
special option were added to the "mount" system call.  I figure they just
decided this was too much trouble.

bzs@bu-cs.BU.EDU (Barry Shein) (12/30/86)

Rather than delving into why the SYSV mount command doesn't check for
magic numbers (as Guy Harris said, because not all mountable file
systems will have them) it's easy enough to implement the following
for yourself: (you could probably do the same thing in a shell script
by some sort of "dd | od | awk" kind of thing but this seemed simpler,
the shell script is left as an exercise to the reader :-)

	-Barry Shein, Boston University

#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/filsys.h>
#include <fcntl.h>

/*
 * Simple hack to check a SYSV file system for a magic number
 * and then mount it if present. If -f flag is present it skips
 * the check (or you could just use /etc/mount directly.)
 *
 * execs /etc/mount to do the actual mount, except for leading
 * optional -f (which is elided) just passes args as is. Could add
 * additional checks easily.
 * -Barry Shein (BZS@BU-CS.BU.EDU)
 */

#define MOUNT "/etc/mount"
#define MOUNT0 "mount"

main(argc,argv) int argc ; char **argv ;
{
	int fd ;
	struct filsys s ;
	char *prog;
	char *special, *dir, *ronly;
	int force = 0;
	int i;

	prog = *argv;	/* name we were run under, for error msgs */
	/*
	 * check for leading "-f" flag to force the mount
	 */
	if((argc > 1) && (strcmp(argv[1],"-f") == 0)) {
		--argc;
		++argv;
		force = 1;
	}
	/* must be at least "prog special dir" */
	if(argc < 3 ) {
		fprintf(stderr,"Usage: %s [-f] special dir [-r]\n",*prog);
		exit(1) ;
	}
	special = argv[1];
	dir = argv[2];
	ronly = (argc == 4) ? argv[3] : NULL;
	if(force) goto doit;	/* they gave a -f */

	if((fd = open(special,O_RDONLY)) < 0) {
		perror(argv[1]);
		exit(1);
	}
	if(lseek(fd,512L,0) < 0) {
		perror("seek");
		exit(1);
	}
	/* read in the super-block */
	if((i = read(fd,&s,sizeof s)) != sizeof s) {
		if(i < 0)
			perror("read");
		else fprintf(stderr,"short read?\n");
		exit(1);
	}
	if(s.s_magic != FsMAGIC) {
		fprintf(stderr,"%s: bad magic number! use -f to mount anyhow\n",
			special);
		exit(1);
	}
doit:
	/* do the actual mount */
	execl(MOUNT,MOUNT0,special,dir,ronly,0);
	fprintf(stderr,"can't run %s?!\n",MOUNT);
	exit(1);
}

geoff@desint.UUCP (Geoff Kuenning) (01/06/87)

In article <10848@sun.uucp> guy%gorodish@Sun.COM (Guy Harris) writes:

> Requiring a valid magic number would have required that the root file
> system be fixed before an S5 kernel was booted on it...I figure they just
> decided this was too much trouble.

Too bad.  The pre-conversion fix could have been done with a one-line script:

	echo '0x3fc?W <magic>' | adb -w /dev/root - > /dev/null
-- 

	Geoff Kuenning
	{hplabs,ihnp4}!trwrb!desint!geoff