Skip to content
s_gpt.rs 18.6 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/>.
 */

Luker's avatar
Luker committed
use super::{CheckStatus, NextTarget};
use crate::config::trgt;

// 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 trgts to fully know how lifetimes will impact the
//   code.
// so for now, clone
pub struct GPT(trgt::gpt::GPT);

impl GPT {
    pub fn new(cfg: &trgt::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
Luker's avatar
Luker committed
    fn make_standard(
        &self,
        dev: &::udev::Device,
    ) -> Result<trgt::gpt::GPT, ::std::io::Error> {
Luker's avatar
Luker committed
        let mut ret = self.0.clone();
        let (min_lba, max_lba, disk_alignment) = {
Luker's avatar
Luker committed
            // enclose this in a block just to drop the ::gpt::Disk early

Luker's avatar
Luker committed
            // 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 node = dev.devnode().ok_or(::std::io::Error::new(
                ::std::io::ErrorKind::Other,
                "device does not have a node",
            ))?;
Luker's avatar
Luker committed
            let gpt =
                ::gpt::GptConfig::new().writable(false).initialized(false);
            let mut disk = gpt.open(node)?;
Luker's avatar
Luker committed
            let new_partitions = ::std::collections::BTreeMap::<
                u32,
                ::gpt::partition::Partition,
            >::new();
Luker's avatar
Luker committed

            disk.update_partitions(new_partitions.clone()).unwrap();
            let disk_alignment = match disk.logical_block_size() {
                ::gpt::disk::LogicalBlockSize::Lb512 => 512,
                ::gpt::disk::LogicalBlockSize::Lb4096 => 4096,
            };
            let (min_lba, max_lba) =
                disk.find_free_sectors().get(0).unwrap().clone();
            (min_lba, max_lba, disk_alignment)
Luker's avatar
Luker committed
        };
Luker's avatar
Luker committed

Luker's avatar
Luker committed
        use trgt::gpt::{align_raw, Alignment, Index, PartFrom, PartTo};
        // convert all to Index(LBA), Alignment(LBA) for ease of usage
        for p in &mut ret.partitions {
            p.from.lba_align(disk_alignment, min_lba, max_lba);
            p.to.lba_align(disk_alignment, min_lba, max_lba);
Luker's avatar
Luker committed
        }
Luker's avatar
Luker committed
        let mut last_lba: u64 = min_lba - 1;
        // now convert everything to
        // PartFrom::Index(Index::LBA(_), Alignment/LBA(0))
        for p in &mut ret.partitions {
Luker's avatar
Luker committed
            p.from = match &p.from {
                idx @ PartFrom::Index(_, _) => idx.clone(),
                PartFrom::SkipFree(
                    Index::LBA(l),
                    Alignment::Alignment(Index::LBA(al)),
                ) => {
Luker's avatar
Luker committed
                    let start_from = last_lba + 1 + l;
                    let start_aligned = align_raw(start_from, *al);
                    last_lba = start_aligned;
Luker's avatar
Luker committed
                    PartFrom::Index(
                        Index::LBA(start_aligned),
Luker's avatar
Luker committed
                        Alignment::Alignment(Index::LBA(0)),
                    )
                }
                PartFrom::SkipFreePercent(
                    perc,
                    Alignment::Alignment(Index::LBA(al)),
                ) => {
Luker's avatar
Luker committed
                    let skip_lba: u64 = (((max_lba - last_lba) as f64) / 100.0
                        * perc)
                        .ceil() as u64;
                    let start_from = last_lba + 1 + skip_lba;
                    let start_aligned = align_raw(start_from, *al);
                    last_lba = start_aligned;
Luker's avatar
Luker committed
                    PartFrom::Index(
                        Index::LBA(start_aligned),
Luker's avatar
Luker committed
                        Alignment::Alignment(Index::LBA(0)),
                    )
                }
                _ => {
                    assert!(false, "GPT: from: not everything is LBA");
                    return Err(::std::io::Error::new(
                        ::std::io::ErrorKind::Other,
                        "internal error: \"from\" should be LBA",
                    ));
Luker's avatar
Luker committed
                }
            };
            p.to = match &p.to {
                idx @ PartTo::Index(_, _) => idx.clone(),
                PartTo::Size(
                    Index::LBA(l),
                    Alignment::Alignment(Index::LBA(al)),
                ) => {
Luker's avatar
Luker committed
                    let up_to = last_lba + l;
                    let up_to_aligned = align_raw(up_to, *al);
                    last_lba = up_to_aligned;
Luker's avatar
Luker committed
                    PartTo::Index(
                        Index::LBA(up_to_aligned),
Luker's avatar
Luker committed
                        Alignment::Alignment(Index::LBA(0)),
                    )
                }
                PartTo::Leave(
                    Index::LBA(l),
                    Alignment::Alignment(Index::LBA(al)),
                ) => {
Luker's avatar
Luker committed
                    let up_to = max_lba - l;
                    let up_to_aligned = align_raw(up_to, *al);
                    last_lba = up_to_aligned;
Luker's avatar
Luker committed
                    PartTo::Index(
                        Index::LBA(up_to_aligned),
Luker's avatar
Luker committed
                        Alignment::Alignment(Index::LBA(0)),
                    )
                }
                PartTo::Percent(perc, Alignment::Alignment(Index::LBA(al))) => {
Luker's avatar
Luker committed
                    let size: u64 = (((max_lba - last_lba) as f64) / 100.0
                        * perc)
                        .ceil() as u64;
                    let up_to = last_lba + size;
                    let up_to_aligned = align_raw(up_to, *al);
                    last_lba = up_to_aligned;
Luker's avatar
Luker committed
                    PartTo::Index(
                        Index::LBA(up_to_aligned),
Luker's avatar
Luker committed
                        Alignment::Alignment(Index::LBA(0)),
                    )
                }
                PartTo::LeavePercent(
                    perc,
                    Alignment::Alignment(Index::LBA(al)),
                ) => {
Luker's avatar
Luker committed
                    let size: u64 = (((max_lba - last_lba) as f64) / 100.0
                        * perc)
                        .ceil() as u64;
                    let up_to = max_lba - size;
                    let up_to_aligned = align_raw(up_to, *al);
                    last_lba = up_to_aligned;
Luker's avatar
Luker committed
                    PartTo::Index(
                        Index::LBA(up_to_aligned),
Luker's avatar
Luker committed
                        Alignment::Alignment(Index::LBA(0)),
                    )
                }
                _ => {
                    assert!(false, "GPT: to: not everything is LBA");
                    return Err(::std::io::Error::new(
                        ::std::io::ErrorKind::Other,
                        "internal error: \"to\" should be LBA",
                    ));
                }
            };
            if let PartFrom::Index(Index::LBA(l_f), _) = p.from {
                if let PartTo::Index(Index::LBA(l_t), _) = p.to {
Luker's avatar
Luker committed
                    if l_f > l_t || l_t > max_lba || l_f < min_lba {
                        return Err(::std::io::Error::new(
                            ::std::io::ErrorKind::InvalidData,
                            "Wrong partition boundaries",
                        ));
                    }
Luker's avatar
Luker committed
    }
    /// get the current device GPT. make it standard enough that we can
Luker's avatar
Luker committed
    /// easily check with what comes from make_standard(dev)
    fn get_current_conf(
Luker's avatar
Luker committed
        dev: &::udev::Device,
    ) -> Result<trgt::gpt::GPT, ::std::io::Error> {
        let gpt = ::gpt::GptConfig::new().writable(false);
        let node = dev.devnode().ok_or(::std::io::Error::new(
            ::std::io::ErrorKind::Other,
            "device does not have a node",
        ))?;
        let disk = gpt.open(node)?;
        let disk_header =
            disk.primary_header().ok_or(::std::io::Error::new(
                ::std::io::ErrorKind::InvalidData,
                "Failed to retrieve GPT primary header",
            ))?;

        let mut ret = trgt::gpt::GPT {
Luker's avatar
Luker committed
            id: "DFIM generated for ".to_owned()
Luker's avatar
Luker committed
                + dev.syspath().to_str().unwrap(),
            guid: trgt::gpt::GptUuid::UUID(disk.guid().clone()),
            max_partitions: disk_header.part_size,
            partitions: Vec::with_capacity(disk_header.num_parts as usize),
        };
Luker's avatar
Luker committed

Luker's avatar
Luker committed
        for (num, disk_p) in disk.partitions() {
            use trgt::gpt::{
                Alignment, Index, PartFrom, PartId, PartTo, PartUUID, Partition,
            };
            let p = Partition {
                label: disk_p.name.clone(),
                number: ::std::num::NonZeroU32::new(*num).ok_or(
                    ::std::io::Error::new(
                        ::std::io::ErrorKind::InvalidData,
                        "Found partition id 0 on drive",
                    ),
                )?,
Luker's avatar
Luker committed
                part_type: PartId::UUID(
                    ::uuid::Uuid::parse_str(&disk_p.part_type_guid.guid)
                        .unwrap(),
                ),
                part_uuid: PartUUID::UUID(disk_p.part_guid),
                flags: trgt::gpt::Flag::from_bits(disk_p.flags),
                from: PartFrom::Index(
                    Index::LBA(disk_p.first_lba),
                    Alignment::Alignment(Index::LBA(0)),
                ),
                to: PartTo::Index(
                    Index::LBA(disk_p.last_lba),
                    Alignment::Alignment(Index::LBA(0)),
                ),
Luker's avatar
Luker committed
                target: trgt::TargetId::Id("DFIM generated None".to_owned()),
Luker's avatar
Luker committed
            };
            ret.partitions.push(p);
        }
        Ok(ret)
Luker's avatar
Luker committed
    }
    fn find_children(
        &self,
        cfg: &crate::config::trgt::gpt::GPT,
        dev: &::udev::Device,
    ) -> Result<Vec<NextTarget>, ::std::io::Error> {
        let mut scanner = ::udev::Enumerator::new()?;
        scanner.match_parent(dev)?;
        scanner.match_subsystem(
            crate::config::device::DevType::Partition.to_str(),
        )?;

        let mut ret = Vec::with_capacity(cfg.partitions.len());
        let children = scanner.scan_devices()?;

        for child in children {
            // these should be partitions of the disk
            // ...but how do we distinguish one from the other?
            match child.parent() {
                None => continue,
                Some(parent) => {
                    // I am unsure if we match also the children's children
                    // Therefore make sure we are
                    // really only looking at the first
                    // children of the dev we are given
                    if parent.syspath() != dev.syspath() {
                        continue;
                    }
                }
            }
            if None == child.devnode() {
                assert!(false, "We have a partition without a node?");
                continue;
            }
            let partnum = match child.sysnum() {
                None => {
                    assert!(false, "Partition without sys number?");
                    continue;
                }
                Some(partnum) => ::std::num::NonZeroU32::new(partnum as u32)
                    .ok_or(::std::io::Error::new(
                        ::std::io::ErrorKind::InvalidData,
                        "dev num 0 but we can't have partition 0",
                    ))?,
            };
            let p = match cfg.partitions.iter().find(|x| x.number == partnum) {
                Some(p) => p,
                None => {
                    return Err(::std::io::Error::new(
                        ::std::io::ErrorKind::InvalidData,
                        "We tought we had partitions we don't have",
                    ));
                }
            };
            ret.push(NextTarget {
                id: p.target.id().to_owned(),
                dev: child,
            });
        }

        Ok(ret)
    }
Luker's avatar
Luker committed
}

impl super::TargetApply for GPT {
Luker's avatar
Luker committed
    fn dev_to_config(
        &mut self,
Luker's avatar
Luker committed
        _logger: &::slog::Logger,
        _cfg: &crate::state::cfg::Cfg,
        _cfg_dev: &crate::config::device::Device,
        dev: &::udev::Device,
Luker's avatar
Luker committed
    ) -> Result<crate::config::trgt::Target, ::std::io::Error> {
        let current = self.make_standard(dev)?;
        Ok(crate::config::trgt::Target::GPT(current))
    }
    fn get_current(
        &mut self,
        _logger: &::slog::Logger,
        _cfg: &crate::state::cfg::Cfg,
        _cfg_dev: &crate::config::device::Device,
        dev: &::udev::Device,
    ) -> Result<super::GetCurrentResult, ::std::io::Error> {
        let current = GPT::get_current_conf(dev)?;
Luker's avatar
Luker committed

Luker's avatar
Luker committed
        match self.0 == current {
            true => Ok(super::GetCurrentResult {
                status: CheckStatus::AlreadyApplied,
Luker's avatar
Luker committed
                conf: crate::config::trgt::Target::GPT(current),
Luker's avatar
Luker committed
                next: self.find_children(&self.0, dev)?,
            }),
            false => Ok(super::GetCurrentResult {
                status: CheckStatus::CanBeApplied,
Luker's avatar
Luker committed
                conf: crate::config::trgt::Target::GPT(current),
                next: Vec::new(),
Luker's avatar
Luker committed
            }),
        }
    }
    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> {
Luker's avatar
Luker committed
        let node = dev.devnode().ok_or(::std::io::Error::new(
            ::std::io::ErrorKind::Other,
Luker's avatar
Luker committed
            "device does not have a node",
        ))?;
        if cfg_dev.only_if_empty {
            if !crate::state::is_empty(dev)? {
                return Err(::std::io::Error::new(
                    ::std::io::ErrorKind::InvalidData,
                    "GPT: can't apply: not empty disk",
                ));
            }
        }
Luker's avatar
Luker committed
        let to_be_applied = self.make_standard(dev)?;
Luker's avatar
Luker committed

Luker's avatar
Luker committed
        let gptconf = ::gpt::GptConfig::new()
            .writable(!cfg.main.dry_run)
            .initialized(false);
Luker's avatar
Luker committed
        let mut disk = gptconf.open(node)?;

        if let trgt::gpt::GptUuid::UUID(disk_uuid) = to_be_applied.guid {
            disk.update_guid(Some(disk_uuid))?;
        }
        let mut disk_parts = ::std::collections::BTreeMap::<
            u32,
            ::gpt::partition::Partition,
        >::new();

        use trgt::gpt::{Index, PartFrom, PartId, PartTo, PartUUID};
        for conf_p in to_be_applied.partitions.iter() {
            let from_lba = match conf_p.from {
                PartFrom::Index(Index::LBA(from_lba), _) => from_lba,
                _ => {
                    return Err(::std::io::Error::new(
                        ::std::io::ErrorKind::Other,
                        "GPT: nonstandard standard partition",
                    ));
                }
            };
            let to_lba = match conf_p.to {
                PartTo::Index(Index::LBA(to_lba), _) => to_lba,
                _ => {
                    return Err(::std::io::Error::new(
                        ::std::io::ErrorKind::Other,
                        "GPT: nonstandard standard partition",
                    ));
                }
            };
            let type_uuid = match conf_p.part_type {
                PartId::UUID(type_uuid) => type_uuid,
                _ => {
                    return Err(::std::io::Error::new(
                        ::std::io::ErrorKind::Other,
                        "GPT: nonstandard standard partition",
                    ));
                }
            };
            let part_uuid = match conf_p.part_uuid {
                PartUUID::UUID(part_uuid) => part_uuid.clone(),
                PartUUID::Random => ::uuid::Uuid::new_v4(),
            };
            let p = ::gpt::partition::Partition {
                part_type_guid: ::gpt::partition_types::Type::from_uuid(
                    &type_uuid,
                )
                .unwrap(),
                part_guid: part_uuid,
                first_lba: from_lba,
                last_lba: to_lba,
Luker's avatar
Luker committed
                flags: conf_p.flag_bits(),
Luker's avatar
Luker committed
                name: conf_p.label.clone(),
            };
            disk_parts.insert(conf_p.number.get(), p);
Luker's avatar
Luker committed
        }

Luker's avatar
Luker committed
        match disk.update_partitions_embedded(disk_parts, self.0.max_partitions)
        {
            Ok(_) => {}
            Err(e) => {
                ::slog::error!(
                    logger,
                    "GPT: target {}: wrong partition definition?",
                    self.0.id,
                );
                return Err(e);
            }
        }
        match cfg.main.dry_run {
            true => {
                ::slog::info!(
                    logger,
                    "DRY RUN: GPT: refusing to write target {} on drive {:?}",
                    self.0.id,
                    dev.syspath()
                );
                return Ok(Vec::new());
            }
            false => {
                match disk.write() {
                    Err(e) => {
                        ::slog::debug!(
                            logger,
                            "GPT: error writing table: {:?}",
                            e
                        );
                        return Err(e);
                    }
                    Ok(_) => {}
                }
                use std::os::unix::io::AsRawFd;
Luker's avatar
Luker committed
                let disk_file = match ::std::fs::File::open(node) {
Luker's avatar
Luker committed
                    Ok(disk_file) => disk_file,
                    Err(e) => {
                        ::slog::debug!(
                            logger,
                            "GPT: can't reload partition table: {:?}",
                            e
                        );
                        return Err(e);
                    }
                };
Luker's avatar
Luker committed
                let fd = disk_file.as_raw_fd();
Luker's avatar
Luker committed
                // reread partition table
                // include/uapi/linux/fs.h: #define BLKRRPART  _IO(0x12,95)
                ::nix::ioctl_none!(reread_partition_table, 0x12, 95);
                unsafe {
Luker's avatar
Luker committed
                    if reread_partition_table(fd) != Ok(0) {
                        ::slog::error!(
                            logger,
                            "Partition table reload failed. Trying to \
                             continue..."
                        );
                    }
Luker's avatar
Luker committed
                }
Luker's avatar
Luker committed
        }
        self.find_children(&to_be_applied, dev)