-
Notifications
You must be signed in to change notification settings - Fork 0
/
gptextend.c
64 lines (58 loc) · 1.21 KB
/
gptextend.c
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
#define _FILE_OFFSET_BITS 64
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include "filesize.h"
#include "part.h"
#include "gpt.h"
#ifdef static_assert
static_assert(sizeof(loff_t)==sizeof(off_t));
#endif
static void
usage(void)
{
dprintf(2, "usage: gptextend [-n part] disk\n");
exit(1);
}
int
main(int argc, char **argv)
{
long long start, length;
const char *disk;
bool tellkernel;
off_t disksize;
int fd, part;
char c;
tellkernel = false;
part = -1;
while ((c = getopt(argc, argv, "+n:k")) != -1) {
switch (c) {
case 'k':
tellkernel = true;
break;
case 'n':
part = atoi(optarg);
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc <= 0)
usage();
disk = argv[0];
fd = open(disk, O_RDWR|O_CLOEXEC);
if (fd < 0)
err(1, "open %s", disk);
disksize = fgetsize(fd);
if (!disksize)
err(1, "computing disk size");
if ((part = gpt_add_lastpart(fd, part, disksize>>9, &start, &length)) < 0)
err(1, "adding partition");
if (tellkernel && kernel_add_part(fd, part, start, length) < 0)
err(1, "couldn't update kernel partition table");
close(fd);
return 0;
}