]>
Commit | Line | Data |
---|---|---|
8863a802 KZ |
1 | #include <getopt.h> |
2 | #include <stdio.h> | |
3 | #include <stdlib.h> | |
4 | #include <fcntl.h> | |
5 | #include <sys/types.h> | |
6 | #include <sys/stat.h> | |
7 | #include <unistd.h> | |
8 | ||
9 | #include "c.h" | |
10 | #include "nls.h" | |
11 | #include "partx.h" | |
12 | #include "sysfs.h" | |
13 | #include "strutils.h" | |
dd79e7c9 | 14 | #include "closestream.h" |
8863a802 KZ |
15 | |
16 | static void __attribute__ ((__noreturn__)) usage(FILE * out) | |
17 | { | |
18 | fputs(USAGE_HEADER, out); | |
19 | fprintf(out, _(" %s <disk device> <partition number> <length>\n"), | |
20 | program_invocation_short_name); | |
451dbcfa BS |
21 | |
22 | fputs(USAGE_SEPARATOR, out); | |
23 | fputs(_("Tell the kernel about the new size of a partition.\n"), out); | |
24 | ||
8863a802 KZ |
25 | fputs(USAGE_OPTIONS, out); |
26 | fputs(USAGE_HELP, out); | |
27 | fputs(USAGE_VERSION, out); | |
28 | fprintf(out, USAGE_MAN_TAIL("resizepart(8)")); | |
29 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
30 | } | |
31 | ||
32 | static int get_partition_start(int fd, int partno, uint64_t *start) | |
33 | { | |
34 | struct stat st; | |
35 | struct sysfs_cxt disk = UL_SYSFSCXT_EMPTY, | |
36 | part = UL_SYSFSCXT_EMPTY; | |
37 | dev_t devno = 0; | |
38 | int rc = -1; | |
39 | ||
40 | /* | |
41 | * wholedisk | |
42 | */ | |
43 | if (fstat(fd, &st) || !S_ISBLK(st.st_mode)) | |
44 | goto done; | |
45 | devno = st.st_rdev; | |
46 | if (sysfs_init(&disk, devno, NULL)) | |
47 | goto done; | |
48 | /* | |
49 | * partition | |
50 | */ | |
51 | devno = sysfs_partno_to_devno(&disk, partno); | |
52 | if (!devno) | |
53 | goto done; | |
54 | if (sysfs_init(&part, devno, &disk)) | |
55 | goto done; | |
56 | if (sysfs_read_u64(&part, "start", start)) | |
57 | goto done; | |
58 | ||
59 | rc = 0; | |
60 | done: | |
61 | sysfs_deinit(&part); | |
62 | sysfs_deinit(&disk); | |
63 | return rc; | |
64 | } | |
65 | ||
66 | int main(int argc, char **argv) | |
67 | { | |
68 | int c, fd, partno; | |
69 | const char *wholedisk; | |
70 | uint64_t start; | |
71 | ||
72 | static const struct option longopts[] = { | |
87918040 SK |
73 | {"help", no_argument, NULL, 'h'}, |
74 | {"version", no_argument, NULL, 'V'}, | |
75 | {NULL, 0, NULL, '0'}, | |
8863a802 KZ |
76 | }; |
77 | ||
78 | setlocale(LC_ALL, ""); | |
79 | bindtextdomain(PACKAGE, LOCALEDIR); | |
80 | textdomain(PACKAGE); | |
dd79e7c9 | 81 | atexit(close_stdout); |
8863a802 KZ |
82 | |
83 | while ((c = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1) | |
84 | switch (c) { | |
85 | case 'V': | |
86 | printf(UTIL_LINUX_VERSION); | |
87 | return EXIT_SUCCESS; | |
88 | case 'h': | |
89 | usage(stdout); | |
90 | default: | |
677ec86c | 91 | errtryhelp(EXIT_FAILURE); |
8863a802 KZ |
92 | } |
93 | ||
94 | if (argc != 4) | |
95 | usage(stderr); | |
96 | ||
97 | wholedisk = argv[1]; | |
98 | partno = strtou32_or_err(argv[2], _("invalid partition number argument")); | |
99 | ||
100 | if ((fd = open(wholedisk, O_RDONLY)) < 0) | |
101 | err(EXIT_FAILURE, _("cannot open %s"), wholedisk); | |
102 | ||
103 | if (get_partition_start(fd, partno, &start)) | |
104 | err(EXIT_FAILURE, _("%s: failed to get start of the partition number %s"), | |
105 | wholedisk, argv[2]); | |
106 | ||
8863a802 KZ |
107 | if (partx_resize_partition(fd, partno, start, |
108 | strtou64_or_err(argv[3], _("invalid length argument")))) | |
109 | err(EXIT_FAILURE, _("failed to resize partition")); | |
110 | ||
dd79e7c9 SK |
111 | if (close_fd(fd) != 0) |
112 | err(EXIT_FAILURE, _("write failed")); | |
113 | ||
8863a802 KZ |
114 | return 0; |
115 | } |