summaryrefslogtreecommitdiffstats
path: root/include/partx.h
blob: 7924aae6acb9cc773f1915abc5eac0cb0d6abbd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * No copyright is claimed.  This code is in the public domain; do with
 * it what you wish.
 */
#ifndef UTIL_LINUX_PARTX_H
#define UTIL_LINUX_PARTX_H

#include <sys/ioctl.h>
#include <linux/blkpg.h>
#include <stdint.h>

#ifndef BLKPG_ADD_PARTITION
# define BLKPG_ADD_PARTITION	1
#endif

#ifndef BLKPG_DEL_PARTITION
# define BLKPG_DEL_PARTITION	2
#endif

#ifndef BLKPG_RESIZE_PARTITION
# define BLKPG_RESIZE_PARTITION	3		/* since Linux 3.6 */
#endif


#define INIT_BLKPG_PARTITION(_partno, _start, _size) {	\
		.pno    = (_partno),			\
		.start  = (_start) << 9,		\
		.length = (_size) << 9,			\
		.devname[0] = 0,			\
		.volname[0] = 0				\
}

#define INIT_BLKPG_ARG(_action, _part) {		\
		.op      = (_action),			\
		.flags   = 0,				\
		.datalen = sizeof(*(_part)),		\
		.data = (_part)				\
}


static inline int partx_del_partition(int fd, unsigned int partno)
{
	struct blkpg_partition p = INIT_BLKPG_PARTITION(partno, 0, 0);
	struct blkpg_ioctl_arg a = INIT_BLKPG_ARG(BLKPG_DEL_PARTITION, &p);

	return ioctl(fd, BLKPG, &a);
}

static inline int partx_add_partition(int fd, int partno,
			uint64_t start, uint64_t size)
{
	struct blkpg_partition p = INIT_BLKPG_PARTITION(partno, start, size);
	struct blkpg_ioctl_arg a = INIT_BLKPG_ARG(BLKPG_ADD_PARTITION, &p);

	return ioctl(fd, BLKPG, &a);
}

static inline int partx_resize_partition(int fd, int partno,
			uint64_t start, uint64_t size)
{
	struct blkpg_partition p = INIT_BLKPG_PARTITION(partno, start, size);
	struct blkpg_ioctl_arg a = INIT_BLKPG_ARG(BLKPG_RESIZE_PARTITION, &p);

	return ioctl(fd, BLKPG, &a);
}

#endif /*  UTIL_LINUX_PARTX_H */