linuxutils-disk 0.1.0

Disk utilities from linuxutils (blockdev, isosize)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use linuxutils_common::man::ManContent;

pub const MAN: ManContent = ManContent::empty();

use clap::Parser;
use cols::{Cols, print_table};
use std::{
    fs::File,
    io::{self, BufRead},
    os::unix::io::AsRawFd,
    process::ExitCode,
};

// Block device ioctls
const BLKROSET: libc::c_ulong = 0x125d;
const BLKROGET: libc::c_ulong = 0x125e;
const BLKRRPART: libc::c_ulong = 0x125f;
const BLKGETSIZE: libc::c_ulong = 0x1260;
const BLKFLSBUF: libc::c_ulong = 0x1261;
const BLKRASET: libc::c_ulong = 0x1262;
const BLKRAGET: libc::c_ulong = 0x1263;
const BLKFRASET: libc::c_ulong = 0x1264;
const BLKFRAGET: libc::c_ulong = 0x1265;
const BLKSECTGET: libc::c_ulong = 0x1267;
const BLKSSZGET: libc::c_ulong = 0x1268;
const BLKBSZGET: libc::c_ulong = 0x80081270;
const BLKBSZSET: libc::c_ulong = 0x40081271;
const BLKGETSIZE64: libc::c_ulong = 0x80081272;
const BLKIOMIN: libc::c_ulong = 0x1278;
const BLKIOOPT: libc::c_ulong = 0x1279;
const BLKALIGNOFF: libc::c_ulong = 0x127a;
const BLKPBSZGET: libc::c_ulong = 0x127b;
const BLKDISCARDZEROES: libc::c_ulong = 0x127c;

/// Call block device ioctls from the command line.
///
/// Each `--getXXX` or `--setXXX` command performs a single ioctl on
/// the specified block device(s). Multiple commands and devices can be
/// given in a single invocation.
#[derive(Parser)]
#[command(
    name = "blockdev",
    about = "Call block device ioctls from the command line",
    override_usage = "blockdev [-q] [-v] command [command...] device [device...]\n       \
                      blockdev --report [device...]"
)]
pub struct Args {
    /// Commands, arguments, and device paths
    #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
    args: Vec<String>,
}

enum Cmd {
    GetInt(libc::c_ulong, &'static str),
    GetUint(libc::c_ulong, &'static str),
    GetU64(libc::c_ulong, &'static str),
    GetUlong(libc::c_ulong, &'static str),
    GetSz,
    SetInt(libc::c_ulong, libc::c_int, &'static str),
    SetUlong(libc::c_ulong, libc::c_ulong, &'static str),
    Action(libc::c_ulong, &'static str),
}

fn ioctl_get_int(fd: i32, nr: libc::c_ulong) -> io::Result<i64> {
    let mut val: libc::c_int = 0;
    if unsafe { libc::ioctl(fd, nr, &mut val) } < 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(val as i64)
    }
}

fn ioctl_get_uint(fd: i32, nr: libc::c_ulong) -> io::Result<i64> {
    let mut val: libc::c_uint = 0;
    if unsafe { libc::ioctl(fd, nr, &mut val) } < 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(val as i64)
    }
}

fn ioctl_get_u64(fd: i32, nr: libc::c_ulong) -> io::Result<u64> {
    let mut val: u64 = 0;
    if unsafe { libc::ioctl(fd, nr, &mut val) } < 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(val)
    }
}

fn ioctl_get_ulong(fd: i32, nr: libc::c_ulong) -> io::Result<i64> {
    let mut val: libc::c_ulong = 0;
    if unsafe { libc::ioctl(fd, nr, &mut val) } < 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(val as i64)
    }
}

fn ioctl_set_int(
    fd: i32,
    nr: libc::c_ulong,
    val: libc::c_int,
) -> io::Result<()> {
    if unsafe { libc::ioctl(fd, nr, &val) } < 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(())
    }
}

fn ioctl_set_ulong(
    fd: i32,
    nr: libc::c_ulong,
    val: libc::c_ulong,
) -> io::Result<()> {
    if unsafe { libc::ioctl(fd, nr, val) } < 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(())
    }
}

fn ioctl_action(fd: i32, nr: libc::c_ulong) -> io::Result<()> {
    if unsafe { libc::ioctl(fd, nr, 0) } < 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(())
    }
}

fn execute_cmd(
    fd: i32,
    cmd: &Cmd,
    verbose: bool,
    quiet: bool,
) -> io::Result<()> {
    match cmd {
        Cmd::GetInt(nr, name) => {
            let val = ioctl_get_int(fd, *nr)?;
            if verbose {
                print!("{name}: ");
            }
            println!("{val}");
        }
        Cmd::GetUint(nr, name) => {
            let val = ioctl_get_uint(fd, *nr)?;
            if verbose {
                print!("{name}: ");
            }
            println!("{val}");
        }
        Cmd::GetU64(nr, name) => {
            let val = ioctl_get_u64(fd, *nr)?;
            if verbose {
                print!("{name}: ");
            }
            println!("{val}");
        }
        Cmd::GetUlong(nr, name) => {
            let val = ioctl_get_ulong(fd, *nr)?;
            if verbose {
                print!("{name}: ");
            }
            println!("{val}");
        }
        Cmd::GetSz => {
            let bytes = ioctl_get_u64(fd, BLKGETSIZE64)?;
            if verbose {
                print!("getsz: ");
            }
            println!("{}", bytes / 512);
        }
        Cmd::SetInt(nr, val, name) => {
            ioctl_set_int(fd, *nr, *val)?;
            if !quiet {
                eprintln!("{name} succeeded.");
            }
        }
        Cmd::SetUlong(nr, val, name) => {
            ioctl_set_ulong(fd, *nr, *val)?;
            if !quiet {
                eprintln!("{name} succeeded.");
            }
        }
        Cmd::Action(nr, name) => {
            ioctl_action(fd, *nr)?;
            if !quiet {
                eprintln!("{name} succeeded.");
            }
        }
    }
    Ok(())
}

fn get_start_sector(device: &str) -> u64 {
    let Ok(f) = File::open(device) else {
        return 0;
    };
    let mut stat: libc::stat = unsafe { std::mem::zeroed() };
    if unsafe { libc::fstat(f.as_raw_fd(), &mut stat) } < 0 {
        return 0;
    }
    let major = libc::major(stat.st_rdev as libc::dev_t);
    let minor = libc::minor(stat.st_rdev as libc::dev_t);
    let path = format!("/sys/dev/block/{major}:{minor}/start");
    std::fs::read_to_string(path)
        .ok()
        .and_then(|s| s.trim().parse().ok())
        .unwrap_or(0)
}

fn devices_from_proc_partitions() -> Vec<String> {
    let mut devices = Vec::new();
    let Ok(file) = File::open("/proc/partitions") else {
        return devices;
    };
    for line in io::BufReader::new(file)
        .lines()
        .map_while(Result::ok)
        .skip(2)
    {
        let fields: Vec<&str> = line.split_whitespace().collect();
        if fields.len() >= 4 {
            devices.push(format!("/dev/{}", fields[3]));
        }
    }
    devices
}

#[derive(Cols)]
struct ReportRow {
    #[column(right, header = "RO")]
    ro: i64,

    #[column(right, header = "RA")]
    ra: i64,

    #[column(right, header = "SSZ")]
    ssz: i64,

    #[column(right, header = "BSZ")]
    bsz: i64,

    #[column(right, header = "StartSec")]
    start_sec: u64,

    #[column(right, header = "Size")]
    size: u64,

    #[column(header = "Device")]
    device: String,
}

fn print_report(devices: &[String]) {
    let mut rows = Vec::new();
    for device in devices {
        let Ok(f) = File::options().read(true).open(device) else {
            continue;
        };
        let fd = f.as_raw_fd();
        rows.push(ReportRow {
            ro: ioctl_get_int(fd, BLKROGET).unwrap_or(-1),
            ra: ioctl_get_ulong(fd, BLKRAGET).unwrap_or(-1),
            ssz: ioctl_get_int(fd, BLKSSZGET).unwrap_or(-1),
            bsz: ioctl_get_int(fd, BLKBSZGET).unwrap_or(-1),
            start_sec: get_start_sector(device),
            size: ioctl_get_u64(fd, BLKGETSIZE64).unwrap_or(0),
            device: device.clone(),
        });
    }
    let table = ReportRow::to_table(&rows);
    let _ = print_table(&table, &mut io::stdout().lock());
}

struct ParsedArgs {
    commands: Vec<Cmd>,
    devices: Vec<String>,
    verbose: bool,
    quiet: bool,
    report: bool,
}

fn parse_commands(raw_args: &[String]) -> Result<ParsedArgs, String> {
    let mut commands: Vec<Cmd> = Vec::new();
    let mut devices: Vec<String> = Vec::new();
    let mut verbose = false;
    let mut quiet = false;
    let mut report = false;
    let mut i = 0;

    while i < raw_args.len() {
        let arg = &raw_args[i];
        match arg.as_str() {
            "-v" | "--verbose" => verbose = true,
            "-q" => quiet = true,
            "--report" => report = true,

            "--getro" => commands.push(Cmd::GetInt(BLKROGET, "getro")),
            "--getbsz" => commands.push(Cmd::GetInt(BLKBSZGET, "getbsz")),
            "--getss" => commands.push(Cmd::GetInt(BLKSSZGET, "getss")),
            "--getpbsz" => commands.push(Cmd::GetUint(BLKPBSZGET, "getpbsz")),
            "--getsize" => commands.push(Cmd::GetUlong(BLKGETSIZE, "getsize")),
            "--getsize64" => {
                commands.push(Cmd::GetU64(BLKGETSIZE64, "getsize64"))
            }
            "--getsz" => commands.push(Cmd::GetSz),
            "--getra" => commands.push(Cmd::GetUlong(BLKRAGET, "getra")),
            "--getfra" => commands.push(Cmd::GetUlong(BLKFRAGET, "getfra")),
            "--getiomin" => commands.push(Cmd::GetUint(BLKIOMIN, "getiomin")),
            "--getioopt" => commands.push(Cmd::GetUint(BLKIOOPT, "getioopt")),
            "--getalignoff" => {
                commands.push(Cmd::GetUint(BLKALIGNOFF, "getalignoff"))
            }
            "--getmaxsect" => {
                commands.push(Cmd::GetUint(BLKSECTGET, "getmaxsect"))
            }
            "--getdiscardzeroes" => {
                commands
                    .push(Cmd::GetUint(BLKDISCARDZEROES, "getdiscardzeroes"));
            }

            "--setro" => commands.push(Cmd::SetInt(BLKROSET, 1, "setro")),
            "--setrw" => commands.push(Cmd::SetInt(BLKROSET, 0, "setrw")),
            "--setbsz" => {
                i += 1;
                let val: libc::c_int = raw_args
                    .get(i)
                    .ok_or("--setbsz requires an argument")?
                    .parse()
                    .map_err(|_| "--setbsz: invalid number")?;
                commands.push(Cmd::SetInt(BLKBSZSET, val, "setbsz"));
            }
            "--setra" => {
                i += 1;
                let val: libc::c_ulong = raw_args
                    .get(i)
                    .ok_or("--setra requires an argument")?
                    .parse()
                    .map_err(|_| "--setra: invalid number")?;
                commands.push(Cmd::SetUlong(BLKRASET, val, "setra"));
            }
            "--setfra" => {
                i += 1;
                let val: libc::c_ulong = raw_args
                    .get(i)
                    .ok_or("--setfra requires an argument")?
                    .parse()
                    .map_err(|_| "--setfra: invalid number")?;
                commands.push(Cmd::SetUlong(BLKFRASET, val, "setfra"));
            }
            "--flushbufs" => commands.push(Cmd::Action(BLKFLSBUF, "flushbufs")),
            "--rereadpt" => commands.push(Cmd::Action(BLKRRPART, "rereadpt")),

            other if other.starts_with('-') => {
                return Err(format!("unknown command: {other}"));
            }
            _ => devices.push(arg.clone()),
        }
        i += 1;
    }

    Ok(ParsedArgs {
        commands,
        devices,
        verbose,
        quiet,
        report,
    })
}

pub fn run(args: Args) -> ExitCode {
    let ParsedArgs {
        commands,
        devices,
        verbose,
        quiet,
        report,
    } = match parse_commands(&args.args) {
        Ok(parsed) => parsed,
        Err(e) => {
            eprintln!("blockdev: {e}");
            return ExitCode::FAILURE;
        }
    };

    if report {
        let devs = if devices.is_empty() {
            devices_from_proc_partitions()
        } else {
            devices
        };
        print_report(&devs);
        return ExitCode::SUCCESS;
    }

    if commands.is_empty() {
        eprintln!("blockdev: no command given, try 'blockdev --help'");
        return ExitCode::FAILURE;
    }

    if devices.is_empty() {
        eprintln!("blockdev: no device specified");
        return ExitCode::FAILURE;
    }

    let mut failed = false;
    for device in &devices {
        let f = match File::options().read(true).write(true).open(device) {
            Ok(f) => f,
            Err(e) => {
                eprintln!("blockdev: cannot open {device}: {e}");
                failed = true;
                continue;
            }
        };
        let fd = f.as_raw_fd();
        for cmd in &commands {
            if let Err(e) = execute_cmd(fd, cmd, verbose, quiet) {
                eprintln!("blockdev: {device}: {e}");
                failed = true;
            }
        }
    }

    if failed {
        ExitCode::FAILURE
    } else {
        ExitCode::SUCCESS
    }
}