Meson has a global option -Ddefault_library which determines whether to
build all generic libraries as static, shared, or both. When creating a
library of the same name twice, once with library() and once with either
shared_library() or static_library(), some values for -Ddefault_library
will result in a fatal configuration error because multiple rules try to
create libXXX.a or libXXX.so
The reason for creating these additional copies of the library is in
order to optionally build some programs as both "prog" and "prog.static"
(and the latter is, of course, static). So depending on the
default_library value may not be desirable.
The MBR is critical as it doesn't have a backup, so we only write it when it is
really useful (ie: the current MBR is different than the new one).
Signed-off-by: Philippe Reynes <philippe.reynes@softathome.com>
The function mkostemp() does not have to be available everywhere,
and for this reason, we have mkstemp_cloexec() as a fallback solution.
Unfortunately, some codes (usually fuzzy tests) do not use the
fallback. Let's fix it.
Signed-off-by: Karel Zak <kzak@redhat.com>
LastUsableLBA (field in GPT header) is calculated to use all free
space on the device. The problem is that size of this area is
unaligned to optimal I/O (or physical sector size) on devices where
the logical sector is 512 bytes, but the physical sector size is 4K.
(Note that after LastUsableLBA is backup GPT header and size of this
backup is calculated by logical sectors.)
The problem is visible for the last partition on the device because
this partition ends on LastUsableLBA.
The ideal solution would be align directly LastUsableLBA number in the
GPT header. Unfortunately, this solution is useless for libfdisk,
because we have to be backwardly compatible with sfdisk scripts
generated by old versions where is explicitly specified partition
size.
It seems usable compromise is to align only last partition if the size
of the partition is unspecified or specified by relative number
(+100M) -- in this case we already align partition, so there is no
change in fdisk semantic (well, last partition will be smaller than in
previous libfdisk versions).
The unaligned partition size is a problem for dm-crypt where device
size is used dynamically, and when unaligned, DM uses 512-bytes with a
negative impact on performance.
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=2044108
Addresses: https://marc.info/?l=dm-crypt&m=164306225923513&w=2
Signed-off-by: Karel Zak <kzak@redhat.com>
The field "First LBA" is confusing; let's use "First usable LBA" as
it's the official UEFI name for the field.
Addresses: https://github.com/util-linux/util-linux/issues/1554
Signed-off-by: Karel Zak <kzak@redhat.com>
* let's use the same code for both supported formats
* add support for '+,' to enlarge by start move (see previous commit)
Signed-off-by: Karel Zak <kzak@redhat.com>
Now it's possible move start of the partition only when offset or
absolute value is specified. This commit implements resize to "use all
available free space before the current start".
We already support the same for end of the partition (resize to use all
free space after the partition).
Signed-off-by: Karel Zak <kzak@redhat.com>
This function fixes beginning and ending CHS values for every partition
according to LBA relative offset, relative beginning and size and fdisk
idea of disk geometry (sectors per track and number of heads). This
function may be used for repairing existing partitions to be compatible
with CHS addressing.
Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
Call this function everytime after changing either relative LBA partition
offset or LBA partition size to ensure that CHS values are in sync with
LBA.
This should fix partition CHS values after moving or deleting partition.
Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
Since libblkid commit 751eca28fc it does
not ignore FAT on whole-disks, so now libfdisk sees collision between
FAT and MBR. It's fine to report the collision to users, but we should
not ignore the MBR.
Signed-off-by: Karel Zak <kzak@redhat.com>
Current algorithm implemented in get_partition_table_geometry() function
is wrong. It take head bits from CHS position from the end of the last
partition and proclaim that this value is number of heads on disk. Same
does for sectors bits from CHS and proclaim that this value is number of
sectors per tracks. As number of disk heads and head bits in CHS position
are different things this just results in bogus detected disk geometry.
Implement a new algorithm to determinate number of heads and sectors per
track from partition sector offsets. In MBR table for every partition is
stored every sector offset in two formats: one as CHS and one as LBA.
Conversion from CHS to LBA is defined by formula:
LBA = (c * N_heads + h) * N_sectors + (s - 1)
So if we have two different offsets in both CHS and LBA formats, written as
(LBA1, c1, h1, s1) and (LBA2, c2, h2, s2) then we can calculate number of
heads and sectors per tracks by solving linear equations as:
N_heads = (o1 * h2 - o2 * h1) / (o2 * c1 - o1 * c2)
N_sectors = (o2 * c1 - o1 * c2) / (c1 * h2 - c2 * h1)
where
o = LBA - (s - 1)
Integer division also verifies that offsets were calculated from same
number of heads and sectors per track as division must be always positive
with no reminder.
There is a special case when either numerator or denominator is zero. This
indicates that comparing partition offsets have also same CHS offset
divisor which leads to fact that calculation of number of heads and sector
per tracks is not possible.
In MBR table are stored up to 8 offset numbers, so it means there are up
to the 28 of candidates which can expose number of disk heads and number of
disk sectors per track. Start with offsets which belongs to same partition
and start from the first partition. As the first partition in most cases
was used by BIOS for booting.
It is required to skip CHS values 1023/254/63 and 1023/255/63 as these
indicates that values are invalid or overflowed.
Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
To determinate if CHS values are valid for consistency check and not
overflowed, it is needed to check current CHS cylinder value and not total
disk cylinder values. It is possible that also on "big" disk there is first
small partition which can be represented by CHS tuple.
Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
To check if LBA sector can be represented in CHS without overflow it is
required to check for cylinder value which belongs to the LBA sector. And
not the total number of disk cylinders. Note that maximal representable
total number of disk cylinders is 1024 and therefore the last cylinder
which which can be stored in CHS tuple is 1023. Hence strict inequality is
used.
Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
It does not matter if dos compatibility flag is turned on or off. CHS
values in partition entry can overflow also when dos compatibility flag is
turned off. In this case maximal representable CHS values should be used.
Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
Function check_consistency() prints errors to console output but does not
calculate correct error numbers in summary error message. Fix it, so fdisk
prints correct number in summary message "N errors detected.".
Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
There is no reason to check only end CHS partition parameters. Check also
begin values and correctly count errors in nerrors variable. fdisk then
print correctly summary message "N errors detected.".
Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
Function check() checks that LBA addresses sector number matches CHS sector
number. But error message contains information about "previous sectors" and
"total sectors" which is misleading so change it.
Also rename variables 'start' and 'total' to make it clear what value is
stored in them. Note that in 'start' variable is currently stored last LBA
partition sector and in 'total' is stored last CHS partition sector. So
neither sector where partition starts nor total sectors of partition.
Code and check logic looks to be correct, just error message and variable
names are misleading. Therefore no functional changes in this patch.
Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
Number of CHS sectors (indexed from zero) is stored in variable 'real_s'.
In variable 's' is stored in high 2 bits number of CHS cylinders.
Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
After geometry is changes it is required to recalculate total number of
cylinders. Otherwise total disk capacity is either reduced or extended.
Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
The function fdisk_reorder_partitions() is also used in sfdisk and cfdisk
and these commands assume info/warn messages from the library. So move all
messages from fdisk to fdisk_reorder_partitions().
Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
Option 'f' currently prints following RED error message:
Nothing to do. Ordering is correct already.
Failed to fix partitions order.
This change removes RED error message when ordering is already correct.
Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
The code should be able to keep track about previous content and print
JSON objects separator automatically.
Signed-off-by: Karel Zak <kzak@redhat.com>
Otherwise, sfdisk would error out on its own dump, as first-lba would be
earlier than "plausible" assuming the default table size.
This is because it did not look for `table-length` when using an sfdisk
script. This means that using `--dump` on a valid image, could not be
unserialized back into a valid image.
This is helpful in exotic situations, where a partition has to be
created earlier than the conventional "first-lba" (2048). For example,
on Allwinner A64 boards, where a firmware is stored at LBA16. With this
it is possible to ask for `first-lba` at or earlier than 16, by reducing
the number of entries to e.g. 48.
It's seems detection by BLKRRPART is broken in recent kernels
(probably regression), and it's also overkill to force kernel re-read
all and generate all the events. It seems more elegant to use O_EXCL.
Signed-off-by: Karel Zak <kzak@redhat.com>