]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/blockdev-util.h
Merge pull request #26146 from keszybz/two-man-page-tweaks
[thirdparty/systemd.git] / src / shared / blockdev-util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <sys/types.h>
5
6 #include "sd-device.h"
7
8 #include "macro.h"
9 #include "stdio-util.h"
10 #include "string-util.h"
11
12 #define SYS_BLOCK_PATH_MAX(suffix) \
13 (STRLEN("/sys/dev/block/") + DECIMAL_STR_MAX(dev_t) + 1 + DECIMAL_STR_MAX(dev_t) + strlen_ptr(suffix))
14 #define xsprintf_sys_block_path(buf, suffix, devno) \
15 xsprintf(buf, "/sys/dev/block/%u:%u%s", major(devno), minor(devno), strempty(suffix))
16
17 typedef enum BlockDeviceLookupFlag {
18 BLOCK_DEVICE_LOOKUP_WHOLE_DISK = 1 << 0, /* whole block device, e.g. sda, nvme0n1, or loop0. */
19 BLOCK_DEVICE_LOOKUP_BACKING = 1 << 1, /* fd may be regular file or directory on file system, in
20 * which case backing block device is determined. */
21 BLOCK_DEVICE_LOOKUP_ORIGINATING = 1 << 2, /* Try to find the underlying layer device for stacked
22 * block device, e.g. LUKS-style DM. */
23 } BlockDeviceLookupFlag;
24
25 int block_device_new_from_fd(int fd, BlockDeviceLookupFlag flag, sd_device **ret);
26 int block_device_new_from_path(const char *path, BlockDeviceLookupFlag flag, sd_device **ret);
27
28 int block_device_is_whole_disk(sd_device *dev);
29 int block_device_get_whole_disk(sd_device *dev, sd_device **ret);
30
31 int block_get_whole_disk(dev_t d, dev_t *ret);
32 int block_get_originating(dev_t d, dev_t *ret);
33
34 int get_block_device_fd(int fd, dev_t *ret);
35 int get_block_device(const char *path, dev_t *dev);
36
37 int get_block_device_harder_fd(int fd, dev_t *dev);
38 int get_block_device_harder(const char *path, dev_t *dev);
39
40 int lock_whole_block_device(dev_t devt, int operation);
41
42 int blockdev_partscan_enabled(int fd);
43
44 int fd_is_encrypted(int fd);
45 int path_is_encrypted(const char *path);
46
47 int fd_get_whole_disk(int fd, bool backing, dev_t *ret);
48 int path_get_whole_disk(const char *path, bool backing, dev_t *ret);
49
50 int block_device_add_partition(int fd, const char *name, int nr, uint64_t start, uint64_t size);
51 int block_device_remove_partition(int fd, const char *name, int nr);
52 int block_device_resize_partition(int fd, int nr, uint64_t start, uint64_t size);
53 int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret);
54 int block_device_remove_all_partitions(sd_device *dev, int fd);
55 int block_device_has_partitions(sd_device *dev);
56 int blockdev_reread_partition_table(sd_device *dev);
57
58 int blockdev_get_sector_size(int fd, uint32_t *ret);