sfdisk: add --no-reread and --force

and also check if the device is in use.

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2014-09-19 10:30:14 +02:00
parent f01f252857
commit db48b6a189
1 changed files with 50 additions and 2 deletions

View File

@ -80,6 +80,8 @@ struct sfdisk {
unsigned int verify : 1, /* call fdisk_verify_disklabel() */
quiet : 1, /* suppres extra messages */
noreread : 1, /* don't check device is in use */
force : 1, /* do also stupid things */
noact : 1; /* do not write to device */
};
@ -702,6 +704,24 @@ static int loop_control_commands(struct sfdisk *sf,
return rc;
}
static int is_device_used(struct sfdisk *sf)
{
#ifdef BLKRRPART
struct stat st;
int fd;
assert(sf);
assert(sf->cxt);
fd = fdisk_get_devfd(sf->cxt);
if (fd < 0)
return 0;
if (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode))
return ioctl(fd, BLKRRPART) != 0;
#endif
return 0;
}
/*
* sfdisk <device> [[-N] <partno>]
@ -763,6 +783,23 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
"Be careful before using the write command.\n"));
}
if (!sf->noact && !sf->noreread) {
if (!sf->quiet)
fputs(_("Checking that no-one is using this disk right now ..."), stdout);
if (is_device_used(sf)) {
fputs(_(" FAILED\n\n"), stdout);
fdisk_warnx(sf->cxt, _(
"This disk is currently in use - repartitioning is probably a bad idea.\n"
"Umount all file systems, and swapoff all swap partitions on this disk.\n"
"Use the --no-reread flag to suppress this check.\n"));
if (!sf->force)
errx(EXIT_FAILURE, _("Use the --force flag to overrule all checks."));
} else
fputs(_(" OK\n\n"), stdout);
}
if (!sf->quiet) {
list_disk_geometry(sf->cxt);
if (fdisk_has_label(sf->cxt)) {
@ -921,10 +958,12 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out);
fputs(USAGE_OPTIONS, out);
fputs(_(" -f, --force disable all consistency checking\n"), out);
fputs(_(" -N, --partno <num> specify partition number\n"), out);
fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out);
fputs(_(" -q, --quiet suppress extra info messages\n"), out);
fputs(_(" -n, --no-act do everything except write to device\n"), out);
fputs(_(" --no-reread do not check whether the device is in use\n"), out);
fputs(USAGE_SEPARATOR, out);
fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out);
fputs(_(" -L, --Linux deprecated and ignored, only for backward copatibility\n"), out);
@ -949,17 +988,20 @@ int main(int argc, char *argv[])
enum {
OPT_CHANGE_ID = CHAR_MAX + 1,
OPT_PRINT_ID,
OPT_ID
OPT_ID,
OPT_NOREREAD
};
static const struct option longopts[] = {
{ "activate",no_argument, NULL, 'a' },
{ "dump", no_argument, NULL, 'd' },
{ "help", no_argument, NULL, 'h' },
{ "force", no_argument, NULL, 'f' },
{ "label", required_argument, NULL, 'X' },
{ "list", no_argument, NULL, 'l' },
{ "list-types", no_argument, NULL, 'T' },
{ "no-act", no_argument, NULL, 'n' },
{ "no-reread", no_argument, NULL, OPT_NOREREAD },
{ "partno", required_argument, NULL, 'N' },
{ "show-size", no_argument, NULL, 's' },
{ "show-geometry", no_argument, NULL, 'g' },
@ -983,7 +1025,7 @@ int main(int argc, char *argv[])
textdomain(PACKAGE);
atexit(close_stdout);
while ((c = getopt_long(argc, argv, "adhglLnN:qsTu:vVX:",
while ((c = getopt_long(argc, argv, "adfhglLnN:qsTu:vVX:",
longopts, &longidx)) != -1) {
switch(c) {
case 'a':
@ -998,6 +1040,9 @@ int main(int argc, char *argv[])
case 'c':
sf->act = ACT_PARTTYPE;
break;
case 'f':
sf->force = 1;
break;
case 'h':
usage(stdout);
break;
@ -1044,6 +1089,9 @@ int main(int argc, char *argv[])
case 'V':
sf->verify = 1;
break;
case OPT_NOREREAD:
sf->noreread = 1;
break;
default:
usage(stderr);
}