[BusyBox] Re: finding real root device

Glenn McGrath bug1 at optushome.com.au
Sun Jan 7 22:57:53 UTC 2001


Erik Andersen wrote:
> 
> On Mon Jan 08, 2001 at 12:33:23AM +1100, Glenn McGrath wrote:
> > I just ran across a reference to you in
> > util-linux/mount/guess-real-rootfs.c (something like that)
> 
> heh.
> 
> > Today i was looking at the same problem, i didnt find the way you had
> > done untill i had come up with a different aproach.
> >
> > By stating / you get the device number, but as an alternative to walking
> > through /dev to find a match, you can look through /proc/partitions
> > which gives the major and minor number of each partition.
> 
> Hmm.  Worth checking.  Mind brewing up a patch to utility.c to
> avoid the linear search through /dev?
> 
> > I think all root devices should be listed in /proc/partitions, loop
> > devices arent, but i dont think loop can be root with a normal kernel
> > anyway.
> 
> Hmm.  I know the linear search through /dev plan works for anything.
> I'd be interested to see if you can help avoid this rather slow and
> ugly search,
> 
>  -Erik
> 

Done, attached is a patch to search /proc/partitions instead fo /dev, i
tested it with df and it works.

Differences
	- 40 bytes larger
	- depends on /proc
	- wont work if you have a strange root device that isnt in
/proc/partitions (e.g. loop)
	+ probably faster than stating /dev/*

So looks like there are more minuses than pluses.


Glenn
-------------- next part --------------
Common subdirectories: busybox.orig/CVS and busybox/CVS
Common subdirectories: busybox.orig/CVSROOT and busybox/CVSROOT
Binary files busybox.orig/busybox and busybox/busybox differ
Common subdirectories: busybox.orig/debian and busybox/debian
Common subdirectories: busybox.orig/docs and busybox/docs
Common subdirectories: busybox.orig/kernel-patches and busybox/kernel-patches
Common subdirectories: busybox.orig/scripts and busybox/scripts
Common subdirectories: busybox.orig/tests and busybox/tests
diff -U 3 busybox.orig/utility.c busybox/utility.c
--- busybox.orig/utility.c	Wed Jan  3 12:38:29 2001
+++ busybox/utility.c	Mon Jan  8 09:46:53 2001
@@ -1529,44 +1529,39 @@
 #if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
 extern int find_real_root_device_name(char* name)
 {
-	DIR *dir;
-	struct dirent *entry;
-	struct stat statBuf, rootStat;
-	char fileName[BUFSIZ];
+	FILE *proc_partitions;
+	char *line;
+	struct stat rootStat;
+	int major, minor, blocks;
+	char device_name[100];
 
 	if (stat("/", &rootStat) != 0) {
 		error_msg("could not stat '/'\n");
-		return( FALSE);
+		return FALSE;
 	}
 
-	dir = opendir("/dev");
-	if (!dir) {
-		error_msg("could not open '/dev'\n");
-		return( FALSE);
+	if ((proc_partitions = fopen("/proc/partitions", "r")) == NULL) {
+		error_msg("couldnt open /proc/partitions");
+		return FALSE;
 	}
-
-	while((entry = readdir(dir)) != NULL) {
-
-		/* Must skip ".." since that is "/", and so we 
-		 * would get a false positive on ".."  */
-		if (strcmp(entry->d_name, "..") == 0)
-			continue;
-
-		snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
-
-		if (stat(fileName, &statBuf) != 0)
-			continue;
-		/* Some char devices have the same dev_t as block
-		 * devices, so make sure this is a block device */
-		if (! S_ISBLK(statBuf.st_mode))
-			continue;
-		if (statBuf.st_rdev == rootStat.st_rdev) {
-			strcpy(name, fileName); 
-			return ( TRUE);
+	
+	/* the firstr two lines are headers */
+	get_line_from_file(proc_partitions);
+	get_line_from_file(proc_partitions);
+
+	/* extract the major and minor number from all following lines */	
+	while ((line = get_line_from_file(proc_partitions)) != NULL) {
+		sscanf(line, " %d %d %d %99s", &major, &minor, &blocks, device_name);
+		if ( ((major << 8) + minor) == rootStat.st_rdev ) {
+			fclose(proc_partitions);
+			strcpy(name, "/dev/");
+			strcat(name, device_name);
+			return TRUE;
 		}
 	}
+	fclose(proc_partitions);
+	return FALSE;
 
-	return( FALSE);
 }
 #endif
 


More information about the busybox mailing list