Newsgroups: comp.os.linux.announce
From: kevin@frobozz.sccsi.com
Subject: MS_REMOUNT support in mount.c
Message-ID: <1993Jul12.134402.23674@dg-rtp.dg.com>
Approved: linux-announce@tc.cornell.edu (Matt Welsh)
Date: Mon, 12 Jul 93 13:44:02 GMT

While upgrading to 0.99.10, I ran across the problem of the kernel mounting
my root filesystem readonly.  This turned out to be the result of using
Frank Xia's mkboot program with the kernel in a raw partition, which is why
I'm sure nobody else saw the behavior at all.

To deal with this, I modified the mount program to allow one to use the new
MS_REMOUNT option to mount().  As it stands now, my kernel mounts my root
filesystem readonly, and I change that with the following command:

    mount -n -o rw,remount /

The mount command won't touch /etc/mtab if remount is one of the flags.
This is a bug in that it should really change the attributes recorded there,
but that seemed more difficult than I wanted to deal with.

What follows is the patch to mount.c and mount.8, in the usermount-0.2
distribution, to allow remounting of filesystems in place.

Usermount-0.2 can be found on tsx-11.mit.edu in the file
/pub/linux/sources/sbin/usermount-0.2.tar.z.

What follows is the patch.  Just do a "patch -p <patchfile" in the same
directory that the usermount source is located.

				Kevin Brown
				kevin@frobozz.sccsi.com


*** mount.c.old	Mon Apr 26 12:34:53 1993
--- mount.c	Sun Jul 11 22:20:51 1993
***************
*** 39,44 ****
--- 39,45 ----
  #define MNTOPT_NODEV    "nodev"
  #define MNTOPT_NOEXEC   "noexec"
  #define MNTOPT_SYNC     "sync"
+ #define MNTOPT_REMOUNT	"remount"
  
  #ifndef __STDC__
  #error Error: This program requires ANSI C to compile
***************
*** 149,154 ****
--- 150,176 ----
  	    }
  	    endmntent(fstab);
  
+ 	    /*
+ 	     * If the fstab lookup failed, maybe it's a filesystem that's
+ 	     * already been mounted, in which case the "remount" option has
+ 	     * to be set.  We process the options early to test this.
+ 	     */
+ 	    process_options ( mntoptions );
+ 	    if (!mntdev && (opt_std & MS_REMOUNT)) {
+ 		if(!(fstab = setmntent(MTAB_FILE, "r"))) err(filename);
+ 		while((mp = getmntent(fstab))) {
+ 		    if(!strcmp(mntdir, mp->mnt_dir)) {
+ 			strncpy(mntdevbuf, mp->mnt_fsname, sizeof(mntdevbuf)-1);
+ 			mntdev = mntdevbuf;
+ 			break;
+ 		    } else if(!strcmp(mntdir, mp->mnt_fsname)) {
+ 			strncpy(mntdevbuf, mp->mnt_dir, sizeof(mntdevbuf)-1);
+ 			mntdev = mntdir;
+ 			mntdir = mntdevbuf;
+ 		    }
+ 		}
+ 	    }
+ 
  	    if(!mntdev) {
  		fprintf(stderr, "mount: Don't know what to mount on %s\n",
  			mntdir);
***************
*** 171,177 ****
      }
  
      /* check for lock */
!     if(!lock_mtab()) {
  	/* don't use err() for this */
  	fprintf(stderr, "mount: A lock-file exists, mount denied.\n");
  	exit(1);
--- 193,199 ----
      }
  
      /* check for lock */
!     if(!opt_nomtab && !lock_mtab()) {
  	/* don't use err() for this */
  	fprintf(stderr, "mount: A lock-file exists, mount denied.\n");
  	exit(1);
***************
*** 292,298 ****
  	       mp->mnt_type, opt_std, opt_nostd);
  #endif
  
! 	if(strcmp(mp->mnt_dir, "/") == 0) {
  	    if(opt_verbose)
  	      fprintf(stderr, "mount: root already mounted by kernel\n");
  	} else {
--- 314,320 ----
  	       mp->mnt_type, opt_std, opt_nostd);
  #endif
  
! 	if(strcmp(mp->mnt_dir, "/") == 0 && !(opt_std & MS_REMOUNT)) {
  	    if(opt_verbose)
  	      fprintf(stderr, "mount: root already mounted by kernel\n");
  	} else {
***************
*** 324,330 ****
  	}
      } /* if(!opt_fakemtab) */
  
!     if(!opt_nomtab) {
  	if(!(mtab = setmntent(MTAB_FILE, "a"))) {
  	    fprintf(stderr, "mount: open(%s) failed.\n", MTAB_FILE);
  	    return 1;
--- 346,352 ----
  	}
      } /* if(!opt_fakemtab) */
  
!     if(!opt_nomtab && !(opt_std & MS_REMOUNT)) {
  	if(!(mtab = setmntent(MTAB_FILE, "a"))) {
  	    fprintf(stderr, "mount: open(%s) failed.\n", MTAB_FILE);
  	    return 1;
***************
*** 391,396 ****
--- 413,419 ----
  	else if(!strcmp(opt, MNTOPT_NODEV))  opt_std |= MS_NODEV;
  	else if(!strcmp(opt, MNTOPT_NOEXEC)) opt_std |= MS_NOEXEC;
  	else if(!strcmp(opt, MNTOPT_SYNC))   opt_std |= MS_SYNC;
+ 	else if(!strcmp(opt, MNTOPT_REMOUNT)) opt_std |= MS_REMOUNT;
  	else {
  	    /* non-standard options are put in a comma-sep. string */
  	    strcat(opt_nostd, opt);
*** mount.8.old	Mon Apr 26 12:28:50 1993
--- mount.8	Sun Jul 11 23:16:32 1993
***************
*** 50,56 ****
  Fake an entry in /etc/mtab without actually mounting anything.
  .TP
  \-n
! Do not write an entry in /etc/mtab for the mount.
  .TP
  \-p
  Print the entry that would otherwise have been written to /etc/mtab on stdout.
--- 50,59 ----
  Fake an entry in /etc/mtab without actually mounting anything.
  .TP
  \-n
! Do not write an entry in /etc/mtab for the mount.  This option also causes
! the lockfile, /etc/mtab~, to be ignored, since no changes will be made to
! /etc/mtab anyway.  This is particularly useful if you're trying to re-mount
! your root filesystem from read-only to read-write.
  .TP
  \-p
  Print the entry that would otherwise have been written to /etc/mtab on stdout.
***************
*** 110,115 ****
--- 113,122 ----
  
  .BR sync :
  Always keep the mounted volume sync'ed.
+ 
+ .BR remount :
+ Apply the other specified options to an already-mounted filesystem.
+ 
  .RE
  .PP
  If \-u is given, or the caller of 

--
Send submissions for comp.os.linux.announce to: linux-announce@tc.cornell.edu
