]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/resizepart.c
blockdev: add missing verbose output for --getsz
[thirdparty/util-linux.git] / disk-utils / resizepart.c
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * Copyright (C) 2012 Karel Zak <kzak@redhat.com>
10 */
11 #include <getopt.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <fcntl.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18
19 #include "c.h"
20 #include "nls.h"
21 #include "partx.h"
22 #include "sysfs.h"
23 #include "strutils.h"
24 #include "closestream.h"
25
26 static void __attribute__((__noreturn__)) usage(void)
27 {
28 FILE *out = stdout;
29 fputs(USAGE_HEADER, out);
30 fprintf(out, _(" %s <disk device> <partition number> <length>\n"),
31 program_invocation_short_name);
32
33 fputs(USAGE_SEPARATOR, out);
34 fputs(_("Tell the kernel about the new size of a partition.\n"), out);
35
36 fputs(USAGE_OPTIONS, out);
37 fprintf(out, USAGE_HELP_OPTIONS(16));
38 fprintf(out, USAGE_MAN_TAIL("resizepart(8)"));
39 exit(EXIT_SUCCESS);
40 }
41
42 static int get_partition_start(int fd, int partno, uint64_t *start)
43 {
44 struct stat st;
45 struct path_cxt *disk = NULL, *part = NULL;
46 dev_t devno = 0;
47 int rc = -1;
48
49 /*
50 * wholedisk
51 */
52 if (fstat(fd, &st) || !S_ISBLK(st.st_mode))
53 goto done;
54 devno = st.st_rdev;
55 disk = ul_new_sysfs_path(devno, NULL, NULL);
56 if (!disk)
57 goto done;
58 /*
59 * partition
60 */
61 devno = sysfs_blkdev_partno_to_devno(disk, partno);
62 if (!devno)
63 goto done;
64
65 part = ul_new_sysfs_path(devno, disk, NULL);
66 if (!part)
67 goto done;
68 if (ul_path_read_u64(part, start, "start"))
69 goto done;
70
71 rc = 0;
72 done:
73 ul_unref_path(part);
74 ul_unref_path(disk);
75 return rc;
76 }
77
78 int main(int argc, char **argv)
79 {
80 int c, fd, partno;
81 const char *wholedisk;
82 uint64_t start;
83
84 static const struct option longopts[] = {
85 {"help", no_argument, NULL, 'h'},
86 {"version", no_argument, NULL, 'V'},
87 {NULL, 0, NULL, '0'},
88 };
89
90 setlocale(LC_ALL, "");
91 bindtextdomain(PACKAGE, LOCALEDIR);
92 textdomain(PACKAGE);
93 close_stdout_atexit();
94
95 while ((c = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
96 switch (c) {
97 case 'V':
98 print_version(EXIT_SUCCESS);
99 case 'h':
100 usage();
101 default:
102 errtryhelp(EXIT_FAILURE);
103 }
104
105 if (argc != 4) {
106 warnx(_("not enough arguments"));
107 errtryhelp(EXIT_FAILURE);
108 }
109
110 wholedisk = argv[1];
111 partno = strtou32_or_err(argv[2], _("invalid partition number argument"));
112
113 if ((fd = open(wholedisk, O_RDONLY)) < 0)
114 err(EXIT_FAILURE, _("cannot open %s"), wholedisk);
115
116 if (get_partition_start(fd, partno, &start))
117 err(EXIT_FAILURE, _("%s: failed to get start of the partition number %s"),
118 wholedisk, argv[2]);
119
120 if (partx_resize_partition(fd, partno, start,
121 strtou64_or_err(argv[3], _("invalid length argument"))))
122 err(EXIT_FAILURE, _("failed to resize partition"));
123
124 if (close_fd(fd) != 0)
125 err(EXIT_FAILURE, _("write failed"));
126
127 return 0;
128 }