gpt 4.1.0

A pure-Rust library to work with GPT partition tables.
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
//! A pure-Rust library to work with GPT partition tables.
//!
//! It provides support for manipulating (R/W) GPT headers and partition
//! tables. Raw disk devices as well as disk images are supported.
//!
//! ```
//! use gpt;
//! use std::convert::TryFrom;
//! use std::io::{Read, Seek};
//!
//! fn inspect_disk() {
//!     let diskpath = std::path::Path::new("/dev/sdz");
//!
//!     let disk = gpt::GptConfig::new()
//!         .open(diskpath).expect("failed to open disk");
//!
//!     println!("Disk header: {:#?}", disk.primary_header());
//!     println!("Partition layout: {:#?}", disk.partitions());
//! }
//!
//! fn create_partition() {
//!     let diskpath = std::path::Path::new("/tmp/chris.img");
//!     let mut disk = gpt::GptConfig::new().writable(true)
//!         .create(diskpath).expect("failed to open disk");
//!     let result = disk.add_partition(
//!         "rust_partition",
//!         100,
//!         gpt::partition_types::LINUX_FS,
//!         0,
//!         None
//!     );
//!     disk.write().unwrap();
//! }
//!
//! /// Demonstrates how to create a new partition table without anything pre-existing
//! fn create_partition_in_ram() {
//!     const TOTAL_BYTES: usize = 1024 * 64;
//!     let mut mem_device = std::io::Cursor::new(vec![0u8; TOTAL_BYTES]);
//!
//!     // Create a protective MBR at LBA0
//!     let mbr = gpt::mbr::ProtectiveMBR::with_lb_size(
//!         u32::try_from((TOTAL_BYTES / 512) - 1).unwrap_or(0xFF_FF_FF_FF));
//!     mbr.overwrite_lba0(&mut mem_device).expect("failed to write MBR");
//!
//!     let mut gdisk = gpt::GptConfig::default()
//!         .writable(true)
//!         .logical_block_size(gpt::disk::LogicalBlockSize::Lb512)
//!         .create_from_device(mem_device, None)
//!         .expect("failed to crate GptDisk");
//!
//!     // At this point, gdisk.primary_header() and gdisk.backup_header() are populated...
//!     gdisk.add_partition("test1", 1024 * 12, gpt::partition_types::BASIC, 0, None)
//!         .expect("failed to add test1 partition");
//!     gdisk.add_partition("test2", 1024 * 18, gpt::partition_types::LINUX_FS, 0, None)
//!         .expect("failed to add test2 partition");
//!
//!     // Write the partition table and take ownership of
//!     // the underlying memory buffer-backed block device
//!     let mut mem_device = gdisk.write().expect("failed to write partition table");
//!
//!     // Read the written bytes out of the memory buffer device
//!     mem_device.seek(std::io::SeekFrom::Start(0)).expect("failed to seek");
//!     let mut final_bytes = vec![0u8; TOTAL_BYTES];
//!     mem_device.read_exact(&mut final_bytes)
//!         .expect("failed to read contents of memory device");
//! }
//!
//! create_partition_in_ram();
//! ```

#![deny(missing_docs)]

use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::{fmt, fs, io, path};

#[macro_use]
mod macros;
#[macro_use]
mod logging;
pub mod disk;
pub mod header;
pub mod mbr;
pub mod partition;
pub mod partition_types;

use header::HeaderError;
use macros::ResultInsert;

/// A generic device that we can read/write partitions from/to.
pub trait DiskDevice: Read + Write + Seek + std::fmt::Debug {}

/// Implement the DiskDevice trait for anything that meets the
/// requirements, e.g., `std::fs::File`
impl<T> DiskDevice for T where T: Read + Write + Seek + std::fmt::Debug {}

/// A dynamic trait object that is used by GptDisk for reading/writing/seeking.
pub type DiskDeviceObject<'a> = Box<dyn DiskDevice + 'a>;

#[non_exhaustive]
#[derive(Debug)]
/// Errors returned when interacting with a Gpt Disk.
pub enum GptError {
    /// Generic IO Error
    Io(io::Error),
    /// Error returned from writing or reading the header
    Header(HeaderError),
    /// we were expecting to read an existing partition table, but instead we're
    /// attempting to create a new blank table
    CreatingInitializedDisk,
    /// Somthing Overflowed or Underflowed
    /// This will never occur when dealing with sane values
    Overflow(&'static str),
    /// Unable to find enough space on drive
    NotEnoughSpace,
    /// disk not opened in writable mode
    ReadOnly,
    /// If you try to create more partition than the header supports
    OverflowPartitionCount,
    /// The partition count changes but you did not allow that
    PartitionCountWouldChange,
    /// The id is already been used
    PartitionIdAlreadyUsed,
}

impl From<io::Error> for GptError {
    fn from(e: io::Error) -> Self {
        Self::Io(e)
    }
}

impl From<HeaderError> for GptError {
    fn from(e: HeaderError) -> Self {
        Self::Header(e)
    }
}

impl std::error::Error for GptError {}

impl fmt::Display for GptError {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        use GptError::*;
        let desc = match self {
            Io(e) => return write!(fmt, "GPT IO Error: {e}"),
            Header(e) => return write!(fmt, "GPT Header Error: {e}"),
            CreatingInitializedDisk => {
                "we were expecting to read an existing \
                partition table, but instead we're attempting to create a \
                new blank table"
            }
            Overflow(m) => return write!(fmt, "GTP error Overflow: {m}"),
            NotEnoughSpace => "Unable to find enough space on drive",
            ReadOnly => "disk not opened in writable mode",
            OverflowPartitionCount => "not enough partition slots",
            PartitionCountWouldChange => {
                "partition would change but is not \
            allowed"
            }
            PartitionIdAlreadyUsed => "partition id already used",
        };
        write!(fmt, "{desc}")
    }
}

/// Configuration options to open a GPT disk.
///
/// ## Default
/// By Default the disk is readonly and only one header needs to be valid.
/// If the disk is writable by default the primary and backup partitions are
/// written to, but changing the partition count will fail.
///
/// ```
/// # use gpt::GptConfig;
/// let _default_config = GptConfig::new()
///     .writable(false)
///     .logical_block_size(gpt::disk::DEFAULT_SECTOR_SIZE)
///     .only_valid_headers(false)
///     .readonly_backup(false)
///     .change_partition_count(false);
/// ```
//
// write_backup, allow_first_usable_last_usable, change
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct GptConfig {
    /// Logical block size.
    lb_size: disk::LogicalBlockSize,
    /// Whether to open a GPT partition table in writable mode.
    writable: bool,
    /// Force both the primary and backup header to be valid
    only_valid_headers: bool,
    /// Treat the backup header as readonly
    readonly_backup: bool,
    /// allows to change the partition count
    ///
    /// ## Warning
    /// This might change the first usable and last usable part
    change_partition_count: bool,
}

impl GptConfig {
    /// Create a new default configuration.
    pub fn new() -> Self {
        GptConfig::default()
    }

    /// Whether to open a GPT partition table in writable mode.
    pub fn writable(mut self, writable: bool) -> Self {
        self.writable = writable;
        self
    }

    /// Size of logical blocks (sectors) for this disk.
    pub fn logical_block_size(mut self, lb_size: disk::LogicalBlockSize) -> Self {
        self.lb_size = lb_size;
        self
    }

    /// Sets wether both header need to be valid to open a device.
    pub fn only_valid_headers(mut self, only_valid_headers: bool) -> Self {
        self.only_valid_headers = only_valid_headers;
        self
    }

    /// Sets wether the backup header should be treated as readonly.
    pub fn readonly_backup(mut self, readonly_backup: bool) -> Self {
        self.readonly_backup = readonly_backup;
        self
    }

    /// Sets wether the partition count of the current header can be change.
    ///
    /// ## Warning
    /// This might change the first usable and last usable lba.
    pub fn change_partition_count(mut self, change_partition_count: bool) -> Self {
        self.change_partition_count = change_partition_count;
        self
    }

    /// Open the GPT disk at the given path and inspect it according
    /// to configuration options.
    pub fn open(self, diskpath: impl AsRef<path::Path>) -> Result<GptDisk<fs::File>, GptError> {
        let file = fs::OpenOptions::new()
            .write(self.writable)
            .read(true)
            .open(diskpath)?;
        let mut gpt = self.open_from_device(file)?;
        gpt.sync_all = Some(file_sync_all);

        Ok(gpt)
    }

    /// Creates the GPT disk at the given path.
    ///
    /// ## Note
    /// This does not touch the fs until `GptDisk::write` get's called.
    pub fn create(self, diskpath: impl AsRef<path::Path>) -> Result<GptDisk<fs::File>, GptError> {
        let file = fs::OpenOptions::new()
            .write(self.writable)
            .read(true)
            .open(diskpath)?;
        let mut gpt = self.create_from_device(file, None)?;
        gpt.sync_all = Some(file_sync_all);

        Ok(gpt)
    }

    /// Open the GPT disk from the given DiskDeviceObject and
    /// inspect it according to configuration options.
    pub fn open_from_device<D>(self, mut device: D) -> Result<GptDisk<D>, GptError>
    where
        D: DiskDevice,
    {
        // Proper GPT disk, fully inspect its layout.
        let h1 = header::read_primary_header(&mut device, self.lb_size);
        let h2 = header::read_backup_header(&mut device, self.lb_size);

        let (h1, h2) = if self.only_valid_headers {
            (Ok(h1?), Ok(h2?))
        } else if h1.is_err() && h2.is_err() {
            return Err(h1.unwrap_err().into());
        } else {
            (h1, h2)
        };

        let header = h1.as_ref().or(h2.as_ref()).unwrap();
        let table = partition::file_read_partitions(&mut device, header, self.lb_size)?;

        let disk = GptDisk {
            config: self,
            device,
            guid: header.disk_guid,
            primary_header: h1,
            backup_header: h2,
            partitions: table,
            sync_all: None,
        };
        debug!("disk: {:?}", disk);
        Ok(disk)
    }

    /// Create a GPTDisk with default headers and an empty partition table.
    /// If guid is None then it will generate a new random guid.
    pub fn create_from_device<D>(
        self,
        device: D,
        guid: Option<uuid::Uuid>,
    ) -> Result<GptDisk<D>, GptError>
    where
        D: DiskDevice,
    {
        let mut disk = GptDisk {
            config: self,
            device,
            guid: guid.unwrap_or_else(uuid::Uuid::new_v4),
            primary_header: Err(HeaderError::InvalidGptSignature),
            backup_header: Err(HeaderError::InvalidGptSignature),
            partitions: BTreeMap::new(),
            sync_all: None,
        };
        // setup default headers
        disk.init_headers()?;
        Ok(disk)
    }
}

impl Default for GptConfig {
    fn default() -> Self {
        Self {
            lb_size: disk::DEFAULT_SECTOR_SIZE,
            writable: false,
            only_valid_headers: false,
            readonly_backup: false,
            change_partition_count: false,
        }
    }
}

/// A GPT disk backed by an arbitrary device.
pub struct GptDisk<D> {
    /// if you set config initialized this means there exists a primary_header
    config: GptConfig,
    device: D,
    guid: uuid::Uuid,
    primary_header: Result<header::Header, HeaderError>,
    backup_header: Result<header::Header, HeaderError>,
    /// partition: 0 does never exist
    partitions: BTreeMap<u32, partition::Partition>,
    // we need this because to really make sure all content is written
    // a call to sync_all is required (but this is only possible with a fs::File)
    sync_all: Option<fn(&mut D) -> io::Result<()>>,
}

impl<D> fmt::Debug for GptDisk<D>
where
    D: fmt::Debug,
{
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct("GptDisk")
            .field("config", &self.config)
            .field("device", &self.device)
            .field("guid", &self.guid)
            .field("primary_header", &self.primary_header)
            .field("backup_header", &self.backup_header)
            .field("partitions", &self.partitions)
            .finish()
    }
}

impl<D: Clone> Clone for GptDisk<D> {
    fn clone(&self) -> Self {
        Self {
            config: self.config.clone(),
            device: self.device.clone(),
            guid: self.guid,
            primary_header: self
                .primary_header
                .as_ref()
                .map_err(|e| e.lossy_clone())
                .cloned(),
            backup_header: self
                .backup_header
                .as_ref()
                .map_err(|e| e.lossy_clone())
                .cloned(),
            partitions: self.partitions.clone(),
            sync_all: self.sync_all,
        }
    }
}

impl<D> GptDisk<D> {
    /// Retrieve primary header, if any.
    pub fn primary_header(&self) -> Result<&header::Header, HeaderError> {
        self.primary_header.as_ref().map_err(|e| e.lossy_clone())
    }

    /// Retrieve backup header, if any.
    pub fn backup_header(&self) -> Result<&header::Header, HeaderError> {
        self.backup_header.as_ref().map_err(|e| e.lossy_clone())
    }

    /// Retrieve the current valid header.
    ///
    /// This can only fail while we're building the disk
    fn try_header(&self) -> Result<&header::Header, HeaderError> {
        self.primary_header
            .as_ref()
            .or(self.backup_header.as_ref())
            .map_err(|e| e.lossy_clone())
    }

    /// Retrieve the current valid header.
    pub fn header(&self) -> &header::Header {
        self.try_header().expect("no primary and no backup header")
    }

    /// Retrieve partition entries.
    pub fn partitions(&self) -> &BTreeMap<u32, partition::Partition> {
        &self.partitions
    }

    /// Retrieve disk UUID.
    pub fn guid(&self) -> &uuid::Uuid {
        &self.guid
    }

    /// Retrieve disk logical block size.
    pub fn logical_block_size(&self) -> &disk::LogicalBlockSize {
        &self.config.lb_size
    }

    /// Change the disk device that we are reading/writing from/to.
    /// Returns the previous disk device.
    pub fn update_disk_device(&mut self, device: D, writable: bool) -> D {
        self.config.writable = writable;
        std::mem::replace(&mut self.device, device)
    }

    /// Updates the disk device that the GptDisk instance is interacting with.
    /// Returns a new GptDisk instance, retaining the previous configuration and GUID,
    /// but with the specified device and writable status.
    pub fn with_disk_device<N>(&self, device: N, writable: bool) -> GptDisk<N> {
        let mut n = GptDisk {
            config: self.config.clone(),
            device,
            guid: self.guid,
            primary_header: self
                .primary_header
                .as_ref()
                .map_err(|e| e.lossy_clone())
                .cloned(),
            backup_header: self
                .backup_header
                .as_ref()
                .map_err(|e| e.lossy_clone())
                .cloned(),
            partitions: self.partitions.clone(),
            sync_all: None,
        };
        n.config.writable = writable;

        n
    }

    /// Get a reference to to the underlying device.
    pub fn device_ref(&self) -> &D {
        &self.device
    }

    /// Get a mutable reference to to the underlying device.
    pub fn device_mut(&mut self) -> &mut D {
        &mut self.device
    }

    /// Take the underlying device object and force
    /// self to drop out of scope.
    ///
    /// Caution: this will abandon any changes that where not written.
    pub fn take_device(self) -> D {
        self.device
    }
}

impl<D> GptDisk<D>
where
    D: DiskDevice,
{
    /// Add another partition to this disk.  This tries to find
    /// the optimum partition location with the lowest block device.
    /// Returns the new partition id if there was sufficient room
    /// to add the partition. Size is specified in bytes.
    ///
    /// ## Panics
    /// If size is empty panics
    pub fn add_partition(
        &mut self,
        name: &str,
        size: u64,
        part_type: partition_types::Type,
        flags: u64,
        part_alignment: Option<u64>,
    ) -> Result<u32, GptError> {
        assert!(size > 0, "size must be greater than zero");

        // Ceiling division which avoids overflow
        let size_lba = (size - 1)
            .checked_div(self.config.lb_size.into())
            .ok_or(GptError::Overflow(
                "invalid logical block size caused bad \
                division when calculating size in blocks",
            ))?
            // we will never divide by 1 so we always have room for one more
            + 1;

        // Find the lowest lba that is larger than size.
        let free_sections = self.find_free_sectors();
        for (starting_lba, length) in free_sections {
            // Get the distance between the starting LBA of this section and the next aligned LBA
            // We don't need to do any checked math here because we guarantee that with `(A % B)`,
            // `A` will always be between 0 and `B-1`.
            let alignment_offset_lba = match part_alignment {
                Some(alignment) => (alignment - (starting_lba % alignment)) % alignment,
                None => 0_u64,
            };

            debug!(
                "starting_lba {}, length {}, alignment_offset_lba {}",
                starting_lba, length, alignment_offset_lba
            );

            if length >= (alignment_offset_lba + size_lba) {
                let starting_lba = starting_lba + alignment_offset_lba;
                // Found our free slice.
                let partition_id = self
                    .find_next_partition_id()
                    .unwrap_or_else(|| self.header().num_parts + 1);
                debug!(
                    "Adding partition id: {} {:?}.  first_lba: {} last_lba: {}",
                    partition_id,
                    part_type,
                    starting_lba,
                    starting_lba + size_lba - 1_u64
                );

                // let's try to increase the num parts
                // because partition_id 0 will never exist the num_parts is without + 1
                let num_parts_changes = self.header().num_parts_would_change(partition_id);
                if num_parts_changes && !self.config.change_partition_count {
                    return Err(GptError::PartitionCountWouldChange);
                }

                let part = partition::Partition {
                    part_type_guid: part_type,
                    part_guid: uuid::Uuid::new_v4(),
                    first_lba: starting_lba,
                    last_lba: starting_lba + size_lba - 1_u64,
                    flags,
                    name: name.to_string(),
                };
                if let Some(p) = self.partitions.insert(partition_id, part.clone()) {
                    debug!("Replacing\n{}\nwith\n{}", p, part);
                }
                if num_parts_changes {
                    // update headers
                    self.init_headers()?;
                }
                return Ok(partition_id);
            }
        }

        Err(GptError::NotEnoughSpace)
    }

    /// create a new partition with a specific id
    /// a specific name
    /// a specific first_lba
    /// a specific length_lba
    /// a specific part_type
    /// a specific flags
    ///
    /// ## Panics
    /// If length is empty panics
    /// If id zero panics
    pub fn add_partition_at(
        &mut self,
        name: &str,
        id: u32,
        first_lba: u64,
        length_lba: u64,
        part_type: partition_types::Type,
        flags: u64,
    ) -> Result<u32, GptError> {
        assert!(length_lba > 0, "length must be greater than zero");
        assert!(id > 0, "id must be greater than zero");

        //check id
        match self.partitions.get(&id) {
            Some(p) if p.is_used() => return Err(GptError::PartitionIdAlreadyUsed),
            // Allow unused ids , because we can allow to modify the part count
            _ => {
                // will override unused partition
            }
        }

        for (starting_lba, length) in self.find_free_sectors() {
            if !(first_lba >= starting_lba && length_lba <= length) {
                continue;
            }

            // part segment is legal
            debug!(
                "starting_lba {}, length {}, id {}",
                first_lba, length_lba, id
            );
            debug!(
                "Adding partition id: {} {:?}.  first_lba: {} last_lba: {}",
                id,
                part_type,
                first_lba,
                first_lba + length_lba - 1_u64
            );

            // let's try to increase the num parts
            // because partition_id 0 will never exist the num_parts is without + 1
            let num_parts_changes = self.header().num_parts_would_change(id);

            if num_parts_changes && !self.config.change_partition_count {
                return Err(GptError::PartitionCountWouldChange);
            }

            let part = partition::Partition {
                part_type_guid: part_type,
                part_guid: uuid::Uuid::new_v4(),
                first_lba,
                last_lba: first_lba + length_lba - 1_u64,
                flags,
                name: name.to_string(),
            };

            if let Some(p) = self.partitions.insert(id, part.clone()) {
                debug!("Replacing\n{}\nwith\n{}", p, part);
            }

            if num_parts_changes {
                // update headers
                self.init_headers()?;
            }

            return Ok(id);
        }

        //given segment is illegal
        Err(GptError::NotEnoughSpace)
    }

    /// calculate sector alignment based on the current partitions
    /// in order to promise uniform alignment
    /// return 0 if no partitions existed
    /// return 1 if partitions are not well aligned ( can be seen as 1 alignment )
    /// MAX ALIGNMENT IS 2048 ! And the result is ok ONLY if existed partitions is well aligned
    pub fn calculate_alignment(&self) -> u64 {
        if self.partitions.is_empty() {
            return 0;
        }

        const MAX_ALIGN: u64 = 2048;
        let mut align = MAX_ALIGN;
        let mut exponent = (align as f64).log2() as u32;

        for partition in self.partitions.values().filter(|p| p.is_used()) {
            loop {
                align = u64::pow(2, exponent);
                if (partition.first_lba % align) == 0 {
                    break;
                }

                exponent -= 1;
            }
        }

        align
    }

    /// Remove partition from this disk.
    pub fn remove_partition(&mut self, id: u32) -> Option<u32> {
        self.partitions.remove(&id).map(|_| {
            debug!("Removing partition number {id}");
            id
        })
    }

    /// Remove partition from this disk. This tries to find the first partition based on its partition guid.
    pub fn remove_partition_by_guid(&mut self, guid: uuid::Uuid) -> Option<u32> {
        let id = self
            .partitions
            .iter()
            .find(|(_, v)| v.part_guid == guid)
            .map(|(k, _)| *k)?;

        debug!("Removing partition number {id}");
        self.partitions.remove(&id);

        Some(id)
    }

    /// Find free space on the disk.
    /// Returns a tuple of (starting_lba, length in lba's).
    pub fn find_free_sectors(&self) -> Vec<(u64, u64)> {
        let header = self.header();

        trace!("first_usable: {}", header.first_usable);
        // a list of boundaries between partitions each point is a used lba
        let mut disk_positions = vec![header.first_usable - 1];
        for part in self.partitions().iter().filter(|p| p.1.is_used()) {
            trace!(
                "used partition: ({}, {})",
                part.1.first_lba,
                part.1.last_lba
            );
            disk_positions.push(part.1.first_lba);
            disk_positions.push(part.1.last_lba);
        }
        disk_positions.push(header.last_usable + 1);
        trace!("last_usable: {}", header.last_usable);
        disk_positions.sort_unstable();

        disk_positions
            // Walk through the LBA's in chunks of 2 (ending, starting).
            .chunks_exact(2)
            // Add 1 to the ending and then subtract the starting if NOT the first usable sector
            .filter_map(|p| {
                // free lba
                let start = p[0] + 1;
                let end = p[1] - 1;

                #[allow(clippy::unnecessary_lazy_evaluations)]
                (start <= end).then(|| (start, end - start + 1))
            })
            .collect()
    }

    /// Find next highest partition id.
    /// Will always return > 0
    ///
    /// If this returns None there is not more space to add a partiton
    pub fn find_next_partition_id(&self) -> Option<u32> {
        if self.partitions.is_empty() {
            // Partitions start at 1.
            return Some(1);
        }

        // get the first free partition slot
        for i in 1..=self.header().num_parts {
            // todo should unused ones be included?
            match self.partitions.get(&i) {
                Some(p) if !p.is_used() => return Some(i),
                None => return Some(i),
                _ => {}
            }
        }

        None
    }

    /// Retrieve partition entries, replacing it with an empty partitions list.
    pub fn take_partitions(&mut self) -> BTreeMap<u32, partition::Partition> {
        std::mem::take(&mut self.partitions)
    }

    /// Update disk UUID.
    ///
    /// If no UUID is specified, a new random one is generated.
    /// No changes are recorded to disk until `write()` is called.
    pub fn update_guid(&mut self, uuid: Option<uuid::Uuid>) {
        let guid = match uuid {
            Some(u) => u,
            None => {
                let u = uuid::Uuid::new_v4();
                debug!("Generated random uuid: {}", u);
                u
            }
        };
        self.guid = guid;
    }

    /// Update current partition table.
    ///
    /// No changes are recorded to disk until `write()` is called.
    ///
    /// ## Note
    /// you need to make sure that all values in the partition are set correctly
    ///
    /// ## Panics
    /// If a partition 0 exists
    pub fn update_partitions(
        &mut self,
        pp: BTreeMap<u32, partition::Partition>,
    ) -> Result<(), GptError> {
        assert!(!pp.contains_key(&0));

        // TODO(lucab): validate partitions.
        let num_parts = pp.len() as u32;

        let num_parts_changes = self.header().num_parts_would_change(num_parts);
        if num_parts_changes && !self.config.change_partition_count {
            return Err(GptError::PartitionCountWouldChange);
        }

        self.partitions = pp;

        self.init_headers()
    }

    /// Makes sure there exists a primary header and if allowed also creates the backup
    /// header.
    pub(crate) fn init_headers(&mut self) -> Result<(), GptError> {
        let bak = header::find_backup_lba(&mut self.device, self.config.lb_size)?;
        let num_parts = self.partitions.len() as u32;

        let h1 = header::HeaderBuilder::from_maybe_header(self.try_header())
            .num_parts(num_parts)
            .backup_lba(bak)
            .disk_guid(self.guid)
            .primary(true)
            .build(self.config.lb_size)?;
        let header = self.primary_header.insert_ok(h1);

        if !self.config.readonly_backup {
            let h2 = header::HeaderBuilder::from_header(header)
                .primary(false)
                .build(self.config.lb_size)?;
            self.backup_header = Ok(h2);
        }

        Ok(())
    }

    /// Persist state to disk, consuming this disk object.
    ///
    /// This is a destructive action, as it overwrite headers and
    /// partitions entries on disk. All writes are flushed to disk
    /// before returning the underlying DiskDeviceObject.
    ///
    /// ## Note
    /// If you provided you're own DiskDevice you need to make sure
    /// that the device is flushed to disk for example via `sync_all`.
    pub fn write(mut self) -> Result<D, GptError> {
        self.write_inplace()?;

        Ok(self.device)
    }

    /// Persist state to disk, leaving this disk object intact.
    ///
    /// This is a destructive action, as it overwrites headers
    /// and partitions entries on disk. All writes are flushed
    /// to disk before returning.
    ///
    /// ## Note
    /// If you provided you're own DiskDevice you need to make sure
    /// that the device is flushed to disk for example via `sync_all`.
    //
    // Primary header and backup header don't need to match.
    // so both need to be checked
    pub fn write_inplace(&mut self) -> Result<(), GptError> {
        if !self.config.writable {
            return Err(GptError::ReadOnly);
        }

        debug!("Computing new headers");
        trace!("old primary header: {:?}", self.primary_header);
        trace!("old backup header: {:?}", self.backup_header);
        let bak = header::find_backup_lba(&mut self.device, self.config.lb_size)?;
        trace!("old backup lba: {}", bak);

        let primary_header = header::HeaderBuilder::from_header(self.header())
            .primary(true)
            .build(self.config.lb_size)?;
        let primary_header = self.primary_header.insert_ok(primary_header);

        let backup_header = if !self.config.readonly_backup {
            let header = header::HeaderBuilder::from_header(primary_header)
                .primary(false)
                .build(self.config.lb_size)?;

            Some(self.backup_header.insert_ok(header))
        } else {
            None
        };

        // Write all of the used partitions at the start of the partition array.
        let mut next_partition_index = 0;
        for (part_idx, partition) in self
            .partitions
            .clone()
            .iter()
            .filter(|p| p.1.is_used())
            .enumerate()
        {
            // don't allow us to overflow partition array...
            // todo this should not be possible since we
            // check in add partition that it is valid
            if part_idx >= primary_header.num_parts as usize {
                return Err(GptError::OverflowPartitionCount);
            }

            // Write to primary partition array
            partition.1.write_to_device(
                &mut self.device,
                part_idx as u64,
                primary_header.part_start,
                self.config.lb_size,
                primary_header.part_size,
            )?;
            // IMPORTANT: must also write it to the backup header if it uses a different
            // area to store the partition array; otherwise backup header will not point
            // to an up to date partition array on disk.
            if let Some(backup_header) = &backup_header {
                if part_idx >= backup_header.num_parts as usize {
                    return Err(GptError::OverflowPartitionCount);
                }

                if primary_header.part_start != backup_header.part_start {
                    partition.1.write_to_device(
                        &mut self.device,
                        part_idx as u64,
                        backup_header.part_start,
                        self.config.lb_size,
                        backup_header.part_size,
                    )?;
                }
            }
            next_partition_index = part_idx + 1;
        }

        // Next, write zeros to the rest of the primary/backup partition array
        // (ensures any newly deleted partitions are truly removed from disk, etc.)
        // NOTE: we should never underflow here because of boundary checking in loop above.
        partition::Partition::write_zero_entries_to_device(
            &mut self.device,
            next_partition_index as u64,
            (primary_header.num_parts as u64)
                .checked_sub(next_partition_index as u64)
                .unwrap(),
            primary_header.part_start,
            self.config.lb_size,
            primary_header.part_size,
        )?;
        if let Some(backup_header) = &backup_header {
            partition::Partition::write_zero_entries_to_device(
                &mut self.device,
                next_partition_index as u64,
                (backup_header.num_parts as u64)
                    .checked_sub(next_partition_index as u64)
                    .unwrap(),
                backup_header.part_start,
                self.config.lb_size,
                backup_header.part_size,
            )?;
        }

        if let Some(backup_header) = backup_header {
            debug!("Writing backup header");
            backup_header.write_backup(&mut self.device, self.config.lb_size)?;
        }
        debug!("Writing primary header");
        primary_header.write_primary(&mut self.device, self.config.lb_size)?;

        self.device.flush()?;

        if let Some(sync_all) = self.sync_all {
            sync_all(&mut self.device)?;
        }

        Ok(())
    }
}

fn file_sync_all(device: &mut fs::File) -> io::Result<()> {
    device.sync_all()
}