Skip to content
s_gpt.rs 7 KiB
Newer Older
/*
 * Copyright (c) 2021, Luca Fulchir <luker@fenrirproject.org>
 *
 * This file is part of dfim.
 *
 * dfim is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * dfim is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with dfim.  If not, see <https://www.gnu.org/licenses/>.
 */

use super::{CheckResult, CheckStatus, NextTarget};
use crate::config::target;

// we could use lifetimes here instead of copying stuff, but
// - we should have realatively low usage anyway
// - lifetimes can get messy
// - we don't have enough modules to fully know how lifetimes will impact the
//   code.
// so for now, clone
Luker's avatar
Luker committed
pub struct GPT(target::gpt::GPT);

impl GPT {
Luker's avatar
Luker committed
    pub fn new(cfg: &target::gpt::GPT) -> Self {
        GPT(cfg.clone())
    }
}

Luker's avatar
Luker committed
impl GPT {
    /// generate a standard config that can be checked easily
    /// so that we can compare the config GPT with the device GPT
    fn generate_standard(&self, dev: &::udev::Device) -> target::gpt::GPT {
        let mut ret = target::gpt::GPT {
            id: self.0.id.clone(),
            guid: self.0.guid.clone(),
            partitions: Vec::with_capacity(self.0.partitions.len()),
        };
        // we now have to recalculate the partition actual size,
        // and move them all to LBA
        // create (but never write) a new GPT table
        // and work with that to translate to LBA
        let gpt = ::gpt::GptConfig::new().writable(false);
        let node = match dev.devnode() {
            Some(node) => node,
            None => return ret,
        };

        let mut disk = match gpt.open(node) {
            Ok(disk) => disk,
            Err(_) => {
                return ret;
            }
        };
        let mut new_partitions = ::std::collections::BTreeMap::<
            u32,
            ::gpt::partition::Partition,
        >::new();

        disk.update_partitions(new_partitions).unwrap();

        for p in &self.0.partitions {
            let p_guid = match p.part_uuid {
                target::gpt::PartUUID::Random => ::uuid::Uuid::new_v4(),
                target::gpt::PartUUID::UUID(u) => u,
            };
            if let target::gpt::PartId::UUID(p_type) = p.part_type {
                let uuid_str = p_type
                    .to_simple()
                    .encode_lower(&mut ::uuid::Uuid::encode_buffer())
                    .to_owned();
                let mut new_p = ::gpt::partition::Partition {
                    part_type_guid: ::gpt::partition_types::Type {
                        guid: &uuid_str,
                        os: ::gpt::partition_types::OperatingSystem::Custom(
                            "whatever".to_owned(),
                        ),
                    },
                    part_guid: p_guid,
                    first_lba: 0,
                    last_lba: 0,
                    flags: p.flag_bits(),
                    name: p.label.clone(),
                };
            }
        }
        ret
    }
    /*
    /// get the current drevice GPT. make it standard enough that we can
    /// easily check with what comes from generate_standard(dev)
    fn get_current(&self, dev: &::udev::Device) -> Result<target::gpt::GPT, ::std::io::Error> {

    }
    */
}

impl super::TargetApply for GPT {
    fn check(
        &mut self,
        logger: &::slog::Logger,
        cfg: &crate::state::cfg::Cfg,
        dev: &::udev::Device,
    ) -> Result<CheckResult, ::std::io::Error> {
        let node = match dev.devnode() {
            Some(node) => ::std::path::Path::new(node),
            None => {
                ::slog::error!(
                    logger,
                    "GPT: device \"{:?}\" does not have a corresponding node",
                    dev.syspath()
                );
                return Err(::std::io::Error::new(
                    ::std::io::ErrorKind::NotFound,
                    "GPT: device does not have a node",
                ));
            }
        };

        let cfg = ::gpt::GptConfig::new().writable(false);

        if let Ok(true) = crate::state::is_empty(dev) {
            return Ok(CheckResult {
                status: CheckStatus::CanBeApplied,
                // FIXME
                next: Vec::new(),
            });
        }

        let disk = match cfg.open(node) {
            Ok(disk) => disk,
            Err(e) => {
                ::slog::error!(
                    logger,
                    "GPT: device \"{:?}\" node \"{:?}\": can't open",
                    dev.syspath(),
                    node
                );
                return Err(e);
            }
        };
        let disk_guid = disk.guid();
        if let target::gpt::GptUuid::UUID(uuid) = self.0.guid {
            if *disk_guid != uuid {
                ::slog::debug!(
                    logger,
                    "GPT: device \"{:?}\" wrong guid. Got: {:?} Expected: {:?}",
                    dev.syspath(),
                    disk_guid,
                    uuid
                );
                return Ok(CheckResult {
                    status: CheckStatus::DoNotApply,
                    next: Vec::new(),
                });
            }
        }
        let disk_part = disk.partitions();
        if disk_part.len() != self.0.partitions.len() {
            ::slog::debug!(
                logger,
                "GPT: device \"{:?}\" partitions don't match",
                dev.syspath()
            );
            return Ok(CheckResult {
                status: CheckStatus::DoNotApply,
                next: Vec::new(),
            });
        }
        for (idx, p) in disk_part {
Luker's avatar
Luker committed
            match self.0.partitions.iter().find(|x| x.number == Some(*idx)) {
                None => {
                    ::slog::debug!(
                        logger,
                        "GPT: device \"{:?}\" partition {} does not match",
                        dev.syspath(),
                        idx,
                    );
                    return Ok(CheckResult {
                        status: CheckStatus::DoNotApply,
                        next: Vec::new(),
                    });
                }
                Some(self_p) => {
                    // TODO
                }
            }
        }
        Err(::std::io::Error::new(
            ::std::io::ErrorKind::Other,
            "Exec: nonZero exit status",
        ))
    }
    fn apply(
        &mut self,
        logger: &::slog::Logger,
        cfg: &crate::state::cfg::Cfg,
        cfg_dev: &crate::config::device::Device,
        dev: &::udev::Device,
    ) -> Result<Vec<NextTarget>, ::std::io::Error> {
        Err(::std::io::Error::new(
            ::std::io::ErrorKind::Other,
            "Exec: nonZero exit status",
        ))
    }
}