]>
Commit | Line | Data |
---|---|---|
faeb1b64 KZ |
1 | /* |
2 | * No copyright is claimed. This code is in the public domain; do with | |
3 | * it what you wish. | |
4 | */ | |
c4ecaf21 DB |
5 | #ifndef UTIL_LINUX_PARTX_H |
6 | #define UTIL_LINUX_PARTX_H | |
7 | ||
8 | #include <sys/ioctl.h> | |
9 | #include <linux/blkpg.h> | |
e163adb8 | 10 | #include <stdint.h> |
c4ecaf21 | 11 | |
8863a802 KZ |
12 | #ifndef BLKPG_ADD_PARTITION |
13 | # define BLKPG_ADD_PARTITION 1 | |
14 | #endif | |
15 | ||
16 | #ifndef BLKPG_DEL_PARTITION | |
17 | # define BLKPG_DEL_PARTITION 2 | |
18 | #endif | |
19 | ||
20 | #ifndef BLKPG_RESIZE_PARTITION | |
21 | # define BLKPG_RESIZE_PARTITION 3 /* since Linux 3.6 */ | |
22 | #endif | |
23 | ||
24b7db7e KZ |
24 | |
25 | #define INIT_BLKPG_PARTITION(_partno, _start, _size) { \ | |
26 | .pno = (_partno), \ | |
27 | .start = (_start) << 9, \ | |
28 | .length = (_size) << 9, \ | |
29 | .devname[0] = 0, \ | |
30 | .volname[0] = 0 \ | |
31 | } | |
32 | ||
33 | #define INIT_BLKPG_ARG(_action, _part) { \ | |
34 | .op = (_action), \ | |
35 | .flags = 0, \ | |
36 | .datalen = sizeof(*(_part)), \ | |
37 | .data = (_part) \ | |
38 | } | |
39 | ||
40 | ||
659e5f5b | 41 | static inline int partx_del_partition(int fd, unsigned int partno) |
c4ecaf21 | 42 | { |
24b7db7e KZ |
43 | struct blkpg_partition p = INIT_BLKPG_PARTITION(partno, 0, 0); |
44 | struct blkpg_ioctl_arg a = INIT_BLKPG_ARG(BLKPG_DEL_PARTITION, &p); | |
c4ecaf21 DB |
45 | |
46 | return ioctl(fd, BLKPG, &a); | |
47 | } | |
22853e4a | 48 | |
c4ecaf21 | 49 | static inline int partx_add_partition(int fd, int partno, |
659e5f5b | 50 | uint64_t start, uint64_t size) |
c4ecaf21 | 51 | { |
24b7db7e KZ |
52 | struct blkpg_partition p = INIT_BLKPG_PARTITION(partno, start, size); |
53 | struct blkpg_ioctl_arg a = INIT_BLKPG_ARG(BLKPG_ADD_PARTITION, &p); | |
c4ecaf21 DB |
54 | |
55 | return ioctl(fd, BLKPG, &a); | |
22853e4a | 56 | } |
1d4ad1de | 57 | |
8863a802 KZ |
58 | static inline int partx_resize_partition(int fd, int partno, |
59 | uint64_t start, uint64_t size) | |
60 | { | |
24b7db7e KZ |
61 | struct blkpg_partition p = INIT_BLKPG_PARTITION(partno, start, size); |
62 | struct blkpg_ioctl_arg a = INIT_BLKPG_ARG(BLKPG_RESIZE_PARTITION, &p); | |
8863a802 KZ |
63 | |
64 | return ioctl(fd, BLKPG, &a); | |
65 | } | |
66 | ||
c4ecaf21 | 67 | #endif /* UTIL_LINUX_PARTX_H */ |