[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH rebase v4 02/11] resize: add logical_partitions and extended_partition



For MBR, logical partitions laid inside extended partition.
Add 3 variables to describe this:
 - partitions
  flat list of primary partitions (as now, the global 'partitions').
  extended partitions is essentially  primary partition

 - logical_partitions
  flat list of logical partitions

 - extended_partition
  one MBR extended partition


Signed-off-by: Chen Hanxiao <chenhanxiao cn fujitsu com>
---
v3:
 rewrite partitions/logical_partitions/extended_partition
  according to Rich and Pino's commnets

 resize/resize.ml | 44 +++++++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 15 deletions(-)

diff --git a/resize/resize.ml b/resize/resize.ml
index 65464eb..f4cc588 100644
--- a/resize/resize.ml
+++ b/resize/resize.ml
@@ -32,13 +32,7 @@ type align_first_t = [ `Never | `Always | `Auto ]
 (* Source partition type. *)
 type parttype = MBR | GPT
 
-(* Data structure describing the source disk's partition layout.
- *
- * NOTE: For MBR, only primary/extended partitions are tracked here.
- * Logical partitions are contained within an extended partition, and
- * we don't track them (they are just copied within the extended
- * partition).  For the same reason we cannot resize logical partitions.
- *)
+(* Data structure describing the source disk's partition layout. *)
 type partition = {
   p_name : string;               (* Device name, like /dev/sda1. *)
   p_part : G.partition;          (* SOURCE partition data from libguestfs. *)
@@ -496,12 +490,6 @@ read the man page virt-resize(1).
             p_target_start = 0L; p_target_end = 0L }
       ) parts in
 
-    (* Filter out logical partitions.  See note above. *)
-    let partitions =
-      (* for GPT, all partitions are regarded as Primary Partition,
-       * e.g. there is no Extended Partition or Logical Partition. *)
-      List.filter (fun p -> parttype <> MBR || p.p_mbr_p_type <> LogicalPartition) partitions in
-
     (* Check content isn't larger than partitions.  If it is then
      * something has gone wrong and we shouldn't continue.  Old
      * virt-resize didn't do these checks.
@@ -533,11 +521,35 @@ read the man page virt-resize(1).
       error (f_"%s: this partition overlaps the previous one") name
     | { p_part = { G.part_end = part_end } } :: parts -> loop part_end parts
   in
+
+  let extended_partition_list =
+    List.filter (fun p -> parttype = MBR && p.p_mbr_p_type = ExtendedPartition) partitions in
+  let extended_partition =
+    match extended_partition_list with
+    | h :: _ -> Some h
+    | [] -> None in
+
+  let is_logical_partition p =
+    parttype = MBR && p.p_mbr_p_type = LogicalPartition
+  in
+
+  let logical_partitions, partitions =
+    List.partition is_logical_partition partitions in
+
+  let nr_logical = List.length logical_partitions in
+
   loop 0L partitions;
+  loop 0L logical_partitions;
 
   if verbose () then (
-    printf "%d partitions found\n" (List.length partitions);
-    List.iter (debug_partition ~sectsize) partitions
+    printf "%d partitions found\n"
+      (List.length partitions + List.length logical_partitions);
+    List.iter (debug_partition ~sectsize) partitions;
+    List.iter (debug_partition ~sectsize) logical_partitions;
+    match extended_partition with
+    | Some v -> printf "%s is extended partition\n"
+                (List.hd extended_partition_list).p_name
+    | None -> ()
     );
 
   (* Build a data structure describing LVs on the source disk. *)
@@ -603,6 +615,8 @@ read the man page virt-resize(1).
     let hash = Hashtbl.create 13 in
     List.iter (fun ({ p_name = name } as p) -> Hashtbl.add hash name p)
       partitions;
+    List.iter (fun ({ p_name = name } as p) -> Hashtbl.add hash name p)
+      logical_partitions;
     fun ~option name ->
       let name =
         if String.length name < 5 || String.sub name 0 5 <> "/dev/" then
-- 
2.1.0


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]