Chen Hanxiao
2015-Mar-30 09:39 UTC
[Libguestfs] [PATCH RFC] resize: add p_mbr_p_type as member of type partition
Add p_mbr_p_type as member of type partition to describe mbr partition type. Currently we use: List.filter (fun p -> parttype <> MBR || p.G.part_num <= 4_l) to filter out logical partitions. Commit 0c396a4bce578486dfc4a38e1f8c47fd5c2836ea introduce API part_get_mbr_part_type, we could use this to know the part_type. Furthermore, we could also use p_mbr_p_type for resizing logical partitions. Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- resize/resize.ml | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/resize/resize.ml b/resize/resize.ml index 84fd6d4..284d0e3 100644 --- a/resize/resize.ml +++ b/resize/resize.ml @@ -57,6 +57,7 @@ type partition = { p_target_partnum : int; (* TARGET partition number. *) p_target_start : int64; (* TARGET partition start (sector num). *) p_target_end : int64; (* TARGET partition end (sector num). *) + p_mbr_p_type : partition_type (* Partiton Type (master/extended/logical) *) } and partition_content | ContentUnknown (* undetermined *) @@ -73,9 +74,11 @@ and partition_id | No_ID (* No identifier. *) | MBR_ID of int (* MBR ID. *) | GPT_Type of string (* GPT UUID. *) - -type partition_type +and partition_type | PrimaryPartition + | ExtendedPartition + | LogicalPartition + | NoTypePartition let rec debug_partition p printf "%s:\n" p.p_name; @@ -99,7 +102,8 @@ let rec debug_partition p (match p.p_guid with | Some guid -> guid | None -> "(none)" - ) + ); + printf "\tpartition type: %s\n" (string_of_partition_type p.p_mbr_p_type) and string_of_partition_content = function | ContentUnknown -> "unknown data" | ContentPV sz -> sprintf "LVM PV (%Ld bytes)" sz @@ -110,6 +114,11 @@ and string_of_partition_content_no_size = function | ContentPV _ -> "LVM PV" | ContentFS (fs, _) -> sprintf "filesystem %s" fs | ContentExtendedPartition -> "extended partition" +and string_of_partition_type = function + | PrimaryPartition -> "primary" + | ExtendedPartition -> "extended" + | LogicalPartition -> "logical" + | NoTypePartition -> "none" (* Data structure describing LVs on the source disk. This is only * used if the user gave the --lv-expand option. @@ -453,18 +462,9 @@ read the man page virt-resize(1). | MBR_ID _ | GPT_Type _ | No_ID -> false in - let find_partitions part_type + let find_partitions () let parts = Array.to_list (g#part_list "/dev/sda") in - (* Filter out logical partitions. See note above. *) - let parts - match part_type with - (* for GPT, all partitions are regarded as Primary Partition, - * e.g. there is no Extended Partition or Logical Partition. *) - | PrimaryPartition -> - List.filter (fun p -> parttype <> MBR || p.G.part_num <= 4_l) - parts in - let partitions List.map ( fun ({ G.part_num = part_num } as part) -> @@ -491,14 +491,28 @@ read the man page virt-resize(1). | GPT -> try Some (g#part_get_gpt_guid "/dev/sda" part_num) with G.Error _ -> None in + let mbr_part_type + let mbr_part_type_str = g#part_get_mbr_part_type "/dev/sda" part_num in + match mbr_part_type_str with + | "primary" -> PrimaryPartition + | "extended" -> ExtendedPartition + | "logical" -> LogicalPartition + | str -> NoTypePartition + in { p_name = name; p_part = part; p_bootable = bootable; p_id = id; p_type = typ; - p_label = label; p_guid = guid; + p_label = label; p_guid = guid; p_mbr_p_type = mbr_part_type; p_operation = OpCopy; p_target_partnum = 0; 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. @@ -530,7 +544,7 @@ read the man page virt-resize(1). partitions in - let partitions = find_partitions PrimaryPartition in + let partitions = find_partitions () in if verbose then ( printf "%d partitions found\n" (List.length partitions); @@ -1085,7 +1099,8 @@ read the man page virt-resize(1). (* Target information is meaningful. *) p_operation = OpIgnore; p_target_partnum = partnum; - p_target_start = start; p_target_end = ~^ 64L + p_target_start = start; p_target_end = ~^ 64L; + p_mbr_p_type = NoTypePartition } ] ) else -- 2.1.0
Richard W.M. Jones
2015-Mar-30 18:03 UTC
Re: [Libguestfs] [PATCH RFC] resize: add p_mbr_p_type as member of type partition
Yes it seems a straightforward transformation. I have pushed it. Thanks, Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
Maybe Matching Threads
- [PATCH v2 02/11] resize: add logical_partitions and extended_partition
- [PATCH v3 6/7] resize: add partition type LogicalPartition
- [PATCH V5 1/4] resize: add partition type LogicalPartition
- [PATCH v4 1/7] resize: add function find_partitions
- [PATCH] resize: show sector infor in debug_partition