displaying ext2 partition names in fdisk

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is a patch I've been modifying for a dozen years...

I wanted fdisk -l to return the partition names of all the 
disks.  I find it clumsy to run cfdisk on each disk...and you
get wonderfully confusing results when you mount by name and 
two partitions on the machine with the same name...and its useful
to know then to guess on these names (especially if you boot by name,
which I like to do).

fdisk -l looks like:

: leisner@gateway 10:36:34;./fdisk -l

Disk /dev/sda: 36.7 GB, 36703918080 bytes
64 heads, 32 sectors/track, 35003 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk identifier: 0x0cb10cb1

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         144      147440   83  Linux (/boot)
/dev/sda2             145        9160     9232384    5  Extended
/dev/sda3           19177       28713     9765888   83  Linux (suse-10-2)
/dev/sda5   *         145        1576     1466352   83  Linux (/1)
/dev/sda6            1577        8730     7325680   83  Linux (/usr1)
/dev/sda7            8731        9160      440304   82  Linux swap / Solaris

Disk /dev/sdb: 147.0 GB, 147086327808 bytes
255 heads, 63 sectors/track, 17882 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xfd794735

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        9120    73256368+   5  Extended
/dev/sdb2           14236       17882    29294527+  83  Linux (iso)
/dev/sdb5               1        1216     9767457   83  Linux (home)
/dev/sdb6            1217        3040    14651248+  83  Linux (usr-local)
/dev/sdb7            3041        5472    19535008+   b  W95 FAT32
/dev/sdb8            5473        7904    19535008+  83  Linux (pictures)
/dev/sdb9            7905        9120     9767488+  83  Linux

Disk /dev/sdc: 36.7 GB, 36748945408 bytes
64 heads, 32 sectors/track, 35046 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk identifier: 0x13741373

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        9537     9765872   83  Linux (backup)

Disk /dev/sdd: 73.5 GB, 73543163904 bytes
255 heads, 63 sectors/track, 8941 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x13241323

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1               1        1459    11719386    7  HPFS/NTFS
/dev/sdd2   *        1460        6681    41945715    f  W95 Ext'd (LBA)
/dev/sdd5            1460        1485      208813+  83  Linux (sdd-suse-boot)
/dev/sdd6            1486        2791    10490413+  83  Linux (sdd-suse-root)
/dev/sdd7            2792        2948     1261071   82  Linux swap / Solaris
/dev/sdd8            2949        5380    19535008+  83  Linux (images)

It could be extended to other types of disks fairly easily...

I didn't see a way to get a list of active partition names on a stock linux 
system.

Here's the patch -- its wrt util-linux-ng-2.13-rc2

--- Makefile.am	2007/07/22 02:21:51	1.1
+++ Makefile.am	2007/07/22 02:39:25
@@ -8,7 +8,7 @@
 
 sbin_PROGRAMS = fdisk
 man_MANS = fdisk.8
-fdisk_SOURCES = fdisk.c fdiskbsdlabel.c fdisksgilabel.c \
+fdisk_SOURCES = ext2_volumename.c fdisk.c fdiskbsdlabel.c fdisksgilabel.c \
 	fdisksunlabel.c fdiskaixlabel.c fdiskmaclabel.c partname.c \
 	fdisk.h fdisksunlabel.h fdisksgilabel.h fdiskaixlabel.h \
 	fdiskbsdlabel.h fdiskmaclabel.h $(fdisk_common)
--- ext2_volumename.c	2007/07/22 02:16:47	1.1
+++ ext2_volumename.c	2007/07/22 02:51:26
@@ -0,0 +1,100 @@
+/*
+ * From:   
+ * e2label.c		- Print or change the volume label on an ext2 fs
+ *
+ * Written by Andries Brouwer (aeb@xxxxxx), 970714
+ * 
+ * Copyright 1997, 1998 by Theodore Ts'o.
+ *
+ * %Begin-Header%
+ * This file may be redistributed under the terms of the GNU Public
+ * License.
+ * %End-Header%
+ *
+ * Modified marty leisner  Dec 18, 1998
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+#include <termios.h>
+#include <time.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#define EXT2_SUPER_MAGIC 0xEF53
+
+#define VOLNAMSZ 16
+
+static struct ext2_super_block {
+	char  s_dummy0[56];
+	unsigned char  s_magic[2];
+	char  s_dummy1[62];
+	char  s_volume_name[VOLNAMSZ];
+	char  s_last_mounted[64];
+	char  s_dummy2[824];
+} sb;
+
+/* stick valid superblock in sb.
+ * return 0 if it works, -1 otherwise
+ */
+static int read_superblock(char *dev)
+{
+	int fd;
+
+	fd = open(dev, O_RDONLY);
+	if (fd < 0) {
+	     perror(dev);
+	     fprintf (stderr, "e2label: cannot open %s\n", dev);
+	     return -1;
+	}
+	if (lseek(fd, 1024, SEEK_SET) != 1024) {
+	     close(fd);
+	     perror(dev);
+	     fprintf (stderr, "e2label: cannot seek to superblock\n");
+	     return -1;
+	}
+
+	if (read(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
+	     close(fd);
+	     perror(dev);
+	     fprintf (stderr, "e2label: error reading superblock\n");
+	     return -1;
+	}
+	
+	close(fd);
+
+	if (sb.s_magic[0] + 256*sb.s_magic[1] != EXT2_SUPER_MAGIC) {
+#if 0
+	     fprintf (stderr, "e2label: not an ext2 filesystem\n");
+#endif
+             return -1;	
+	}
+	return 0;
+
+}
+
+
+
+/* return a NULL point on failure, or the actual NULL volume name
+ * if it works
+ */
+char *extfs_volume_name(char *base, int device)
+{ 
+	char device_name[80];
+	static char volume_name[VOLNAMSZ + 1];
+	int result;
+
+	sprintf(device_name, "%s%d", base, device);	/* no error detection */
+	result = read_superblock(device_name);
+	if(result < 0) 
+		return NULL;
+
+	bzero(volume_name, sizeof(volume_name));
+	strncpy(volume_name, sb.s_volume_name, VOLNAMSZ);
+	return volume_name;
+	
+	
+}
--- fdisk.c	2007/07/22 02:09:33	1.1
+++ fdisk.c	2007/07/22 02:09:39
@@ -1816,7 +1816,7 @@
 			if (sector_size > 1024)
 				pblocks *= (sector_size / 1024);
                         printf(
-			    "%s  %c %11lu %11lu %11lu%c  %2x  %s\n",
+			    "%s  %c %11lu %11lu %11lu%c  %2x  %s",
 			partname(disk_device, i+1, w+2),
 /* boot flag */		!p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG
 			? '*' : '?',
@@ -1827,6 +1827,16 @@
 /* type id */		p->sys_ind,
 /* type name */		(type = partition_type(p->sys_ind)) ?
 			type : _("Unknown"));
+
+			if(LINUX_NATIVE == p->sys_ind) {
+				char *p;
+
+				p = extfs_volume_name(disk_device, i+ 1);
+				if(p && *p ) {
+					printf(" (%s)", p);
+				}
+			}
+			printf("\n");
 			check_consistency(p, i);
 		}
 	}
--- fdisk.h	2007/07/22 02:14:38	1.1
+++ fdisk.h	2007/07/22 02:17:22
@@ -107,4 +107,7 @@
 /* prototypes for fdisksgilabel.c */
 extern int valid_part_table_flag(unsigned char *b);
 
+/* prototypes for ext2_volumename.c */
+extern char *extfs_volume_name(char *base, int device);
+
 #define PROC_PARTITIONS "/proc/partitions"

-
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux