aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--util-linux/fdisk.c35
1 files changed, 28 insertions, 7 deletions
diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c
index da03e683e..f4fd4d31d 100644
--- a/util-linux/fdisk.c
+++ b/util-linux/fdisk.c
@@ -2846,13 +2846,37 @@ open_list_and_close(const char *device, int user_specified)
2846 close_dev_fd(); 2846 close_dev_fd();
2847} 2847}
2848 2848
2849/* Is it a whole disk? The digit check is still useful
2850 for Xen devices for example. */
2851static int is_whole_disk(const char *disk)
2852{
2853 unsigned len;
2854 int fd = open(disk, O_RDONLY);
2855
2856 if (fd != -1) {
2857 struct hd_geometry geometry;
2858 int err = ioctl(fd, HDIO_GETGEO, &geometry);
2859 close(fd);
2860 if (!err)
2861 return (geometry.start == 0);
2862 }
2863
2864 /* Treat "nameN" as a partition name, not whole disk */
2865 /* note: mmcblk0 should work from the geometry check above */
2866 len = strlen(disk);
2867 if (len != 0 && isdigit(disk[len - 1]))
2868 return 0;
2869
2870 return 1;
2871}
2872
2849/* for fdisk -l: try all things in /proc/partitions 2873/* for fdisk -l: try all things in /proc/partitions
2850 that look like a partition name (do not end in a digit) */ 2874 that look like a partition name (do not end in a digit) */
2851static void 2875static void
2852list_devs_in_proc_partititons(void) 2876list_devs_in_proc_partititons(void)
2853{ 2877{
2854 FILE *procpt; 2878 FILE *procpt;
2855 char line[100], ptname[100], devname[120], *s; 2879 char line[100], ptname[100], devname[120];
2856 int ma, mi, sz; 2880 int ma, mi, sz;
2857 2881
2858 procpt = fopen_or_warn("/proc/partitions", "r"); 2882 procpt = fopen_or_warn("/proc/partitions", "r");
@@ -2861,13 +2885,10 @@ list_devs_in_proc_partititons(void)
2861 if (sscanf(line, " %u %u %u %[^\n ]", 2885 if (sscanf(line, " %u %u %u %[^\n ]",
2862 &ma, &mi, &sz, ptname) != 4) 2886 &ma, &mi, &sz, ptname) != 4)
2863 continue; 2887 continue;
2864 for (s = ptname; *s; s++) 2888
2865 continue;
2866 /* note: excluding '0': e.g. mmcblk0 is not a partition name! */
2867 if (s[-1] >= '1' && s[-1] <= '9')
2868 continue;
2869 sprintf(devname, "/dev/%s", ptname); 2889 sprintf(devname, "/dev/%s", ptname);
2870 open_list_and_close(devname, 0); 2890 if (is_whole_disk(devname))
2891 open_list_and_close(devname, 0);
2871 } 2892 }
2872#if ENABLE_FEATURE_CLEAN_UP 2893#if ENABLE_FEATURE_CLEAN_UP
2873 fclose(procpt); 2894 fclose(procpt);