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/>.
*/
mod flags;
mod part_type;
mod verify;
use strum_macros::Display;
#[::serde_with::serde_as]
#[derive(::serde::Deserialize, ::serde::Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub enum PartId {
#[serde_as(as = "serde_with::hex::Hex")]
GPTFdisk(u16),
Def(part_type::PartType),
UUID(::uuid::Uuid),
}
// This is an enum to force ron to use Alignemnt(u64) so the user will never
// confuse the fields
#[derive(::serde::Deserialize, ::serde::Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub enum Alignment {
Alignment(u64),
}
#[derive(::serde::Deserialize, ::serde::Serialize, Clone)]
#[derive(::serde::Deserialize, ::serde::Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub enum PartFrom {
/// LBAs to leave free from the partition
/// this partition will have at most size X, calculated from the
/// end of the drive, or from the partitions at the end of the drive
#[derive(::serde::Deserialize, ::serde::Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub enum PartTo {
/// give a size, it will be converted to the appropriate LBA
/// use the specified percentage of the free disk
Percent(f32, Alignment),
/// use all, leaving this percentage of disk free
LeavePercent(f32, Alignment),
}
#[derive(::serde::Deserialize, ::serde::Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub enum PartUUID {
Random,
UUID(::uuid::Uuid),
}
#[derive(::serde::Deserialize, ::serde::Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct Partition {
#[serde(default)]
pub number: Option<u8>, //FIXME: 0-127, max
pub flags: Vec<flags::Flag>,
pub from: PartFrom,
pub to: PartTo,
#[derive(::serde::Deserialize, ::serde::Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub enum GptUuid {
Random,
UUID(::uuid::Uuid),
}
#[derive(::serde::Deserialize, ::serde::Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct GPT {
impl target::TargetCommon for GPT {
fn id(&self) -> &str {
self.id.as_str()
}
fn targets(&self) -> Vec<String> {
let mut ret = Vec::with_capacity(self.partitions.len());
for p in &self.partitions {
if let target::TargetId::Id(t) = &p.target {
ret.push(t.clone());
}
}
ret
}
#[derive(::serde::Deserialize, ::serde::Serialize, Clone)]
#[serde(deny_unknown_fields)]
// somehow marking this "untagged" breaks the parsing of Gpt :/
//#[serde(untagged)]
pub enum GptId {
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
impl crate::config::CfgVerify for GPT {
fn standardize(&mut self) {
// if all partitions don't have numbers, force them
let mut idx: u8 = 0;
for p in self.partitions.iter_mut() {
match p.number {
None => {
p.number = Some(idx);
idx += 1;
}
Some(_) => break,
}
}
}
fn check_consistency(
&self,
logger: &slog::Logger,
) -> Result<(), errors::ConfigError> {
// FIXME: points of failure:
// * partitions with same self.number (done)
// * overlapping partitions
// * some partitions numbered, others not (done)
for p1 in self.partitions.iter() {
for p2 in self.partitions.iter() {
if p1.number == None || p2.number == None {
::slog::error!(
logger,
"GPT: \"{}\": mixing automatic partition index and \
manual is not supported",
self.id
);
return Err(errors::ConfigError::Consistency(
"mixed automatic and manual partition number"
.to_owned(),
));
}
if let Some(num) = p2.number {
if num > 127 {
::slog::error!(
logger,
"GPT: partition numbers must be 0-127 in GPT: \
\"{}\"",
self.id
);
return Err(errors::ConfigError::Consistency(
"max partition number exceeded".to_owned(),
));
}
if p1.number == p2.number {
::slog::error!(
logger,
"GPT: multiple partitions with the same number \
({}) in GPT: \"{}\"",
num,
self.id
);
return Err(errors::ConfigError::Consistency(
"multiple partitions with the same number"
.to_owned(),
));
}
}
}
}
Ok(())
}
}