summaryrefslogtreecommitdiffstats
path: root/disk-utils/blockdev.c
diff options
context:
space:
mode:
authorKarel Zak2006-12-07 00:25:52 +0100
committerKarel Zak2006-12-07 00:25:52 +0100
commit612721dba838fe37af543421278416bb7acf770c (patch)
treec9e54454cd39b2c26604446ee1a91fb2e9443ed4 /disk-utils/blockdev.c
parentImported from util-linux-2.11m tarball. (diff)
downloadkernel-qcow2-util-linux-612721dba838fe37af543421278416bb7acf770c.tar.gz
kernel-qcow2-util-linux-612721dba838fe37af543421278416bb7acf770c.tar.xz
kernel-qcow2-util-linux-612721dba838fe37af543421278416bb7acf770c.zip
Imported from util-linux-2.11n tarball.
Diffstat (limited to 'disk-utils/blockdev.c')
-rw-r--r--disk-utils/blockdev.c100
1 files changed, 97 insertions, 3 deletions
diff --git a/disk-utils/blockdev.c b/disk-utils/blockdev.c
index 0407e4f9d..367eeb23b 100644
--- a/disk-utils/blockdev.c
+++ b/disk-utils/blockdev.c
@@ -29,6 +29,17 @@
#define BLKBSZSET _IOW(0x12,113,sizeof(int))
#endif
+/* Maybe <linux/hdreg.h> could be included */
+#ifndef HDIO_GETGEO
+#define HDIO_GETGEO 0x0301
+struct hd_geometry {
+ unsigned char heads;
+ unsigned char sectors;
+ unsigned short cylinders; /* truncated */
+ unsigned long start;
+};
+#endif
+
const char *progname;
struct bdc {
@@ -85,7 +96,10 @@ struct bdc {
static void
usage(void) {
int i;
- fprintf(stderr, _("Usage: %s [-V] [-v|-q] commands devices\n"), progname);
+ fprintf(stderr, _("Usage:\n"));
+ fprintf(stderr, " %s -V\n", progname);
+ fprintf(stderr, _(" %s --report [devices]\n"), progname);
+ fprintf(stderr, _(" %s [-v|-q] commands devices\n"), progname);
fprintf(stderr, _("Available commands:\n"));
for (i = 0; i < SIZE(bdcms); i++) {
fprintf(stderr, "\t%s", bdcms[i].name);
@@ -109,6 +123,9 @@ find_cmd(char *s) {
}
void do_commands(int fd, char **argv, int d);
+void report_header(void);
+void report_device(char *device, int quiet);
+void report_all_devices(void);
int
main(int argc, char **argv) {
@@ -136,6 +153,18 @@ main(int argc, char **argv) {
exit(0);
}
+ /* --report not together with other commands */
+ if (!strcmp(argv[1], "--report")) {
+ report_header();
+ if (argc > 2) {
+ for (d = 2; d < argc; d++)
+ report_device(argv[d], 0);
+ } else {
+ report_all_devices();
+ }
+ exit(0);
+ }
+
/* do each of the commands on each of the devices */
/* devices start after last command */
for (d = 1; d < argc; d++) {
@@ -188,7 +217,8 @@ do_commands(int fd, char **argv, int d) {
j = find_cmd(argv[i]);
if (j == -1) {
- fprintf(stderr, _("%s: Unknown command: %s\n"), progname, argv[i]);
+ fprintf(stderr, _("%s: Unknown command: %s\n"),
+ progname, argv[i]);
usage();
}
@@ -246,8 +276,72 @@ do_commands(int fd, char **argv, int d) {
break;
default:
if (verbose)
- printf("%s succeeded.\n", _(bdcms[j].help));
+ printf(_("%s succeeded.\n"), _(bdcms[j].help));
break;
}
}
}
+
+#define PROC_PARTITIONS "/proc/partitions"
+
+void
+report_all_devices(void) {
+ FILE *procpt;
+ char line[200];
+ char ptname[200];
+ char device[210];
+ int ma, mi, sz;
+
+ procpt = fopen(PROC_PARTITIONS, "r");
+ if (!procpt) {
+ fprintf(stderr, _("%s: cannot open %s\n"),
+ progname, PROC_PARTITIONS);
+ exit(1);
+ }
+
+ while (fgets(line, sizeof(line), procpt)) {
+ if (sscanf (line, " %d %d %d %[^\n ]",
+ &ma, &mi, &sz, ptname) != 4)
+ continue;
+
+ sprintf(device, "/dev/%s", ptname);
+ report_device(device, 1);
+ }
+}
+
+void
+report_device(char *device, int quiet) {
+ int fd;
+ int ro, ssz, bsz;
+ long ra, sz, ss;
+ struct hd_geometry g;
+
+ fd = open(device, O_RDONLY | O_NONBLOCK);
+ if (fd < 0) {
+ if (!quiet)
+ fprintf(stderr, _("%s: cannot open %s\n"),
+ progname, device);
+ return;
+ }
+
+ ro = ssz = bsz = 0;
+ g.start = ra = sz = ss = 0;
+ if (ioctl (fd, BLKROGET, &ro) == 0 &&
+ ioctl (fd, BLKRAGET, &ra) == 0 &&
+ ioctl (fd, BLKSSZGET, &ssz) == 0 &&
+ ioctl (fd, BLKBSZGET, &bsz) == 0 &&
+ ioctl (fd, BLKGETSIZE, &sz) == 0 &&
+ ioctl (fd, HDIO_GETGEO, &g) == 0) {
+ printf("%s %5ld %5d %5d %10ld %10ld %s\n",
+ ro ? "ro" : "rw", ra, ssz, bsz, g.start, sz, device);
+ } else {
+ if (!quiet)
+ fprintf(stderr, _("%s: ioctl error on %s\n"),
+ progname, device);
+ }
+}
+
+void
+report_header() {
+ printf(_("RO RA SSZ BSZ StartSec Size Device\n"));
+}