Commit Graph

768 Commits

Author SHA1 Message Date
Sami Kerola 364de8f4f5 lib/randutils: rename random_get_bytes()
Rename random_get_bytes() to avoid colliding a Solaris library function that
has the same name.

Reported-by: Sad Clouds <cryintothebluesky@gmail.com>
Reference: https://lore.kernel.org/util-linux/20201101141608.ba365cf67d92ee3973226de9@gmail.com/
Reference: https://blogs.oracle.com/solaris/solaris-random-number-generation-v2
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-11-03 12:38:45 +01:00
Karel Zak 75cd5e2f28 losetup: increase limit of setup attempts
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-10-13 12:55:44 +02:00
Karel Zak b11e1b7ea1 libfdisk: (gpt) reduce number of entries to fit small device
The default is 128 partitions (entries in partitions array). The size
of the entry is 128 bytes, it means 32 sectors (512-byte) for the
array. The default is too large for tiny devices and it seems better to
dynamically reduce the number to fix the device size.

The reduction is reported to user.

Example:

 $ dd if=/dev/zero of=8-sectors.img count=8 bs=512
 $ fdisk 8-sectors.img
 ...
 Command (m for help): g
 Created a new GPT disklabel (GUID: 3DB3EECE-BCCA-6C46-95FA-7E23783BB3B2).
 The maximal number of partitions is 8 (default is 128).

 Command (m for help): x

 Expert command (m for help): p
 Disk 8-sectors.img: 4 KiB, 4096 bytes, 8 sectors
 Units: sectors of 1 * 512 = 512 bytes
 Sector size (logical/physical): 512 bytes / 512 bytes
 I/O size (minimum/optimal): 512 bytes / 512 bytes
 Disklabel type: gpt
 Disk identifier: 3DB3EECE-BCCA-6C46-95FA-7E23783BB3B2
 First LBA: 4
 Last LBA: 4
 Alternative LBA: 7
 Partition entries LBA: 2
 Allocated partition entries: 8

In this case, sectors:
 0   - PMBR
 1   - GPT header
 2-3 - GPT array of partitions (8 entries, 128 bytes each)
 4   - partition data
 5-6 - backup GPT array of partitions
 7   - backup GPT header

The smallest image is 6 sectors with 4 entries in array of partitions.

Note that Linux kernel has no problem to accept 4K image with 1
512-bytes GPT partition.

  # losetup -P -f 4K.img
  # lsblk /dev/loop0
  NAME      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
  loop0       7:0    0    4K  0 loop
  └─loop0p1 259:5    0  512B  0 part

... but we need to fix libblkid, it ignores so small devices to probe for GPT :)

Addresses: https://github.com/karelzak/util-linux/issues/1147
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-09-30 12:38:59 +02:00
Karel Zak ffac9652c7 libfdisk: (gpt) make sure device is large enough
The current code creates GPT header and partitions arrays (with 128
entries ...) although there is no space for all the stuff. This patch
forces fdisk_create_disklabel() to return -ENOSPC if the last and first
usable LBA calculation is out of device size.

Addresses: https://github.com/karelzak/util-linux/issues/1147
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-09-30 11:44:03 +02:00
yangzz-97 933f095feb libfdisk: reset context FD on error
When the query disk fails, reset cxt->dev_fd.

Addresses: https://github.com/karelzak/util-linux/pull/1137
Addresses: https://github.com/karelzak/util-linux/issues/1131
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-09-07 12:09:36 +02:00
Adriaan de Groot bc743b355c
Generate valid JSON if partition table is empty
When the partition table is present **but** empty, the existing code would output fields followed by a `,` , on the assumption that the list of partitions would follow. But if the list of partitions is empty, it is skipped, leading to output like this:

```
{
  "partitiontable": {
      "label":"gpt",
      "id":"1F9E80D9-DD78-024F-94A3-B61EC82B18C8",
      "device":"/dev/sdb",
      "unit":"sectors",
      "firstlba":2048,
      "lastlba":30949342,
      "sectorsize":512,
  }
}
```

Note the `512,` on the *sectorsize* line.

This is invalid JSON for some parsers, which choke on it.

Avoid this, by checking when outputting the last separator: if there's no table, or there is a table but it is empty, then just put a newline, otherwise use the old path of comma-newline and assume there's going to be a list of partitions after.
2020-08-24 23:30:35 +02:00
Karel Zak 1f50296c0f libfdisk: (script) fix possible partno overflow
Addresses: https://oss-fuzz.com/testcase-detail/5740890480705536
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-08-14 11:13:50 +02:00
Karel Zak 678d03cc8a libfdisk: (script) fix possible memory leaks
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-08-13 13:48:28 +02:00
Karel Zak d8f35960ae libfdisk: another parse_line_nameval() cleanup 2020-08-13 10:13:01 +02:00
Karel Zak 9f03ad60e5 libfdisk: make fdisk_partname() more robust 2020-08-13 10:12:01 +02:00
Karel Zak 3b87a9af49 libfdisk: fix typo from 255f5f4c77
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-08-12 19:48:47 +02:00
Karel Zak 4bdb681571 libfdisk: (script) make sure buffer is initialized
Just to make static analyzers happy.

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-08-12 19:39:34 +02:00
Karel Zak 8bbc11f12f libfdisk: (script) don't use sector size if not specified
This is probably bad script API use, but better be safe than sorry.

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-08-12 15:59:38 +02:00
Karel Zak 255f5f4c77 libfdisk: (script) make sure label is specified
and unref type if already specified (unlikely, but be paranoid)

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-08-12 15:52:53 +02:00
Evgeny Vereshchagin b62ec52e8f tests: add a fuzz target calling fdisk_script_read_file
It has already found a couple of issues mentioned in
https://github.com/karelzak/util-linux/issues/1023#issuecomment-671910621
2020-08-10 23:30:50 +00:00
Karel Zak 4fe7f9b614 libfdisk: fix last free sector detection if partition size specified
We need to skip useless gaps between partition if the gap is no large
enough for a new partition. Unfortunately, the current code checks
size of the gap, but does not care for location of the gap -- this is
good enough for dialog driven partitioning, but it's pretty bad if
start of the partition is explicitly specified (e.g. sfdisk).

Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1860461
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-08-10 11:37:32 +02:00
Karel Zak 33f50706fd libfdisk: fix fdisk_reread_changes() for extended partitions
Linux kernel assumes only 1KiB extended partition to avoid overlapping
with nested logical partitions. We need to follow this rule for
BLKPG_ADD_PARTITION.

Addresses: https://github.com/karelzak/util-linux/issues/1112
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-08-06 11:32:33 +02:00
Karel Zak c709dbcdb5 libfdisk: (docs) add missing comment
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-22 10:44:47 +02:00
Karel Zak f11fe35f5e libfdisk: (docs) add missing references
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-22 10:33:53 +02:00
Karel Zak 29e204d117 docs: reword others "allow to"
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-14 14:31:29 +02:00
Yuri Chornoivan aa1c7a7686 More typo fixes by fossies 2020-07-13 15:33:42 +03:00
Yuri Chornoivan 311e33afef Fix minor typos 2020-07-09 20:14:32 +03:00
Karel Zak d118738095 libfdisk: (gpt) fix compiler warning [-Wmaybe-uninitialized]
Make sure that variables used in  gpt_calculate_* are always initialized.

Addresses: https://github.com/karelzak/util-linux/issues/1091
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-03 12:30:59 +02:00
Toni Uhlig 9eba847689
libfdisk, unshare: fix gcc-4.9.4 warnings
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
2020-06-19 20:37:33 +02:00
Karel Zak 967b8c2579 libfdisk: (docs) document new functions
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-06-09 10:50:42 +02:00
Karel Zak c7fce443b1 libfdisk: (dos) be more explicit in fdisk_verify_disklabel() output
Let's print number of detected errors or "No errors detected." for
MBR. We already use the same for GPT.

The patch also modifies fdisk_verify_disklabel() return code to inform
caller about number of issues.

Addresses: https://github.com/karelzak/util-linux/issues/1051
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-05-27 17:49:26 +02:00
Karel Zak 83fdb880b9 libfdisk: (dos) fix default partition start
The current code implements the default start as first free space, but
it does not check if the space is large enough.

Addresses: https://github.com/karelzak/util-linux/issues/1044
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-05-21 15:37:59 +02:00
Karel Zak 1001633991 libfdisk: (dos) be more robust about max number of partitions
It seems the previous MAXIMUM_PARTS bug fix has not been generic
enough. Let's use the check for all dos_add_partition().

This patch also remove libfdisk attempt to be smart and search for
unused primary partition. If you need more than 60 partition in MBR
than you have a different problem that cannot be solved by libfdisk
...

References: b80fb30a5b
Addresses: https://github.com/karelzak/util-linux/issues/1022
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-05-18 14:13:33 +02:00
Karel Zak 6a4d53ce64 libfdisk: fix partition calculation for BLKPG_* ioctls
The include/partx.h interface we use in util-linux uses 512-byte
sectors, but libfdisk uses real sector sizes.

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-05-06 13:32:46 +02:00
Karel Zak 89c0297bcc libfdisk: fix alignment logic for tiny partitions
Addresses: https://github.com/karelzak/util-linux/issues/1018 (second case)
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-04-29 11:10:54 +02:00
Karel Zak b80fb30a5b libfdisk: make sure we check for maximal number of partitions
Addresses: https://github.com/karelzak/util-linux/issues/1018
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-04-28 12:58:42 +02:00
Karel Zak 243fff1dc6 libfdisk: (script) fix partno_from_devname()
Addresses: https://github.com/karelzak/util-linux/issues/1015
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-04-27 11:15:00 +02:00
Rosen Penev ad296391f9
[clang-tidy] fix wrong *cmp usage
Found with bugprone-suspicious-string-compare

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-04-20 13:21:00 -07:00
Rosen Penev 042f62dfc5
[clang-tidy] do not use else after return
Found with readability-else-after-return

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-04-20 13:20:59 -07:00
Karel Zak ca6e434a7d libfdisk: remove unwanted assert()
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-03-24 10:52:54 +01:00
Gaël PORTAY 3468dda9c3 libfdisk: (script) fix segmentation fault
This patch fixes a segmentation fault that occurs if the name value is
empty (i.e. name=).

Signed-off-by: Gaël PORTAY <gael.portay@collabora.com>
2020-03-20 16:28:33 -04:00
Karel Zak f94e753b35 libfdisk: add partition type aliases and shortcuts
Now, the type shortcuts are supported for sfdisk scripts only.

Unfortunately, the current implementation is not generic enough
and it's also fragile as 'E' shortcut is in collision with 0x0E
type for MBR. The another issue is 'L' which makes shortcuts useless
for fdisk where 'L' is used for another purpose in dialogs.

This patch introduces partition type aliases as extension to
shortcuts. The definition of the shortcut is part of the label
definition and it's not more hardcoded in sfdisk script code.

This patch also introduces 'Ex' shortcut as replacement for (now
deprecated) 'E'.

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-03-03 15:59:49 +01:00
Karel Zak ce8985cc71 libfdisk: (gpt) partition name default to empty string
Just for backward compatibility, otherwise it's strange ;-)

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-02-25 12:41:10 +01:00
Karel Zak f4189a087a libfdisk: add missing comments
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-02-25 12:25:17 +01:00
Karel Zak 487b57f58b libfdisk: use ul_encode_to_utf8()
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-02-25 12:21:32 +01:00
Sami Kerola d8ed9e038c
libfdisk: fix pointer wraparound warning
libfdisk/src/gpt.c:1713:6: warning: assuming pointer wraparound does not
occur when comparing P +- C1 with P +- C2 [-Wstrict-overflow]

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-02-15 15:24:02 +00:00
Karel Zak b7c015d1c5 libfdisk: (docs) fix typos
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-02-05 14:50:05 +01:00
Karel Zak 387ac2771f libfdisk: (gpt) add functionality to move backup header
- add fdisk_gpt_disable_relocation() to disable move backup header to standard location

- add fdisk_gpt_enable_minimize() to move backup header behind last partition

Note that fdisk_gpt_disable_relocation() has to be used before fdisk_assign_device(),
because automatic relocation is done when libfdisk reads the header from the device.

For example:
	lb = fdisk_get_label(cxt, "gpt");

	fdisk_gpt_disable_relocation(lb, 1);
	fdisk_assign_device(cxt, devname, 0);

For fdisk_gpt_enable_minimize() it's not important as recalculation is
done before write, for example:

	fdisk_assign_device(cxt, devname, 0);
	fdisk_gpt_enable_minimize(fdisk_get_label(cxt, NULL), 1);

	... add partition, etc ...

	fdisk_write_disklabel(cxt);

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-02-05 14:37:41 +01:00
Karel Zak 36cd4b3c46 libfdisk: (gpt) add GPT debug mask
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-02-05 14:17:23 +01:00
Karel Zak afb27a8e4c libfdisk: (gpt) cleanup and consolidate write code
- add fsync after write
- use write_all()
- use the same code for all write steps

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-02-05 14:15:23 +01:00
Karel Zak d74b16bdf1 libfdisk: (script) fix memory leak
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-02-04 15:54:10 +01:00
Karel Zak d8c68b52cc libfdisk: (script) accept sector-size, ignore unknown headers
- add sector-size between supported headers (already in --dump output)

- report unknown headers by -ENOTSUP

- ignore ENOTSUP in sfdisk (but print warning) and in fdisk_script_read_file()

Addresses: https://github.com/karelzak/util-linux/issues/949
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-02-04 15:11:19 +01:00
Karel Zak caa37b6e18 libfdisk: fix __copy_partition()
The code called free() for pointers copied from the source partition.

Addresses: https://github.com/systemd/systemd/pull/14677
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-01-28 12:46:39 +01:00
Karel Zak dfe28f0e01 libfdisk: make sure we use NULL after free
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-01-28 12:29:44 +01:00
Karel Zak b1bc5ae39e libfdisk: (gpt) cleanup entries array size calculations
- move all calculations to inline functions
- remove duplicate code
- keep the same names for the same stuff

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-01-23 15:50:53 +01:00