1 (* 'df' command for virtual domains.
3 (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 Support for Master Boot Record partition scheme.
27 open Virt_df_gettext.Gettext
31 let sector_size64 = 512L
33 (* Maximum number of extended partitions possible. *)
34 let max_extended_partitions = 100
36 (* Device representing a single partition. It just acts as an offset
37 * into the underlying device.
40 * (1) 'start'/'size' are measured in sectors.
41 * (2) 'partno' is the partition number, starting at 1
42 * (cf. /dev/hda1 is the first partition).
43 * (3) 'dev' is the underlying block device.
45 class partition_device partno start size dev =
46 let devname = dev#name in
47 let name = sprintf "%s%d" devname partno in
48 let start = start *^ sector_size64 in
49 let size = size *^ sector_size64 in
51 inherit offset_device name start size dev
55 {{:http://en.wikipedia.org/wiki/Master_boot_record}master boot record}
56 (if it is one) and read the partitions.
58 @raise Not_found if it is not an MBR.
60 let rec probe_mbr dev =
61 (* Adjust size to sectors. *)
62 let size = dev#size /^ sector_size64 in
64 (* Read the first sector. *)
66 try dev#read_bitstring 0L sector_size
67 with exn -> raise Not_found in
69 (* Does this match a likely-looking MBR? *)
71 | padding : 3568 : bitstring; (* padding to byte offset 446 *)
72 part0 : 128 : bitstring; (* partitions *)
73 part1 : 128 : bitstring;
74 part2 : 128 : bitstring;
75 part3 : 128 : bitstring;
76 0x55 : 8; 0xAA : 8 -> (* MBR signature *)
78 (* Parse the partition table entries. *)
80 List.mapi (parse_mbr_entry dev) [part0;part1;part2;part3] in
83 (* Read extended partition data. *)
84 let extendeds = List.map (
86 | { part_type = 0x05 } as part ->
87 probe_extended_partition
88 max_extended_partitions fd part part.part_lba_start
91 let extendeds = List.concat extendeds in
94 { parts_name = "MBR"; parts = primaries }
97 raise Not_found (* not an MBR *)
99 (* Parse a single partition table entry. See the table here:
100 * http://en.wikipedia.org/wiki/Master_boot_record
102 and parse_mbr_entry dev i bits =
104 | 0l : 32; 0l : 32; 0l : 32; 0l : 32 ->
105 { part_status = NullEntry; part_type = 0;
106 part_dev = null_device; part_content = `Unknown }
108 | 0 : 8; first_chs : 24;
109 part_type : 8; last_chs : 24;
110 first_lba : 32 : unsigned, littleendian;
111 part_size : 32 : unsigned, littleendian ->
112 make_mbr_entry Nonbootable dev (i+1) part_type first_lba part_size
114 | 0x80 : 8; first_chs : 24;
115 part_type : 8; last_chs : 24;
116 first_lba : 32 : unsigned, littleendian;
117 part_size : 32 : unsigned, littleendian ->
118 make_mbr_entry Bootable dev (i+1) part_type first_lba part_size
121 { part_status = Malformed; part_type = 0;
122 part_dev = null_device; part_content = `Unknown }
124 and make_mbr_entry part_status dev partno part_type first_lba part_size =
125 let first_lba = uint64_of_int32 first_lba in
126 let part_size = uint64_of_int32 part_size in
128 eprintf "make_mbr_entry: first_lba = %Lx part_size = %Lx\n%!"
130 { part_status = part_status;
131 part_type = part_type;
132 part_dev = new partition_device partno first_lba part_size dev;
133 part_content = `Unknown }
136 This code worked previously, but now needs some love ...
139 (* Probe an extended partition. *)
140 and probe_extended_partition max fd epart sect =
142 (* Offset of the first EBR. *)
143 let ebr_offs = sect *^ sector_size in
145 LargeFile.lseek fd (ebr_offs +^ 510L) SEEK_SET;
146 let str = String.create 2 in
147 if read fd str 0 2 <> 2 || str.[0] != '\x55' || str.[1] != '\xAA' then
150 (* Read the extended partition table entries (just 2 of them). *)
151 LargeFile.lseek fd (ebr_offs +^ 446L) SEEK_SET;
152 let str = String.create 32 in
153 if read fd str 0 32 <> 32 then
154 failwith (s_ "error reading extended partition")
156 (* Extract partitions from the data. *)
158 match List.map (get_partition str) [ 0; 16 ] with
160 | _ -> failwith (s_ "probe_extended_partition: internal error") in
161 (* First partition entry has offset to the start of this partition. *)
162 let part1 = { part1 with
163 part_lba_start = sect +^ part1.part_lba_start } in
164 (* Second partition entry is zeroes if end of list, otherwise points
165 * to the next partition.
167 if part2.part_status = NullEntry then
170 part1 :: probe_extended_partition
171 (max-1) fd epart (sect +^ part2.part_lba_start)
178 (* Ugh, fake a UInt32 -> UInt64 conversion without sign extension, until
179 * we get working UInt32/UInt64 modules in extlib.
181 and uint64_of_int32 u32 =
182 let i64 = Int64.of_int32 u32 in
183 if u32 >= 0l then i64
184 else Int64.add i64 0x1_0000_0000_L
186 (* Register with main code. *)
187 let () = partition_type_register "MBR" probe_mbr