int partno; /* -N <partno>, default -1 */
int wipemode; /* remove foreign signatures from disk */
int pwipemode; /* remove foreign signatures from partitions */
+ const char *lockmode; /* as specified by --lock */
const char *label; /* --label <label> */
const char *label_nested; /* --label-nested <label> */
const char *backup_file; /* -O <path> */
free(tpl);
}
+static int assign_device(struct sfdisk *sf, const char *devname, int rdonly)
+{
+ struct fdisk_context *cxt = sf->cxt;
+
+ if (fdisk_assign_device(cxt, devname, rdonly) != 0)
+ err(EXIT_FAILURE, _("cannot open %s"), devname);
+
+ if (!fdisk_is_readonly(cxt)) {
+ if (blkdev_lock(fdisk_get_devfd(cxt), devname, sf->lockmode) != 0) {
+ fdisk_deassign_device(cxt, 1);
+ exit(EXIT_FAILURE);
+ }
+ if (sf->backup)
+ backup_partition_table(sf, devname);
+ }
+ return 0;
+}
+
+
static int move_partition_data(struct sfdisk *sf, size_t partno, struct fdisk_partition *orig_pa)
{
struct fdisk_partition *pa = get_partition(sf->cxt, partno);
fdisk_enable_listonly(sf->cxt, 1);
- if (fdisk_assign_device(sf->cxt, devname, 1)) {
- warn(_("cannot open %s"), devname);
- return 1;
- }
+ assign_device(sf, devname, 1);
color_scheme_enable("header", UL_COLOR_BOLD);
fdisk_info(sf->cxt, "%s:", devname);
{
fdisk_enable_listonly(sf->cxt, 1);
- if (fdisk_assign_device(sf->cxt, devname, 1)) {
- warn(_("cannot open %s"), devname);
- return 1;
- }
+ assign_device(sf, devname, 1);
fdisk_info(sf->cxt, "%s: %ju cylinders, %ju heads, %ju sectors/track",
devname,
/* --activate <device> */
listonly = argc == 1;
- rc = fdisk_assign_device(sf->cxt, devname, listonly);
- if (rc)
- err(EXIT_FAILURE, _("cannot open %s"), devname);
+ assign_device(sf, devname, listonly);
if (fdisk_is_label(sf->cxt, GPT)) {
if (fdisk_gpt_is_hybrid(sf->cxt))
} else if (!fdisk_is_label(sf->cxt, DOS))
errx(EXIT_FAILURE, _("toggle boot flags is supported for MBR or PMBR only"));
- if (!listonly && sf->backup)
- backup_partition_table(sf, devname);
-
nparts = fdisk_get_npartitions(sf->cxt);
for (i = 0; i < nparts; i++) {
char *data = NULL;
errx(EXIT_FAILURE, _("no disk device specified"));
devname = argv[0];
- if (fdisk_assign_device(sf->cxt, devname, 0) != 0)
- err(EXIT_FAILURE, _("cannot open %s"), devname);
-
- if (sf->backup)
- backup_partition_table(sf, devname);
+ assign_device(sf, devname, 0);
/* delete all */
if (argc == 1) {
if (!devname)
errx(EXIT_FAILURE, _("no disk device specified"));
- rc = fdisk_assign_device(sf->cxt, devname, 0); /* read-write */
- if (rc)
- err(EXIT_FAILURE, _("cannot open %s"), devname);
-
- if (sf->backup)
- backup_partition_table(sf, devname);
+ assign_device(sf, devname, 0); /* read-write */
if (fdisk_reorder_partitions(sf->cxt) == 1) /* unchanged */
rc = fdisk_deassign_device(sf->cxt, 1);
if (!devname)
errx(EXIT_FAILURE, _("no disk device specified"));
- rc = fdisk_assign_device(sf->cxt, devname, 1); /* read-only */
- if (rc)
- err(EXIT_FAILURE, _("cannot open %s"), devname);
+ assign_device(sf, devname, 1); /* read-only */
if (!fdisk_has_label(sf->cxt))
errx(EXIT_FAILURE, _("%s: does not contain a recognized partition table"), devname);
if (rc)
err(EXIT_FAILURE, _("cannot open %s"), devname);
+ if (!fdisk_is_readonly(sf->cxt)
+ && blkdev_lock(fdisk_get_devfd(sf->cxt), devname, sf->lockmode) != 0) {
+ fdisk_deassign_device(sf->cxt, 1);
+ return;
+ }
lb = fdisk_get_label(sf->cxt, NULL);
if (!lb)
errx(EXIT_FAILURE, _("%s: no partition table found"), devname);
else if (argc > 2)
errx(EXIT_FAILURE, _("unexpected arguments"));
- if (fdisk_assign_device(sf->cxt, devname, !str) != 0)
- err(EXIT_FAILURE, _("cannot open %s"), devname);
+ assign_device(sf, devname, !str);
/* print */
if (!str) {
return 0;
}
- /* change */
- if (sf->backup)
- backup_partition_table(sf, devname);
-
if (fdisk_set_disklabel_id_from_string(sf->cxt, str) != 0)
errx(EXIT_FAILURE, _("%s: failed to set disklabel ID"), devname);
else if (strcmp(oper, "gpt-bak-std") != 0)
errx(EXIT_FAILURE, _("unsupported relocation operation"));
- if (fdisk_assign_device(sf->cxt, devname, 0) != 0)
- err(EXIT_FAILURE, _("cannot open %s"), devname);
+ assign_device(sf, devname, 0);
fdisk_label_set_changed(lb, 1);
if (!devname)
errx(EXIT_FAILURE, _("no disk device specified"));
- rc = fdisk_assign_device(sf->cxt, devname, 0);
- if (rc)
- err(EXIT_FAILURE, _("cannot open %s"), devname);
+ assign_device(sf, devname, 0);
dp = fdisk_new_script(sf->cxt);
if (!dp)
if (fdisk_get_collision(sf->cxt))
follow_wipe_mode(sf);
- if (sf->backup)
- backup_partition_table(sf, devname);
-
if (!sf->quiet) {
list_disk_geometry(sf->cxt);
if (fdisk_has_label(sf->cxt)) {
_(" --color[=<when>] colorize output (%s, %s or %s)\n"), "auto", "always", "never");
fprintf(out,
" %s\n", USAGE_COLORS_DEFAULT);
+ fprintf(out,
+ _(" --lock[=<mode>] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock");
fputs(_(" -N, --partno <num> specify partition number\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);
OPT_DELETE,
OPT_NOTELL,
OPT_RELOCATE,
+ OPT_LOCK,
};
static const struct option longopts[] = {
{ "backup-file", required_argument, NULL, 'O' },
{ "bytes", no_argument, NULL, OPT_BYTES },
{ "color", optional_argument, NULL, OPT_COLOR },
+ { "lock", optional_argument, NULL, OPT_LOCK },
{ "delete", no_argument, NULL, OPT_DELETE },
{ "dump", no_argument, NULL, 'd' },
{ "help", no_argument, NULL, 'h' },
case OPT_RELOCATE:
sf->act = ACT_RELOCATE;
break;
+ case OPT_LOCK:
+ sf->lockmode = "1";
+ if (optarg) {
+ if (*optarg == '=')
+ optarg++;
+ sf->lockmode = optarg;
+ }
+ break;
default:
errtryhelp(EXIT_FAILURE);
}