]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/blockdev.c
2 * blockdev.c --- Do various simple block device ioctls from the command line
12 #include <sys/ioctl.h>
16 /* Since it is impossible to include <linux/fs.h>, let us
17 give the ioctls explicitly. */
20 #define BLKROSET _IO(0x12,93)
21 #define BLKROGET _IO(0x12,94)
22 #define BLKRRPART _IO(0x12,95)
23 #define BLKGETSIZE _IO(0x12,96)
24 #define BLKFLSBUF _IO(0x12,97)
25 #define BLKRASET _IO(0x12,98)
26 #define BLKRAGET _IO(0x12,99)
27 #define BLKSSZGET _IO(0x12,104)
28 #define BLKBSZGET _IOR(0x12,112,sizeof(int))
29 #define BLKBSZSET _IOW(0x12,113,sizeof(int))
32 /* Maybe <linux/hdreg.h> could be included */
34 #define HDIO_GETGEO 0x0301
37 unsigned char sectors
;
38 unsigned short cylinders
; /* truncated */
61 { "--setro", "BLKROSET", BLKROSET
, ARGINTP
, 1, NULL
, N_("set read-only") },
62 { "--setrw", "BLKROSET", BLKROSET
, ARGINTP
, 0, NULL
, N_("set read-write") },
65 { "--getro", "BLKROGET", BLKROGET
, ARGINTG
, -1, NULL
, N_("get read-only") },
68 { "--getss", "BLKSSZGET", BLKSSZGET
, ARGINTG
, -1, NULL
, N_("get sectorsize") },
71 { "--getbsz", "BLKBSZGET", BLKBSZGET
, ARGINTG
, -1, NULL
, N_("get blocksize") },
74 { "--setbsz", "BLKBSZSET", BLKBSZSET
, ARGINTAP
, 0, "BLOCKSIZE", N_("set blocksize") },
77 { "--getsize", "BLKGETSIZE", BLKGETSIZE
, ARGLINTG
, -1, NULL
, N_("get size") },
80 { "--setra", "BLKRASET", BLKRASET
, ARGINTA
, 0, "READAHEAD", N_("set readahead") },
83 { "--getra", "BLKRAGET", BLKRAGET
, ARGLINTG
, -1, NULL
, N_("get readahead") },
86 { "--flushbufs", "BLKFLSBUF", BLKFLSBUF
, ARGNONE
, 0, NULL
, N_("flush buffers") },
89 { "--rereadpt", "BLKRRPART", BLKRRPART
, ARGNONE
, 0, NULL
,
90 N_("reread partition table") },
94 #define SIZE(a) (sizeof(a)/sizeof((a)[0]))
99 fprintf(stderr
, _("Usage:\n"));
100 fprintf(stderr
, " %s -V\n", progname
);
101 fprintf(stderr
, _(" %s --report [devices]\n"), progname
);
102 fprintf(stderr
, _(" %s [-v|-q] commands devices\n"), progname
);
103 fprintf(stderr
, _("Available commands:\n"));
104 for (i
= 0; i
< SIZE(bdcms
); i
++) {
105 fprintf(stderr
, "\t%s", bdcms
[i
].name
);
106 if (bdcms
[i
].argname
)
107 fprintf(stderr
, " %s", bdcms
[i
].argname
);
109 fprintf(stderr
, "\t(%s)", _(bdcms
[i
].help
));
110 fprintf(stderr
, "\n");
119 for (j
= 0; j
< SIZE(bdcms
); j
++)
120 if (!strcmp(s
, bdcms
[j
].name
))
125 void do_commands(int fd
, char **argv
, int d
);
126 void report_header(void);
127 void report_device(char *device
, int quiet
);
128 void report_all_devices(void);
131 main(int argc
, char **argv
) {
135 /* egcs-2.91.66 is buggy and says:
136 blockdev.c:93: warning: `d' might be used uninitialized */
140 if ((p
= strrchr(progname
, '/')) != NULL
)
143 setlocale(LC_ALL
, "");
144 bindtextdomain(PACKAGE
, LOCALEDIR
);
150 /* -V not together with commands */
151 if (!strcmp(argv
[1], "-V") || !strcmp(argv
[1], "--version")) {
152 printf("%s from %s\n", progname
, util_linux_version
);
156 /* --report not together with other commands */
157 if (!strcmp(argv
[1], "--report")) {
160 for (d
= 2; d
< argc
; d
++)
161 report_device(argv
[d
], 0);
163 report_all_devices();
168 /* do each of the commands on each of the devices */
169 /* devices start after last command */
170 for (d
= 1; d
< argc
; d
++) {
171 j
= find_cmd(argv
[d
]);
173 if (bdcms
[j
].argtype
== ARGINTA
||
174 bdcms
[j
].argtype
== ARGINTAP
)
178 if (!strcmp(argv
[d
], "--")) {
182 if (argv
[d
][0] != '-')
189 for (k
= d
; k
< argc
; k
++) {
190 fd
= open(argv
[k
], O_RDONLY
, 0);
195 do_commands(fd
, argv
, d
);
202 do_commands(int fd
, char **argv
, int d
) {
208 for (i
= 1; i
< d
; i
++) {
209 if (!strcmp(argv
[i
], "-v")) {
213 if (!strcmp(argv
[i
], "-q")) {
218 j
= find_cmd(argv
[i
]);
220 fprintf(stderr
, _("%s: Unknown command: %s\n"),
225 switch(bdcms
[j
].argtype
) {
228 res
= ioctl(fd
, bdcms
[j
].ioc
, 0);
232 fprintf(stderr
, _("%s requires an argument\n"),
236 iarg
= atoi(argv
[++i
]);
237 res
= ioctl(fd
, bdcms
[j
].ioc
, iarg
);
241 fprintf(stderr
, _("%s requires an argument\n"),
245 iarg
= atoi(argv
[++i
]);
246 res
= ioctl(fd
, bdcms
[j
].ioc
, &iarg
);
250 iarg
= bdcms
[j
].argval
;
251 res
= ioctl(fd
, bdcms
[j
].ioc
, &iarg
);
254 larg
= bdcms
[j
].argval
;
255 res
= ioctl(fd
, bdcms
[j
].ioc
, &larg
);
259 perror(bdcms
[j
].iocname
);
261 printf("%s failed.\n", _(bdcms
[j
].help
));
264 switch(bdcms
[j
].argtype
) {
267 printf("%s: %d\n", _(bdcms
[j
].help
), iarg
);
269 printf("%d\n", iarg
);
273 printf("%s: %ld\n", _(bdcms
[j
].help
), larg
);
275 printf("%ld\n", larg
);
279 printf(_("%s succeeded.\n"), _(bdcms
[j
].help
));
285 #define PROC_PARTITIONS "/proc/partitions"
288 report_all_devices(void) {
295 procpt
= fopen(PROC_PARTITIONS
, "r");
297 fprintf(stderr
, _("%s: cannot open %s\n"),
298 progname
, PROC_PARTITIONS
);
302 while (fgets(line
, sizeof(line
), procpt
)) {
303 if (sscanf (line
, " %d %d %d %[^\n ]",
304 &ma
, &mi
, &sz
, ptname
) != 4)
307 sprintf(device
, "/dev/%s", ptname
);
308 report_device(device
, 1);
313 report_device(char *device
, int quiet
) {
317 struct hd_geometry g
;
319 fd
= open(device
, O_RDONLY
| O_NONBLOCK
);
322 fprintf(stderr
, _("%s: cannot open %s\n"),
328 g
.start
= ra
= sz
= ss
= 0;
329 if (ioctl (fd
, BLKROGET
, &ro
) == 0 &&
330 ioctl (fd
, BLKRAGET
, &ra
) == 0 &&
331 ioctl (fd
, BLKSSZGET
, &ssz
) == 0 &&
332 ioctl (fd
, BLKBSZGET
, &bsz
) == 0 &&
333 ioctl (fd
, BLKGETSIZE
, &sz
) == 0 &&
334 ioctl (fd
, HDIO_GETGEO
, &g
) == 0) {
335 printf("%s %5ld %5d %5d %10ld %10ld %s\n",
336 ro
? "ro" : "rw", ra
, ssz
, bsz
, g
.start
, sz
, device
);
339 fprintf(stderr
, _("%s: ioctl error on %s\n"),
346 printf(_("RO RA SSZ BSZ StartSec Size Device\n"));