linuxutils-system 0.1.0

System utilities from linuxutils
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
use linuxutils_common::man::ManContent;

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

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

const LOOP_SET_FD: libc::c_ulong = 0x4C00;
const LOOP_CLR_FD: libc::c_ulong = 0x4C01;
const LOOP_SET_STATUS64: libc::c_ulong = 0x4C04;
const LOOP_GET_STATUS64: libc::c_ulong = 0x4C05;
const LOOP_SET_CAPACITY: libc::c_ulong = 0x4C07;
const LOOP_CTL_GET_FREE: libc::c_ulong = 0x4C82;

const LO_FLAGS_READ_ONLY: u32 = 1;
const _LO_FLAGS_AUTOCLEAR: u32 = 4;
const LO_FLAGS_PARTSCAN: u32 = 8;

#[repr(C)]
struct LoopInfo64 {
    lo_device: u64,
    lo_inode: u64,
    lo_rdevice: u64,
    lo_offset: u64,
    lo_sizelimit: u64,
    lo_number: u32,
    lo_encrypt_type: u32,
    lo_encrypt_key_size: u32,
    lo_flags: u32,
    lo_file_name: [u8; 64],
    lo_crypt_name: [u8; 64],
    lo_encrypt_key: [u8; 32],
    lo_init: [u64; 2],
}

#[derive(Parser)]
#[command(name = "losetup", about = "Set up and control loop devices")]
pub struct Args {
    /// List all used devices
    #[arg(short = 'a', long)]
    all: bool,

    /// Detach one or more devices
    #[arg(short = 'd', long)]
    detach: bool,

    /// Detach all used devices
    #[arg(short = 'D', long = "detach-all")]
    detach_all: bool,

    /// Find first unused device
    #[arg(short = 'f', long)]
    find: bool,

    /// Resize loop device (reread backing file size)
    #[arg(short = 'c', long = "set-capacity")]
    set_capacity: bool,

    /// List devices associated with a backing file
    #[arg(short = 'j', long)]
    associated: Option<String>,

    /// Byte offset into backing file
    #[arg(short = 'o', long)]
    offset: Option<u64>,

    /// Limit device size in bytes
    #[arg(long)]
    sizelimit: Option<u64>,

    /// Force kernel partition table scan
    #[arg(short = 'P', long)]
    partscan: bool,

    /// Set up read-only
    #[arg(short = 'r', long = "read-only")]
    read_only: bool,

    /// Print device name after setup (with -f)
    #[arg(long)]
    show: bool,

    /// Tabular list format
    #[arg(short = 'l', long)]
    list: bool,

    /// Suppress headers in list output
    #[arg(short = 'n', long)]
    noheadings: bool,

    /// Verbose mode
    #[arg(short = 'v', long)]
    verbose: bool,

    /// Device and/or file arguments
    #[arg(trailing_var_arg = true)]
    positional: Vec<String>,
}

#[derive(Cols)]
struct LoopDeviceInfo {
    #[column(header = "NAME")]
    name: String,

    #[column(right, header = "SIZELIMIT")]
    sizelimit: u64,

    #[column(right, header = "OFFSET")]
    offset: u64,

    #[column(right, header = "AUTOCLEAR")]
    autoclear: u8,

    #[column(right, header = "PARTSCAN")]
    partscan: u8,

    #[column(right, header = "RO")]
    read_only: u8,

    #[column(header = "BACK-FILE")]
    backing_file: String,

    #[column(right, header = "DIO")]
    dio: u8,
}

fn sysfs_read(path: &str) -> Option<String> {
    fs::read_to_string(path).ok().map(|s| s.trim().to_string())
}

fn list_loop_devices() -> Vec<LoopDeviceInfo> {
    let mut devices = Vec::new();

    let Ok(entries) = fs::read_dir("/sys/block") else {
        return devices;
    };

    for entry in entries.flatten() {
        let name = entry.file_name();
        let name_str = name.to_string_lossy();
        if !name_str.starts_with("loop") {
            continue;
        }

        let loop_dir = format!("/sys/block/{name_str}/loop");
        if !Path::new(&loop_dir).exists() {
            continue;
        }

        let Some(backing_file) =
            sysfs_read(&format!("{loop_dir}/backing_file"))
        else {
            continue;
        };

        let bool_val = |path: &str| -> u8 {
            sysfs_read(path)
                .map(|s| if s == "1" { 1 } else { 0 })
                .unwrap_or(0)
        };

        devices.push(LoopDeviceInfo {
            name: format!("/dev/{name_str}"),
            sizelimit: sysfs_read(&format!("{loop_dir}/sizelimit"))
                .and_then(|s| s.parse().ok())
                .unwrap_or(0),
            offset: sysfs_read(&format!("{loop_dir}/offset"))
                .and_then(|s| s.parse().ok())
                .unwrap_or(0),
            autoclear: bool_val(&format!("{loop_dir}/autoclear")),
            partscan: bool_val(&format!("{loop_dir}/partscan")),
            read_only: bool_val(&format!("/sys/block/{name_str}/ro")),
            backing_file,
            dio: bool_val(&format!("{loop_dir}/dio")),
        });
    }

    devices.sort_by(|a, b| a.name.cmp(&b.name));
    devices
}

fn print_devices(devices: &[LoopDeviceInfo], noheadings: bool) {
    let mut table = LoopDeviceInfo::to_table(devices);
    table.headings_set(!noheadings);
    let _ = print_table(&table, &mut io::stdout().lock());
}

fn print_old_style(devices: &[LoopDeviceInfo]) {
    for d in devices {
        let mut extra = String::new();
        if d.offset > 0 {
            extra.push_str(&format!(", offset {}", d.offset));
        }
        if d.sizelimit > 0 {
            extra.push_str(&format!(", sizelimit {}", d.sizelimit));
        }
        println!("{}: ({}){extra}", d.name, d.backing_file);
    }
}

fn find_free_device() -> io::Result<String> {
    let ctl = File::open("/dev/loop-control")?;
    let nr = unsafe { libc::ioctl(ctl.as_raw_fd(), LOOP_CTL_GET_FREE) };
    if nr < 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(format!("/dev/loop{nr}"))
}

fn setup_loop(
    device: &str,
    file: &str,
    offset: u64,
    sizelimit: u64,
    flags: u32,
) -> io::Result<()> {
    let backing = if flags & LO_FLAGS_READ_ONLY != 0 {
        File::open(file)?
    } else {
        File::options().read(true).write(true).open(file)?
    };

    let loop_dev = File::options().read(true).write(true).open(device)?;

    let ret = unsafe {
        libc::ioctl(loop_dev.as_raw_fd(), LOOP_SET_FD, backing.as_raw_fd())
    };
    if ret < 0 {
        return Err(io::Error::last_os_error());
    }

    let mut info = unsafe { std::mem::zeroed::<LoopInfo64>() };
    info.lo_offset = offset;
    info.lo_sizelimit = sizelimit;
    info.lo_flags = flags;

    let file_bytes = file.as_bytes();
    let copy_len = file_bytes.len().min(63);
    info.lo_file_name[..copy_len].copy_from_slice(&file_bytes[..copy_len]);

    let ret =
        unsafe { libc::ioctl(loop_dev.as_raw_fd(), LOOP_SET_STATUS64, &info) };
    if ret < 0 {
        let err = io::Error::last_os_error();
        unsafe { libc::ioctl(loop_dev.as_raw_fd(), LOOP_CLR_FD, 0) };
        return Err(err);
    }

    Ok(())
}

fn detach_loop(device: &str) -> io::Result<()> {
    let f = File::open(device)?;
    let ret = unsafe { libc::ioctl(f.as_raw_fd(), LOOP_CLR_FD, 0) };
    if ret < 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(())
    }
}

fn set_capacity(device: &str) -> io::Result<()> {
    let f = File::open(device)?;
    let ret = unsafe { libc::ioctl(f.as_raw_fd(), LOOP_SET_CAPACITY, 0) };
    if ret < 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(())
    }
}

fn show_device(device: &str) -> ExitCode {
    let Ok(f) = File::open(device) else {
        eprintln!("losetup: {device}: failed to open");
        return ExitCode::from(2);
    };

    let mut info = unsafe { std::mem::zeroed::<LoopInfo64>() };
    let ret =
        unsafe { libc::ioctl(f.as_raw_fd(), LOOP_GET_STATUS64, &mut info) };
    if ret < 0 {
        let e = io::Error::last_os_error();
        if e.raw_os_error() == Some(libc::ENXIO) {
            eprintln!("losetup: {device}: not a configured loop device");
            return ExitCode::FAILURE;
        }
        eprintln!("losetup: {device}: {e}");
        return ExitCode::from(2);
    }

    let file_name = extract_string(&info.lo_file_name);
    let mut extra = String::new();
    if info.lo_offset > 0 {
        extra.push_str(&format!(", offset {}", info.lo_offset));
    }
    if info.lo_sizelimit > 0 {
        extra.push_str(&format!(", sizelimit {}", info.lo_sizelimit));
    }
    println!("{device}: ({file_name}){extra}");
    ExitCode::SUCCESS
}

fn extract_string(bytes: &[u8]) -> String {
    let end = bytes.iter().position(|&b| b == 0).unwrap_or(bytes.len());
    String::from_utf8_lossy(&bytes[..end]).to_string()
}

pub fn run(args: Args) -> ExitCode {
    if args.detach_all {
        let devices = list_loop_devices();
        let mut failed = false;
        for d in &devices {
            if let Err(e) = detach_loop(&d.name) {
                eprintln!("losetup: {}: {e}", d.name);
                failed = true;
            }
        }
        return if failed {
            ExitCode::FAILURE
        } else {
            ExitCode::SUCCESS
        };
    }

    if args.detach {
        if args.positional.is_empty() {
            eprintln!("losetup: --detach requires at least one device");
            return ExitCode::FAILURE;
        }
        let mut failed = false;
        for dev in &args.positional {
            if let Err(e) = detach_loop(dev) {
                eprintln!("losetup: {dev}: {e}");
                failed = true;
            }
        }
        return if failed {
            ExitCode::FAILURE
        } else {
            ExitCode::SUCCESS
        };
    }

    if args.set_capacity {
        let dev = match args.positional.first() {
            Some(d) => d,
            None => {
                eprintln!("losetup: --set-capacity requires a device");
                return ExitCode::FAILURE;
            }
        };
        return match set_capacity(dev) {
            Ok(()) => ExitCode::SUCCESS,
            Err(e) => {
                eprintln!("losetup: {dev}: {e}");
                ExitCode::FAILURE
            }
        };
    }

    if let Some(ref file) = args.associated {
        let canonical = fs::canonicalize(file)
            .ok()
            .and_then(|p| p.to_str().map(|s| s.to_string()))
            .unwrap_or_else(|| file.clone());

        let devices: Vec<_> = list_loop_devices()
            .into_iter()
            .filter(|d| {
                d.backing_file == canonical
                    || d.backing_file == *file
                    || d.backing_file.trim_end() == canonical
            })
            .collect();

        print_old_style(&devices);
        return ExitCode::SUCCESS;
    }

    if args.find {
        let dev = match find_free_device() {
            Ok(d) => d,
            Err(e) => {
                eprintln!("losetup: failed to find a free loop device: {e}");
                return ExitCode::FAILURE;
            }
        };

        if let Some(file) = args.positional.first() {
            let mut flags = 0u32;
            if args.read_only {
                flags |= LO_FLAGS_READ_ONLY;
            }
            if args.partscan {
                flags |= LO_FLAGS_PARTSCAN;
            }

            if let Err(e) = setup_loop(
                &dev,
                file,
                args.offset.unwrap_or(0),
                args.sizelimit.unwrap_or(0),
                flags,
            ) {
                eprintln!("losetup: {dev}: {e}");
                return ExitCode::FAILURE;
            }

            if args.show {
                println!("{dev}");
            }
        } else {
            println!("{dev}");
        }

        return ExitCode::SUCCESS;
    }

    // List mode: no args = tabular, -a alone = old-style, -l or -a -l = tabular
    if args.positional.is_empty()
        && (args.all
            || args.list
            || (!args.detach && !args.find && !args.set_capacity))
    {
        let devices = list_loop_devices();
        if args.all && !args.list {
            print_old_style(&devices);
        } else {
            print_devices(&devices, args.noheadings);
        }
        return ExitCode::SUCCESS;
    }

    // Show or setup a specific device
    if args.positional.len() == 1 {
        return show_device(&args.positional[0]);
    }

    if args.positional.len() == 2 {
        let dev = &args.positional[0];
        let file = &args.positional[1];

        let mut flags = 0u32;
        if args.read_only {
            flags |= LO_FLAGS_READ_ONLY;
        }
        if args.partscan {
            flags |= LO_FLAGS_PARTSCAN;
        }

        return match setup_loop(
            dev,
            file,
            args.offset.unwrap_or(0),
            args.sizelimit.unwrap_or(0),
            flags,
        ) {
            Ok(()) => {
                if args.verbose {
                    println!("{dev}: set up with {file}");
                }
                ExitCode::SUCCESS
            }
            Err(e) => {
                eprintln!("losetup: {dev}: {e}");
                ExitCode::FAILURE
            }
        };
    }

    eprintln!("losetup: bad usage, try 'losetup --help'");
    ExitCode::FAILURE
}