btrfs-mkfs 0.5.0

Create btrfs filesystems
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
use clap::Parser;
use std::path::PathBuf;
use uuid::Uuid;

/// Create a btrfs filesystem.
///
/// mkfs.btrfs is used to create a btrfs filesystem on a single or multiple
/// devices. The device is typically a block device but can be a file-backed
/// image as well. Multiple devices are grouped by UUID of the filesystem.
///
/// The default block group profiles for data and metadata depend on the number
/// of devices. For a single device the defaults are SINGLE for data and DUP
/// for metadata. For multiple devices the defaults are SINGLE for data and
/// RAID1 for metadata.
#[derive(Parser, Debug)]
#[command(name = "mkfs.btrfs", version)]
pub struct Arguments {
    /// Data block group profile.
    ///
    /// Valid values are raid0, raid1, raid1c3, raid1c4, raid5, raid6,
    /// raid10, single, or dup (case insensitive).
    #[arg(short = 'd', long = "data", value_name = "PROFILE")]
    pub data_profile: Option<Profile>,

    /// Metadata block group profile.
    ///
    /// Valid values are raid0, raid1, raid1c3, raid1c4, raid5, raid6,
    /// raid10, single, or dup (case insensitive). Default is DUP for a
    /// single device, RAID1 for multiple devices.
    #[arg(short = 'm', long = "metadata", value_name = "PROFILE")]
    pub metadata_profile: Option<Profile>,

    /// Mix data and metadata in the same block groups.
    ///
    /// Recommended for filesystems smaller than 1 GiB. The nodesize and
    /// sectorsize must be equal, and the data and metadata profiles must
    /// match.
    #[arg(short = 'M', long)]
    pub mixed: bool,

    /// Filesystem label (maximum 255 bytes).
    #[arg(short = 'L', long = "label", value_name = "LABEL")]
    pub label: Option<String>,

    /// Size of btree nodes.
    ///
    /// Default is 16 KiB or the page size, whichever is larger. Must be a
    /// multiple of the sectorsize and a power of 2, up to 64 KiB.
    #[arg(short = 'n', long, value_name = "SIZE")]
    pub nodesize: Option<SizeArg>,

    /// Data block allocation unit.
    ///
    /// Default is 4 KiB. Using a value different from the system page size
    /// may result in an unmountable filesystem.
    #[arg(short = 's', long, value_name = "SIZE")]
    pub sectorsize: Option<SizeArg>,

    /// Set filesystem size per device.
    ///
    /// If not set, the entire device size is used. The total filesystem
    /// size is the sum of all device sizes.
    #[arg(short = 'b', long = "byte-count", value_name = "SIZE")]
    pub byte_count: Option<SizeArg>,

    /// Checksum algorithm.
    ///
    /// Valid values are crc32c (default), xxhash, sha256, or blake2.
    #[arg(long = "checksum", alias = "csum", value_name = "TYPE")]
    pub checksum: Option<ChecksumArg>,

    /// Comma-separated list of filesystem features.
    ///
    /// Prefix a feature with ^ to disable it. Use 'list-all' to list all
    /// available features.
    #[arg(
        short = 'O',
        long = "features",
        alias = "runtime-features",
        short_alias = 'R',
        value_name = "LIST",
        value_delimiter = ','
    )]
    pub features: Vec<FeatureArg>,

    /// Specify the filesystem UUID.
    #[arg(short = 'U', long = "uuid", value_name = "UUID")]
    pub filesystem_uuid: Option<Uuid>,

    /// Specify the device UUID (sub-uuid).
    ///
    /// Only meaningful for single-device filesystems.
    #[arg(long = "device-uuid", value_name = "UUID")]
    pub device_uuid: Option<Uuid>,

    /// Force overwrite of an existing filesystem.
    #[arg(short = 'f', long)]
    pub force: bool,

    /// Do not perform whole-device TRIM.
    #[arg(short = 'K', long)]
    pub nodiscard: bool,

    /// Copy files from a directory into the filesystem image.
    #[arg(short = 'r', long = "rootdir", value_name = "DIR")]
    pub rootdir: Option<PathBuf>,

    /// Create a subdirectory as a subvolume (requires --rootdir).
    ///
    /// TYPE is one of: rw (default), ro, default, default-ro.
    /// Can be specified multiple times.
    #[arg(short = 'u', long = "subvol", value_name = "TYPE:SUBDIR")]
    pub subvol: Vec<SubvolArg>,

    /// Specify inode flags for a path (requires --rootdir).
    ///
    /// FLAGS is a comma-separated list of: nodatacow, nodatasum. Can be
    /// specified multiple times.
    #[arg(long = "inode-flags", value_name = "FLAGS:PATH")]
    pub inode_flags: Vec<InodeFlagsArg>,

    /// Compress files when populating from --rootdir.
    ///
    /// ALGO is one of: no (default), zstd, lzo, zlib. An optional
    /// compression level can be appended after a colon.
    #[arg(long = "compress", value_name = "ALGO[:LEVEL]")]
    pub compress: Option<CompressArg>,

    /// Clone file extents from --rootdir instead of copying bytes.
    #[arg(long)]
    pub reflink: bool,

    /// Shrink the filesystem to minimal size after populating from --rootdir.
    #[arg(long)]
    pub shrink: bool,

    /// Quiet mode: only print errors and warnings.
    #[arg(short = 'q', long)]
    pub quiet: bool,

    /// Increase verbosity level.
    #[arg(short = 'v', long)]
    pub verbose: bool,

    /// Block devices or image files to format.
    #[arg(required = true)]
    pub devices: Vec<PathBuf>,
}

/// Size argument with suffix support (e.g. "16k", "4m", "1g").
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SizeArg(pub u64);

impl std::str::FromStr for SizeArg {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s = s.trim();
        let (num_str, suffix) = match s.find(|c: char| c.is_alphabetic()) {
            Some(i) => (&s[..i], &s[i..]),
            None => (s, ""),
        };
        let base: u64 =
            num_str.parse().map_err(|e| format!("invalid size: {e}"))?;
        let multiplier = match suffix.to_lowercase().as_str() {
            "" => 1u64,
            "k" | "kib" => 1 << 10,
            "m" | "mib" => 1 << 20,
            "g" | "gib" => 1 << 30,
            "t" | "tib" => 1 << 40,
            "p" | "pib" => 1 << 50,
            "e" | "eib" => 1 << 60,
            _ => return Err(format!("unknown size suffix: {suffix}")),
        };
        base.checked_mul(multiplier)
            .map(SizeArg)
            .ok_or_else(|| format!("size overflow: {s}"))
    }
}

/// Block group RAID profile.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Profile {
    Single,
    Dup,
    Raid0,
    Raid1,
    Raid1c3,
    Raid1c4,
    Raid5,
    Raid6,
    Raid10,
}

impl std::str::FromStr for Profile {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "single" => Ok(Profile::Single),
            "dup" => Ok(Profile::Dup),
            "raid0" => Ok(Profile::Raid0),
            "raid1" => Ok(Profile::Raid1),
            "raid1c3" => Ok(Profile::Raid1c3),
            "raid1c4" => Ok(Profile::Raid1c4),
            "raid5" => Ok(Profile::Raid5),
            "raid6" => Ok(Profile::Raid6),
            "raid10" => Ok(Profile::Raid10),
            _ => Err(format!("unknown profile: {s}")),
        }
    }
}

impl std::fmt::Display for Profile {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Profile::Single => write!(f, "single"),
            Profile::Dup => write!(f, "DUP"),
            Profile::Raid0 => write!(f, "RAID0"),
            Profile::Raid1 => write!(f, "RAID1"),
            Profile::Raid1c3 => write!(f, "RAID1C3"),
            Profile::Raid1c4 => write!(f, "RAID1C4"),
            Profile::Raid5 => write!(f, "RAID5"),
            Profile::Raid6 => write!(f, "RAID6"),
            Profile::Raid10 => write!(f, "RAID10"),
        }
    }
}

impl Profile {
    /// The block group flag bits for this profile (ORed with type flags).
    pub fn block_group_flag(self) -> u64 {
        use btrfs_disk::raw;
        match self {
            Profile::Single => 0,
            Profile::Dup => raw::BTRFS_BLOCK_GROUP_DUP as u64,
            Profile::Raid0 => raw::BTRFS_BLOCK_GROUP_RAID0 as u64,
            Profile::Raid1 => raw::BTRFS_BLOCK_GROUP_RAID1 as u64,
            Profile::Raid1c3 => raw::BTRFS_BLOCK_GROUP_RAID1C3 as u64,
            Profile::Raid1c4 => raw::BTRFS_BLOCK_GROUP_RAID1C4 as u64,
            Profile::Raid5 => raw::BTRFS_BLOCK_GROUP_RAID5 as u64,
            Profile::Raid6 => raw::BTRFS_BLOCK_GROUP_RAID6 as u64,
            Profile::Raid10 => raw::BTRFS_BLOCK_GROUP_RAID10 as u64,
        }
    }

    /// Number of physical stripes for this profile with `n_devices` devices.
    ///
    /// For mirror-based profiles (DUP, RAID1, RAID1C3, RAID1C4) this is
    /// fixed. For striping profiles (RAID0) it equals the device count.
    pub fn num_stripes(self, n_devices: usize) -> u16 {
        match self {
            Profile::Single => 1,
            Profile::Dup | Profile::Raid1 => 2,
            Profile::Raid1c3 => 3,
            Profile::Raid1c4 => 4,
            Profile::Raid0 => n_devices as u16,
            // RAID5/6/10 not yet supported.
            Profile::Raid5 | Profile::Raid6 | Profile::Raid10 => {
                n_devices as u16
            }
        }
    }

    /// Minimum number of devices required for this profile.
    pub fn min_devices(self) -> usize {
        match self {
            Profile::Single | Profile::Dup => 1,
            Profile::Raid0 | Profile::Raid1 | Profile::Raid5 => 2,
            Profile::Raid1c3 | Profile::Raid6 => 3,
            Profile::Raid1c4 | Profile::Raid10 => 4,
        }
    }
}

/// Checksum algorithm selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChecksumArg {
    Crc32c,
    Xxhash,
    Sha256,
    Blake2,
}

impl std::str::FromStr for ChecksumArg {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "crc32c" => Ok(ChecksumArg::Crc32c),
            "xxhash" | "xxhash64" => Ok(ChecksumArg::Xxhash),
            "sha256" => Ok(ChecksumArg::Sha256),
            "blake2" | "blake2b" => Ok(ChecksumArg::Blake2),
            _ => Err(format!("unknown checksum type: {s}")),
        }
    }
}

impl std::fmt::Display for ChecksumArg {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ChecksumArg::Crc32c => write!(f, "crc32c"),
            ChecksumArg::Xxhash => write!(f, "xxhash"),
            ChecksumArg::Sha256 => write!(f, "sha256"),
            ChecksumArg::Blake2 => write!(f, "blake2"),
        }
    }
}

/// Filesystem feature that can be enabled or disabled at mkfs time.
///
/// Prefix with ^ to disable (e.g. "^no-holes").
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FeatureArg {
    pub feature: Feature,
    pub enabled: bool,
}

impl std::str::FromStr for FeatureArg {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (enabled, name) = if let Some(rest) = s.strip_prefix('^') {
            (false, rest)
        } else {
            (true, s)
        };
        let feature = name.parse::<Feature>()?;
        Ok(FeatureArg { feature, enabled })
    }
}

/// Known filesystem features.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Feature {
    MixedBg,
    Extref,
    Raid56,
    SkinnyMetadata,
    NoHoles,
    Zoned,
    Quota,
    FreeSpaceTree,
    BlockGroupTree,
    RaidStripeTree,
    Squota,
    ListAll,
}

impl std::str::FromStr for Feature {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().replace('_', "-").as_str() {
            "mixed-bg" => Ok(Feature::MixedBg),
            "extref" => Ok(Feature::Extref),
            "raid56" => Ok(Feature::Raid56),
            "skinny-metadata" => Ok(Feature::SkinnyMetadata),
            "no-holes" => Ok(Feature::NoHoles),
            "zoned" => Ok(Feature::Zoned),
            "quota" => Ok(Feature::Quota),
            "free-space-tree" | "fst" => Ok(Feature::FreeSpaceTree),
            "block-group-tree" | "bgt" => Ok(Feature::BlockGroupTree),
            "raid-stripe-tree" => Ok(Feature::RaidStripeTree),
            "squota" => Ok(Feature::Squota),
            "list-all" => Ok(Feature::ListAll),
            _ => Err(format!("unknown feature: {s}")),
        }
    }
}

impl std::fmt::Display for Feature {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Feature::MixedBg => write!(f, "mixed-bg"),
            Feature::Extref => write!(f, "extref"),
            Feature::Raid56 => write!(f, "raid56"),
            Feature::SkinnyMetadata => write!(f, "skinny-metadata"),
            Feature::NoHoles => write!(f, "no-holes"),
            Feature::Zoned => write!(f, "zoned"),
            Feature::Quota => write!(f, "quota"),
            Feature::FreeSpaceTree => write!(f, "free-space-tree"),
            Feature::BlockGroupTree => write!(f, "block-group-tree"),
            Feature::RaidStripeTree => write!(f, "raid-stripe-tree"),
            Feature::Squota => write!(f, "squota"),
            Feature::ListAll => write!(f, "list-all"),
        }
    }
}

/// Subvolume specification for --rootdir: `[TYPE:]SUBDIR`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubvolArg {
    pub subvol_type: SubvolType,
    pub path: PathBuf,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SubvolType {
    #[default]
    Rw,
    Ro,
    Default,
    DefaultRo,
}

impl std::str::FromStr for SubvolArg {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // If the path starts with "./" it is literal (no type prefix).
        if s.starts_with("./") {
            return Ok(SubvolArg {
                subvol_type: SubvolType::Rw,
                path: PathBuf::from(s),
            });
        }
        if let Some((prefix, rest)) = s.split_once(':') {
            let subvol_type = match prefix {
                "rw" => SubvolType::Rw,
                "ro" => SubvolType::Ro,
                "default" => SubvolType::Default,
                "default-ro" => SubvolType::DefaultRo,
                _ => {
                    // Not a known type prefix — treat the whole string as a path.
                    return Ok(SubvolArg {
                        subvol_type: SubvolType::Rw,
                        path: PathBuf::from(s),
                    });
                }
            };
            if rest.is_empty() {
                return Err("subvolume path cannot be empty".to_string());
            }
            Ok(SubvolArg {
                subvol_type,
                path: PathBuf::from(rest),
            })
        } else {
            Ok(SubvolArg {
                subvol_type: SubvolType::Rw,
                path: PathBuf::from(s),
            })
        }
    }
}

/// Inode flags specification for --rootdir: `FLAGS:PATH`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InodeFlagsArg {
    pub nodatacow: bool,
    pub nodatasum: bool,
    pub path: PathBuf,
}

impl std::str::FromStr for InodeFlagsArg {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (flags_str, path) = s
            .split_once(':')
            .ok_or_else(|| "expected FLAGS:PATH format".to_string())?;
        if path.is_empty() {
            return Err("path cannot be empty".to_string());
        }
        let mut nodatacow = false;
        let mut nodatasum = false;
        for flag in flags_str.split(',') {
            match flag.trim() {
                "nodatacow" => nodatacow = true,
                "nodatasum" => nodatasum = true,
                other => return Err(format!("unknown inode flag: {other}")),
            }
        }
        Ok(InodeFlagsArg {
            nodatacow,
            nodatasum,
            path: PathBuf::from(path),
        })
    }
}

/// Compression specification: `ALGO[:LEVEL]`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompressArg {
    pub algorithm: CompressAlgorithm,
    pub level: Option<u32>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompressAlgorithm {
    No,
    Zstd,
    Lzo,
    Zlib,
}

impl std::str::FromStr for CompressArg {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (algo_str, level) = if let Some((a, l)) = s.split_once(':') {
            let level: u32 =
                l.parse().map_err(|e| format!("invalid level: {e}"))?;
            (a, Some(level))
        } else {
            (s, None)
        };
        let algorithm = match algo_str.to_lowercase().as_str() {
            "no" | "none" => CompressAlgorithm::No,
            "zstd" => CompressAlgorithm::Zstd,
            "lzo" => CompressAlgorithm::Lzo,
            "zlib" => CompressAlgorithm::Zlib,
            _ => {
                return Err(format!(
                    "unknown compression algorithm: {algo_str}"
                ));
            }
        };
        if level.is_some() && algorithm == CompressAlgorithm::No {
            return Err(
                "compression level not valid with 'no' algorithm".to_string()
            );
        }
        if let Some(l) = level {
            match algorithm {
                CompressAlgorithm::Zstd if l > 15 => {
                    return Err(format!("zstd level must be 1..15, got {l}"));
                }
                CompressAlgorithm::Zlib if l > 9 => {
                    return Err(format!("zlib level must be 1..9, got {l}"));
                }
                CompressAlgorithm::Lzo if level.is_some() => {
                    return Err(
                        "lzo does not support compression levels".to_string()
                    );
                }
                _ => {}
            }
        }
        Ok(CompressArg { algorithm, level })
    }
}