- let target_partnum =
- match p.p_operation with
- | OpDelete -> None (* do nothing *)
- | OpIgnore | OpCopy -> (* new partition, same size *)
- (* Size in sectors. *)
- let size = (p.p_size +^ sectsize -^ 1L) /^ sectsize in
- Some (add_partition size)
- | OpResize newsize -> (* new partition, resized *)
- (* Size in sectors. *)
- let size = (newsize +^ sectsize -^ 1L) /^ sectsize in
- Some (add_partition size) in
-
- (match target_partnum with
- | None -> (* OpDelete *)
- ()
- | Some target_partnum -> (* not OpDelete *)
- p.p_target_partnum <- target_partnum;
-
- (* Set bootable and MBR IDs *)
- if p.p_bootable then
- g#part_set_bootable "/dev/sdb" target_partnum true;
-
- (match p.p_mbr_id with
- | None -> ()
- | Some mbr_id ->
- g#part_set_mbr_id "/dev/sdb" target_partnum mbr_id
- );
- );
-
- repartition ps
-
- (* Add a partition, returns the partition number on the target. *)
- and add_partition size (* in SECTORS *) =
- let target_partnum, end_ =
- if !nextpart <= 3 || parttype <> "msdos" then (
- let target_partnum = !nextpart in
- let end_ = !start +^ size -^ 1L in
- g#part_add "/dev/sdb" "primary" !start end_;
- incr nextpart;
- target_partnum, end_
- ) else (
- if !nextpart = 4 then (
- g#part_add "/dev/sdb" "extended" !start (-1L);
- incr nextpart;
- start := !start +^ 64L
- );
- let target_partnum = !nextpart in
- let end_ = !start +^ size -^ 1L in
- g#part_add "/dev/sdb" "logical" !start end_;
- incr nextpart;
- target_partnum, end_
- ) in
-
- (* Start of next partition + alignment to 64 sectors. *)
- start := ((end_ +^ 1L) +^ 63L) &^ (~^ 63L);
+ (match p.p_operation with
+ | OpDelete -> loop partnum start ps (* skip p *)
+
+ | OpIgnore | OpCopy -> (* same size *)
+ (* Size in sectors. *)
+ let size = (p.p_part.G.part_size +^ sectsize -^ 1L) /^ sectsize in
+ (* Start of next partition + alignment. *)
+ let end_ = start +^ size in
+ let next = roundup64 end_ alignment in
+
+ { p with p_target_start = start; p_target_end = end_ -^ 1L;
+ p_target_partnum = partnum } :: loop (partnum+1) next ps
+
+ | OpResize newsize -> (* resized partition *)
+ (* New size in sectors. *)
+ let size = (newsize +^ sectsize -^ 1L) /^ sectsize in
+ (* Start of next partition + alignment. *)
+ let next = start +^ size in
+ let next = roundup64 next alignment in
+
+ { p with p_target_start = start; p_target_end = next -^ 1L;
+ p_target_partnum = partnum } :: loop (partnum+1) next ps
+ )