Signed-off-by: Chen Hanxiao <chenhanxiao(a)cn.fujitsu.com>
---
resize/resize.ml | 34 +++++++++++++++++++++++++++++++++-
1 file changed, 33 insertions(+), 1 deletion(-)
diff --git a/resize/resize.ml b/resize/resize.ml
index d5c457c..67321c2 100644
--- a/resize/resize.ml
+++ b/resize/resize.ml
@@ -727,6 +727,7 @@ read the man page virt-resize(1).
(* We need some overhead for partitioning. *)
let overhead =
let maxl64 = List.fold_left max 0L in
+ let alignment = if alignment = 1L then 2L else alignment in
let nr_partitions = List.length partitions in
@@ -753,6 +754,31 @@ read the man page virt-resize(1).
start_overhead_sects +^ alignment_sects +^ gpt_end_sects in
sectsize *^ overhead_sects in
+ let required_logical = List.fold_left (
+ fun total p ->
+ if p.p_mbr_p_type <> LogicalPartition then 0L else
+ let newsize =
+ match p.p_operation with
+ | OpCopy | OpIgnore -> roundup64 p.p_part.G.part_size (alignment *^
sectsize)
+ | OpDelete -> 0L
+ | OpResize newsize -> roundup64 newsize (alignment *^ sectsize) in
+ total +^ newsize
+ ) 0L partitions in
+
+ let required_logical = required_logical +^ alignment *^ sectsize in
+ let required_extended = List.fold_left (
+ fun total p ->
+ let newsize =
+ match p.p_type with
+ | ContentExtendedPartition ->
+ (* resizing extended partition by --resize or --expand is supported *)
+ (match p.p_operation with
+ | OpResize newsize -> newsize
+ | _ -> p.p_part.G.part_size)
+ | _ -> 0L in
+ total +^ newsize
+ ) 0L partitions in
+
let required = List.fold_left (
fun total p ->
let newsize =
@@ -765,13 +791,19 @@ read the man page virt-resize(1).
total +^ newsize
) 0L partitions in
+ let required = required +^
+ if required_extended > required_logical then
+ required_extended else required_logical in
+
let surplus = outsize -^ (required +^ overhead) in
if verbose then
printf "calculate surplus: outsize=%Ld required=%Ld overhead=%Ld
surplus=%Ld\n%!"
outsize required overhead surplus;
- surplus
+ (* XXX: fix of expanding a logical partition. without the sectsize,
+ * we'll get a deficit of 512 bytes. *)
+ surplus +^ sectsize
in
(* Handle --expand and --shrink options. *)
--
2.1.0