Add support for logical partitions.
- add max_logi_align for calculate_surplus
If we resize logical partition, we will lose size becasue of alignment.
max_logi_align shows the max loss for alignment,
which can ensure a successfully resize
- don't count size of extended partition
For it'll duplicate with logical partition, we'll count it later
- we need at leat 1 gap between logical partitions.
so --aligment=1 will be increased by 1
Signed-off-by: Chen Hanxiao <chenhanxiao(a)cn.fujitsu.com>
---
v3: introduce max_logi_align for calculate_surplus
resize/resize.ml | 27 +++++++++++++++++++++++----
1 file changed, 23 insertions(+), 4 deletions(-)
diff --git a/resize/resize.ml b/resize/resize.ml
index 7aa1c96..6974869 100644
--- a/resize/resize.ml
+++ b/resize/resize.ml
@@ -736,10 +736,12 @@ read the man page virt-resize(1).
* the size of the target disk. If the return value >= 0 then it's
* a surplus, if it is < 0 then it's a deficit.
*)
- let calculate_surplus () =
+ let calculate_surplus max_logi_align =
(* We need some overhead for partitioning. *)
let overhead =
let maxl64 = List.fold_left max 0L in
+ (* We need at least 1 sector gap between logical partitions *)
+ let alignment = if alignment = 1L then 2L else alignment in
let nr_partitions = List.length partitions in
@@ -769,12 +771,29 @@ read the man page virt-resize(1).
let required = List.fold_left (
fun total p ->
let newsize =
+ (* size of extended partition is calculated seperately *)
+ if p.p_type = ContentExtendedPartition then 0L else
+ match p.p_operation with
+ | OpCopy | OpIgnore -> p.p_part.G.part_size
+ | OpDelete -> 0L
+ | OpResize newsize -> newsize in
+ total +^ newsize
+ ) 0L partitions in
+
+ let required_logical = List.fold_left (
+ fun total p ->
+ let newsize =
match p.p_operation with
| OpCopy | OpIgnore -> p.p_part.G.part_size
| OpDelete -> 0L
| OpResize newsize -> newsize in
total +^ newsize
- ) 0L partitions in
+ ) 0L logical_partitions in
+
+ let required_logical =
+ (* an extra alignment for the gap between extended and 1st logical *)
+ required_logical +^ (max_logi_align +^ alignment) *^sectsize in
+ let required = required +^ required_logical in
let surplus = outsize -^ (required +^ overhead) in
@@ -790,7 +809,7 @@ read the man page virt-resize(1).
error (f_"you cannot use options --expand and --shrink together");
if expand <> None || shrink <> None then (
- let surplus = calculate_surplus () in
+ let surplus = calculate_surplus 0L in
if verbose () then
printf "surplus before --expand or --shrink: %Ld\n" surplus;
@@ -841,7 +860,7 @@ read the man page virt-resize(1).
* At this point, this number must be >= 0.
*)
let surplus =
- let surplus = calculate_surplus () in
+ let surplus = calculate_surplus logical_align in
if surplus < 0L then (
let deficit = Int64.neg surplus in
--
2.1.0