2 * SPDX-License-Identifier: GPL-2.0-or-later
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.
9 * Copyright (C) 2012 Karel Zak <kzak@redhat.com>
15 #include <sys/types.h>
24 #include "closestream.h"
26 static void __attribute__((__noreturn__
)) usage(void)
29 fputs(USAGE_HEADER
, out
);
30 fprintf(out
, _(" %s <disk device> <partition number> <length>\n"),
31 program_invocation_short_name
);
33 fputs(USAGE_SEPARATOR
, out
);
34 fputs(_("Tell the kernel about the new size of a partition.\n"), out
);
36 fputs(USAGE_OPTIONS
, out
);
37 fprintf(out
, USAGE_HELP_OPTIONS(16));
38 fprintf(out
, USAGE_MAN_TAIL("resizepart(8)"));
42 static int get_partition_start(int fd
, int partno
, uint64_t *start
)
45 struct path_cxt
*disk
= NULL
, *part
= NULL
;
52 if (fstat(fd
, &st
) || !S_ISBLK(st
.st_mode
))
55 disk
= ul_new_sysfs_path(devno
, NULL
, NULL
);
61 devno
= sysfs_blkdev_partno_to_devno(disk
, partno
);
65 part
= ul_new_sysfs_path(devno
, disk
, NULL
);
68 if (ul_path_read_u64(part
, start
, "start"))
78 int main(int argc
, char **argv
)
81 const char *wholedisk
;
84 static const struct option longopts
[] = {
85 {"help", no_argument
, NULL
, 'h'},
86 {"version", no_argument
, NULL
, 'V'},
90 setlocale(LC_ALL
, "");
91 bindtextdomain(PACKAGE
, LOCALEDIR
);
93 close_stdout_atexit();
95 while ((c
= getopt_long(argc
, argv
, "Vh", longopts
, NULL
)) != -1)
98 print_version(EXIT_SUCCESS
);
102 errtryhelp(EXIT_FAILURE
);
106 warnx(_("not enough arguments"));
107 errtryhelp(EXIT_FAILURE
);
111 partno
= strtou32_or_err(argv
[2], _("invalid partition number argument"));
113 if ((fd
= open(wholedisk
, O_RDONLY
)) < 0)
114 err(EXIT_FAILURE
, _("cannot open %s"), wholedisk
);
116 if (get_partition_start(fd
, partno
, &start
))
117 err(EXIT_FAILURE
, _("%s: failed to get start of the partition number %s"),
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"));
124 if (close_fd(fd
) != 0)
125 err(EXIT_FAILURE
, _("write failed"));