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
extern crate libudev;
extern crate regex;
extern crate uuid;

use regex::Regex;
use uuid::Uuid;

use std::fs;
use std::io::ErrorKind;
use std::path::PathBuf;
use std::process::{Command, Output};

// Formats a block device at Path p with XFS
/// This is used for formatting btrfs filesystems and setting the metadata profile
#[derive(Clone, Debug)]
pub enum MetadataProfile {
    Raid0,
    Raid1,
    Raid5,
    Raid6,
    Raid10,
    Single,
    Dup,
}

// This will be used to make intelligent decisions about setting up the device
/// Device information that is gathered with udev
#[derive(Debug)]
pub struct Device {
    pub id: Option<Uuid>,
    pub name: String,
    pub media_type: MediaType,
    pub capacity: u64,
    pub fs_type: FilesystemType,
}

/// What type of media has been detected.
#[derive(Debug)]
pub enum MediaType {
    /// AKA SSD
    SolidState,
    /// Regular rotational disks
    Rotational,
    /// Special loopback device
    Loopback,
    Unknown,
}

/// What type of filesystem
#[derive(Debug)]
pub enum FilesystemType {
    Btrfs,
    Ext4,
    Xfs,
    Unknown,
}

impl FilesystemType {
    pub fn from_str(fs_type: &str) -> FilesystemType {
        match fs_type {
            "btrfs" => FilesystemType::Btrfs,
            "ext4" => FilesystemType::Ext4,
            "xfs" => FilesystemType::Xfs,
            _ => FilesystemType::Unknown,
        }
    }
}

impl MetadataProfile {
    pub fn to_string(self) -> String {
        match self {
            MetadataProfile::Raid0 => "raid0".to_string(),
            MetadataProfile::Raid1 => "raid1".to_string(),
            MetadataProfile::Raid5 => "raid5".to_string(),
            MetadataProfile::Raid6 => "raid6".to_string(),
            MetadataProfile::Raid10 => "raid10".to_string(),
            MetadataProfile::Single => "single".to_string(),
            MetadataProfile::Dup => "dup".to_string(),
        }
    }
}

/// This allows you to tweak some settings when you're formatting the filesystem
#[derive(Debug)]
pub enum Filesystem {
    Btrfs {
        metadata_profile: MetadataProfile,
        leaf_size: u64,
        node_size: u64,
    },
    Ext4 {
        inode_size: u64,
        reserved_blocks_percentage: u8,
    },
    Xfs {
        /// This is optional.  Boost knobs are on by default:
        /// http://xfs.org/index.php/XFS_FAQ#Q:_I_want_to_tune_my_XFS_filesystems_for_.3Csomething.3E
        inode_size: Option<u64>,
        force: bool,
    },
}

impl Filesystem {
    /// Create a new Filesystem with defaults.
    pub fn new(name: &str) -> Filesystem {
        match name.trim() {
            // Defaults.  Can be changed as needed by the caller
            "xfs" => {
                Filesystem::Xfs {
                    inode_size: None,
                    force: false,
                }
            }
            "btrfs" => {
                Filesystem::Btrfs {
                    metadata_profile: MetadataProfile::Single,
                    leaf_size: 32768,
                    node_size: 32768,
                }
            }
            "ext4" => {
                Filesystem::Ext4 {
                    inode_size: 256,
                    reserved_blocks_percentage: 0,
                }
            }
            _ => {
                Filesystem::Xfs {
                    inode_size: None,
                    force: false,
                }
            }
        }
    }
}

fn run_command(command: &str, arg_list: &Vec<String>, as_root: bool) -> Output {
    if as_root {
        let mut cmd = Command::new("sudo");
        cmd.arg(command);
        for arg in arg_list {
            cmd.arg(&arg);
        }
        let output = cmd.output().unwrap_or_else(|e| panic!("failed to execute process: {} ", e));
        return output;
    } else {
        let mut cmd = Command::new(command);
        for arg in arg_list {
            cmd.arg(&arg);
        }
        let output = cmd.output().unwrap_or_else(|e| panic!("failed to execute process: {} ", e));
        return output;
    }
}

// Install xfsprogs, btrfs-tools, etc for mkfs to succeed
fn install_utils() -> Result<i32, String> {
    let arg_list = vec!["install".to_string(),
                        "-y".to_string(),
                        "btrfs-tools".to_string(),
                        "xfsprogs".to_string()];
    return process_output(run_command("/usr/bin/apt-get", &arg_list, true));
}

/// Utility function to mount a device at a mount point
/// NOTE: This assumes the device is formatted at this point.  The mount
/// will fail if the device isn't formatted.
pub fn mount_device(device: &Device, mount_point: &str) -> Result<i32, String> {
    let mut arg_list: Vec<String> = Vec::new();
    match device.id {
        Some(id) => {
            arg_list.push("-U".to_string());
            arg_list.push(id.hyphenated().to_string());
        }
        None => {
            arg_list.push(format!("/dev/{}", device.name));
        }
    };
    // arg_list.push("-t".to_string());
    // match device.fs_type{
    // FilesystemType::Btrfs => {
    // arg_list.push("btrfs".to_string());
    // },
    // FilesystemType::Ext4 => {
    // arg_list.push("ext4".to_string());
    // },
    // FilesystemType::Xfs => {
    // arg_list.push("xfs".to_string());
    // },
    // FilesystemType::Unknown => {
    // return Err("Unable to mount unknown filesystem type".to_string());
    // }
    // };
    //
    arg_list.push(mount_point.to_string());

    return process_output(run_command("mount", &arg_list, true));
}

fn process_output(output: Output) -> Result<i32, String> {
    if output.status.success() {
        Ok(0)
    } else {
        let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
        Err(stderr)
    }
}

/// Utility to format a block device with a given filesystem.
pub fn format_block_device(device: &PathBuf, filesystem: &Filesystem) -> Result<i32, String> {
    match filesystem {
        &Filesystem::Btrfs { ref metadata_profile, ref leaf_size, ref node_size } => {
            let mut arg_list: Vec<String> = Vec::new();

            arg_list.push("-m".to_string());
            arg_list.push(metadata_profile.clone().to_string());

            arg_list.push("-l".to_string());
            arg_list.push(leaf_size.to_string());

            arg_list.push("-n".to_string());
            arg_list.push(node_size.to_string());

            arg_list.push(device.to_string_lossy().to_string());
            // Check if mkfs.xfs is installed
            match fs::metadata("/sbin/mkfs.btrfs") {
                Ok(_) => {}
                Err(e) => {
                    match e.kind() {
                        ErrorKind::NotFound => {
                            try!(install_utils());
                        }
                        _ => {}
                    }
                }
            }
            return process_output(run_command("mkfs.btrfs", &arg_list, true));
        }
        &Filesystem::Xfs { ref inode_size, ref force } => {
            let mut arg_list: Vec<String> = Vec::new();

            if (*inode_size).is_some() {
                arg_list.push("-i".to_string());
                arg_list.push(inode_size.unwrap().to_string());
            }

            if *force {
                arg_list.push("-f".to_string());
            }

            arg_list.push(device.to_string_lossy().to_string());

            // Check if mkfs.xfs is installed
            match fs::metadata("/sbin/mkfs.xfs") {
                Ok(_) => {}
                Err(e) => {
                    match e.kind() {
                        ErrorKind::NotFound => {
                            try!(install_utils());
                        }
                        _ => {}
                    }
                }
            }
            return process_output(run_command("/sbin/mkfs.xfs", &arg_list, true));
        }
        &Filesystem::Ext4 { ref inode_size, ref reserved_blocks_percentage } => {
            let mut arg_list: Vec<String> = Vec::new();

            arg_list.push("-I".to_string());
            arg_list.push(inode_size.to_string());

            arg_list.push("-m".to_string());
            arg_list.push(reserved_blocks_percentage.to_string());

            arg_list.push(device.to_string_lossy().to_string());

            return process_output(run_command("mkfs.ext4", &arg_list, true));
        }
    }
}

#[test]
fn test_get_device_info() {
    print!("{:?}", get_device_info(&PathBuf::from("/dev/sda1")));
    print!("{:?}", get_device_info(&PathBuf::from("/dev/loop0")));
}

fn get_size(device: &libudev::Device) -> Option<u64> {
    match device.attribute_value("size") {
        // 512 is the block size
        Some(size_str) => {
            let size = size_str.to_str().unwrap_or("0").parse::<u64>().unwrap_or(0);
            return Some(size * 512);
        }
        None => return None,
    }
}

fn get_uuid(device: &libudev::Device) -> Option<Uuid> {
    match device.property_value("ID_FS_UUID") {
        Some(value) => {
            match Uuid::parse_str(value.to_str().unwrap()) {
                Ok(result) => return Some(result),
                Err(_) => return None,
            }
        }
        None => return None,
    }
}

fn get_fs_type(device: &libudev::Device) -> FilesystemType {
    match device.property_value("ID_FS_TYPE") {
        Some(s) => {
            let value = s.to_string_lossy();
            match value.as_ref() {
                "btrfs" => return FilesystemType::Btrfs,
                "xfs" => return FilesystemType::Xfs,
                "ext4" => return FilesystemType::Ext4,
                _ => return FilesystemType::Unknown,
            }
        }
        None => return FilesystemType::Unknown,
    }
}

fn get_media_type(device: &libudev::Device) -> MediaType {
    let device_sysname = device.sysname().to_str();
    let loop_regex = Regex::new(r"loop\d+").unwrap();

    if loop_regex.is_match(device_sysname.unwrap()) {
        return MediaType::Loopback;
    }

    match device.property_value("ID_ATA_ROTATION_RATE_RPM") {
        Some(value) => {
            if value == "0" {
                return MediaType::SolidState;
            } else {
                return MediaType::Rotational;
            }
        }
        None => return MediaType::Unknown,
    }
}

/// Checks to see if the subsystem this device is using is block
pub fn is_block_device(device_path: &PathBuf) -> Result<bool, String> {
    let context = try!(libudev::Context::new().map_err(|e| e.to_string()));
    let mut enumerator = try!(libudev::Enumerator::new(&context).map_err(|e| e.to_string()));
    let devices = try!(enumerator.scan_devices().map_err(|e| e.to_string()));

    let sysname = try!(device_path.file_name()
        .ok_or(format!("Unable to get file_name on device {:?}", device_path)));

    for device in devices {
        if sysname == device.sysname() {
            if device.subsystem() == "block" {
                return Ok(true);
            }
        }
    }

    return Err(format!("Unable to find device with name {:?}", device_path));
}

/// Returns device information that is gathered with udev.
pub fn get_device_info(device_path: &PathBuf) -> Result<Device, String> {
    let context = try!(libudev::Context::new().map_err(|e| e.to_string()));
    let mut enumerator = try!(libudev::Enumerator::new(&context).map_err(|e| e.to_string()));
    let devices = try!(enumerator.scan_devices().map_err(|e| e.to_string()));

    let sysname = try!(device_path.file_name()
        .ok_or(format!("Unable to get file_name on device {:?}", device_path)));

    for device in devices {
        if sysname == device.sysname() {
            // This is going to get complicated
            if device.subsystem() == "block" {
                // Ok we're a block device
                let id: Option<Uuid> = get_uuid(&device);
                let media_type = get_media_type(&device);
                let capacity = match get_size(&device) {
                    Some(size) => size,
                    None => 0,
                };
                let fs_type = get_fs_type(&device);

                return Ok(Device {
                    id: id,
                    name: sysname.to_string_lossy().to_string(),
                    media_type: media_type,
                    capacity: capacity,
                    fs_type: fs_type,
                });
            }
        }
    }
    return Err(format!("Unable to find device with name {:?}", device_path));
}