]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - disk-utils/cfdisk.c
wipefs: add --lock and LOCK_BLOCK_DEVICE
[thirdparty/util-linux.git] / disk-utils / cfdisk.c
index e4470e9519e2d45237fb23ccc2025551c04c32ed..c783f9daa3154143303f6effb1d69d0603b55ead 100644 (file)
 # include <slang/slang.h>
 #endif
 
+#ifndef _XOPEN_SOURCE
+# define _XOPEN_SOURCE 500 /* for inclusion of get_wch */
+#endif
+
 #ifdef HAVE_SLCURSES_H
 # include <slcurses.h>
 #elif defined(HAVE_SLANG_SLCURSES_H)
@@ -60,6 +64,7 @@
 #include "colors.h"
 #include "debug.h"
 #include "list.h"
+#include "blkdev.h"
 
 static const char *default_disks[] = {
 #ifdef __GNU__
@@ -134,6 +139,7 @@ static struct cfdisk_menu *menu_push(struct cfdisk *cf, struct cfdisk_menuitem *
 static struct cfdisk_menu *menu_pop(struct cfdisk *cf);
 static void menu_refresh_size(struct cfdisk *cf);
 
+static int ui_end(void);
 static int ui_refresh(struct cfdisk *cf);
 static void ui_warnx(const char *fmt, ...);
 static void ui_warn(const char *fmt, ...);
@@ -142,16 +148,17 @@ static void ui_draw_menu(struct cfdisk *cf);
 static int ui_menu_move(struct cfdisk *cf, int key);
 static void ui_menu_resize(struct cfdisk *cf);
 
-static int ui_get_size(struct cfdisk *cf, const char *prompt, uintmax_t *res,
-                      uintmax_t low, uintmax_t up, int *expsize);
+static int ui_get_size(struct cfdisk *cf, const char *prompt, uint64_t *res,
+                      uint64_t low, uint64_t up, int *expsize);
 
 static int ui_enabled;
-static int ui_resize;
+static volatile sig_atomic_t sig_resize;
+static volatile sig_atomic_t sig_die;
 
 /* ncurses LINES and COLS may be actual variables or *macros*, but we need
  * something portable and writable */
-size_t ui_lines;
-size_t ui_cols;
+static size_t ui_lines;
+static size_t ui_cols;
 
 /* menu item */
 struct cfdisk_menuitem {
@@ -183,6 +190,7 @@ struct cfdisk_menu {
 static struct cfdisk_menuitem main_menuitems[] = {
        { 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
        { 'd', N_("Delete"), N_("Delete the current partition") },
+       { 'r', N_("Resize"), N_("Reduce or enlarge the current partition") },
        { 'n', N_("New"), N_("Create new partition from free space") },
        { 'q', N_("Quit"), N_("Quit program without writing changes") },
        { 't', N_("Type"), N_("Change the partition type") },
@@ -212,6 +220,7 @@ struct cfdisk_line {
 struct cfdisk {
        struct fdisk_context    *cxt;   /* libfdisk context */
        struct fdisk_table      *table; /* partition table */
+       struct fdisk_table      *original_layout; /* original on-disk PT */
 
        struct cfdisk_menu      *menu;  /* the current menu */
 
@@ -238,6 +247,7 @@ struct cfdisk {
 #endif
        unsigned int    wrong_order :1,         /* PT not in right order */
                        zero_start :1,          /* ignore existing partition table */
+                       device_is_used : 1,     /* don't use re-read ioctl */
                        show_extra :1;          /* show extra partinfo */
 };
 
@@ -245,7 +255,7 @@ struct cfdisk {
 /*
  * let's use include/debug.h stuff for cfdisk too
  */
-UL_DEBUG_DEFINE_MASK(cfdisk);
+static UL_DEBUG_DEFINE_MASK(cfdisk);
 UL_DEBUG_DEFINE_MASKNAMES(cfdisk) = UL_DEBUG_EMPTY_MASKNAMES;
 
 #define CFDISK_DEBUG_INIT      (1 << 1)
@@ -259,7 +269,7 @@ UL_DEBUG_DEFINE_MASKNAMES(cfdisk) = UL_DEBUG_EMPTY_MASKNAMES;
 
 static void cfdisk_init_debug(void)
 {
-       __UL_INIT_DEBUG(cfdisk, CFDISK_DEBUG_, 0, CFDISK_DEBUG);
+       __UL_INIT_DEBUG_FROM_ENV(cfdisk, CFDISK_DEBUG_, 0, CFDISK_DEBUG);
 }
 
 /* Initialize output columns -- we follow libfdisk fields (usually specific
@@ -276,6 +286,13 @@ static int cols_init(struct cfdisk *cf)
        return fdisk_label_get_fields_ids(NULL, cf->cxt, &cf->fields, &cf->nfields);
 }
 
+static void die_on_signal(void)
+{
+       DBG(MISC, ul_debug("die on signal."));
+       ui_end();
+       exit(EXIT_FAILURE);
+}
+
 static void resize(void)
 {
        struct winsize ws;
@@ -293,7 +310,7 @@ static void resize(void)
 
        DBG(UI, ul_debug("ui: resize refresh ui_cols=%zu, ui_lines=%zu",
                                ui_cols, ui_lines));
-       ui_resize = 0;
+       sig_resize = 0;
 }
 
 /* Reads partition in tree-like order from scols
@@ -323,7 +340,7 @@ static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb)
 {
        struct fdisk_partition *pa;
        struct fdisk_label *lb;
-       struct fdisk_iter *itr = NULL;
+       struct fdisk_iter *itr;
        struct libscols_table *table = NULL;
        struct libscols_iter *s_itr = NULL;
        char *res = NULL;
@@ -420,9 +437,7 @@ static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb)
         * parno stored within struct fdisk_partition)  */
 
        /* remove all */
-       fdisk_reset_iter(itr, FDISK_ITER_FORWARD);
-       while (fdisk_table_next_partition(tb, itr, &pa) == 0)
-               fdisk_table_remove_partition(tb, pa);
+       fdisk_reset_table(tb);
 
        s_itr = scols_new_iter(SCOLS_ITER_FORWARD);
        if (!s_itr)
@@ -572,10 +587,12 @@ static int ask_menu(struct fdisk_ask *ask, struct cfdisk *cf)
        refresh();
 
        /* wait for keys */
-       do {
+       while (!sig_die) {
                key = getch();
 
-               if (ui_resize)
+               if (sig_die)
+                       break;
+               if (sig_resize)
                        ui_menu_resize(cf);
                if (ui_menu_move(cf, key) == 0)
                        continue;
@@ -591,7 +608,10 @@ static int ask_menu(struct fdisk_ask *ask, struct cfdisk *cf)
                        free(cm);
                        return 0;
                }
-       } while (1);
+       }
+
+       if (sig_die)
+               die_on_signal();
 
        menu_pop(cf);
        free(cm);
@@ -781,17 +801,15 @@ static void ui_clean_hint(void)
        clrtoeol();
 }
 
-static void die_on_signal(int dummy __attribute__((__unused__)))
+
+static void sig_handler_die(int dummy __attribute__((__unused__)))
 {
-       DBG(MISC, ul_debug("die on signal."));
-       ui_end();
-       exit(EXIT_FAILURE);
+       sig_die = 1;
 }
 
-static void resize_on_signal(int dummy __attribute__((__unused__)))
+static void sig_handler_resize(int dummy __attribute__((__unused__)))
 {
-       DBG(MISC, ul_debug("resize on signal."));
-       ui_resize = 1;
+       sig_resize = 1;
 }
 
 static void menu_refresh_size(struct cfdisk *cf)
@@ -919,11 +937,11 @@ static int ui_init(struct cfdisk *cf __attribute__((__unused__)))
        /* setup SIGCHLD handler */
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = 0;
-       sa.sa_handler = die_on_signal;
+       sa.sa_handler = sig_handler_die;
        sigaction(SIGINT, &sa, NULL);
        sigaction(SIGTERM, &sa, NULL);
 
-       sa.sa_handler = resize_on_signal;
+       sa.sa_handler = sig_handler_resize;
        sigaction(SIGWINCH, &sa, NULL);
 
        ui_enabled = 1;
@@ -969,7 +987,9 @@ static size_t menuitem_get_line(struct cfdisk *cf, size_t idx)
                if (!m->page_sz)                                /* small menu */
                        return (ui_lines - (cf->menu->nitems + 1)) / 2 + idx;
                return (idx % m->page_sz) + 1;
-       } else {
+       }
+
+       {
                size_t len = MENU_H_ITEMWIDTH(m) + MENU_H_BETWEEN; /** item width */
                size_t items = ui_cols / len;                   /* items per line */
 
@@ -986,7 +1006,9 @@ static int menuitem_get_column(struct cfdisk *cf, size_t idx)
                if ((size_t) ui_cols <= nc)
                        return 0;
                return (ui_cols - nc) / 2;
-       } else {
+       }
+
+       {
                size_t len = MENU_H_ITEMWIDTH(cf->menu) + MENU_H_BETWEEN; /* item width */
                size_t items = ui_cols / len;                           /* items per line */
                size_t extra = items < cf->menu->nitems ?               /* extra space on line */
@@ -1049,9 +1071,10 @@ static void ui_draw_menuitem(struct cfdisk *cf,
                             struct cfdisk_menuitem *d,
                             size_t idx)
 {
-       char buf[80 * MB_CUR_MAX], *ptr = buf;
+       char *buf, *ptr;
        const char *name;
        size_t width;
+       const size_t buf_sz = 80 * MB_CUR_MAX;
        int ln, cl, vert = cf->menu->vertical;
 
        if (!menuitem_on_page(cf, idx))
@@ -1059,6 +1082,7 @@ static void ui_draw_menuitem(struct cfdisk *cf,
        ln = menuitem_get_line(cf, idx);
        cl = menuitem_get_column(cf, idx);
 
+       ptr = buf = xmalloc(buf_sz);
        /* string width */
        if (vert) {
                width = cf->menu->width + MENU_V_SPADDING;
@@ -1068,7 +1092,7 @@ static void ui_draw_menuitem(struct cfdisk *cf,
                width = MENU_H_SPADDING + cf->menu->width + MENU_H_SPADDING;
 
        name = _(d->name);
-       mbsalign(name, ptr, sizeof(buf), &width,
+       mbsalign(name, ptr, buf_sz, &width,
                        vert ? MBS_ALIGN_LEFT : MBS_ALIGN_CENTER,
                        0);
 
@@ -1087,6 +1111,7 @@ static void ui_draw_menuitem(struct cfdisk *cf,
                mvprintw(ln, cl, "%s", buf);
        else
                mvprintw(ln, cl, "%s%s%s", MENU_H_PRESTR, buf, MENU_H_POSTSTR);
+       free(buf);
 
        if (cf->menu->idx == idx) {
                standend();
@@ -1246,7 +1271,10 @@ static char *get_mountpoint(struct cfdisk *cf, const char *tagname, const char *
                        cf->fstab = mnt_new_table();
                        if (cf->fstab) {
                                mnt_table_set_cache(cf->fstab, cf->mntcache);
-                               mnt_table_parse_fstab(cf->fstab, NULL);
+                               if (mnt_table_parse_fstab(cf->fstab, NULL) != 0) {
+                                       mnt_unref_table(cf->fstab);
+                                       cf->fstab = NULL;
+                               }
                        }
                }
                if (cf->fstab)
@@ -1468,7 +1496,7 @@ static int ui_menu_move(struct cfdisk *cf, int key)
        assert(cf);
        assert(cf->menu);
 
-       if (key == ERR)
+       if (key == (int) ERR)
                return 0;       /* ignore errors */
 
        m = cf->menu;
@@ -1527,14 +1555,14 @@ static int ui_menu_move(struct cfdisk *cf, int key)
                return 0;
        }
 
-       DBG(MENU, ul_debug(" no memu move key"));
+       DBG(MENU, ul_debug(" no menu move key"));
        return 1;
 }
 
 /* but don't call me from ui_run(), this is for pop-up menus only */
 static void ui_menu_resize(struct cfdisk *cf)
 {
-       DBG(MENU, ul_debug("memu resize/refresh"));
+       DBG(MENU, ul_debug("menu resize/refresh"));
        resize();
        ui_clean_menu(cf);
        menu_refresh_size(cf);
@@ -1677,7 +1705,8 @@ static int ui_refresh(struct cfdisk *cf)
        if (!ui_enabled)
                return -EINVAL;
 
-       strsz = size_to_human_string(SIZE_SUFFIX_SPACE
+       strsz = size_to_human_string(SIZE_DECIMAL_2DIGITS
+                               | SIZE_SUFFIX_SPACE
                                | SIZE_SUFFIX_3LETTER, bytes);
 
        lb = fdisk_get_label(cf->cxt, NULL);
@@ -1689,7 +1718,7 @@ static int ui_refresh(struct cfdisk *cf)
        attron(A_BOLD);
        ui_center(0, _("Disk: %s"), fdisk_get_devname(cf->cxt));
        attroff(A_BOLD);
-       ui_center(1, _("Size: %s, %ju bytes, %ju sectors"),
+       ui_center(1, _("Size: %s, %"PRIu64" bytes, %ju sectors"),
                        strsz, bytes, (uintmax_t) fdisk_get_nsectors(cf->cxt));
        if (fdisk_get_disklabel_id(cf->cxt, &id) == 0 && id)
                ui_center(2, _("Label: %s, identifier: %s"),
@@ -1697,6 +1726,7 @@ static int ui_refresh(struct cfdisk *cf)
        else
                ui_center(2, _("Label: %s"), fdisk_label_get_name(lb));
        free(strsz);
+       free(id);
 
        ui_draw_table(cf);
        ui_draw_menu(cf);
@@ -1723,7 +1753,7 @@ static ssize_t ui_get_string(const char *prompt,
        clrtoeol();
 
        if (prompt) {
-               mvaddstr(ln, cl, (char *) prompt);
+               mvaddstr(ln, cl, prompt);
                cl += mbs_safe_width(prompt);
        }
 
@@ -1740,7 +1770,7 @@ static ssize_t ui_get_string(const char *prompt,
 
        curs_set(1);
 
-       while (1) {
+       while (!sig_die) {
                wint_t c;       /* we have fallback in widechar.h */
 
                move(ln, cl);
@@ -1753,9 +1783,11 @@ static ssize_t ui_get_string(const char *prompt,
     defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
                if (get_wch(&c) == ERR) {
 #else
-               if ((c = getch()) == ERR) {
+               if ((c = getch()) == (wint_t) ERR) {
 #endif
-                       if (ui_resize) {
+                       if (sig_die)
+                               break;
+                       if (sig_resize) {
                                resize();
                                continue;
                        }
@@ -1795,6 +1827,7 @@ static ssize_t ui_get_string(const char *prompt,
                        rc = mbs_edit_delete(edit);
                        break;
                case '\b':
+               case KEY_DELETE:
                case KEY_BACKSPACE:
                        rc = mbs_edit_backspace(edit);
                        break;
@@ -1806,6 +1839,9 @@ static ssize_t ui_get_string(const char *prompt,
                        beep();
        }
 
+       if (sig_die)
+               die_on_signal();
+
        rc = strlen(edit->buf);         /* success */
 done:
        move(ln, 0);
@@ -1817,16 +1853,19 @@ done:
        return rc;
 }
 
-/* @res is default value as well as result in bytes */
-static int ui_get_size(struct cfdisk *cf, const char *prompt, uintmax_t *res,
-                      uintmax_t low, uintmax_t up, int *expsize)
+static int ui_get_size(struct cfdisk *cf,      /* context */
+                      const char *prompt,      /* UI dialog string */
+                      uint64_t *res,           /* result in bytes */
+                      uint64_t low,            /* minimal size */
+                      uint64_t up,             /* maximal size */
+                      int *expsize)            /* explicitly specified size */
 {
        char buf[128];
-       uintmax_t user = 0;
+       uint64_t user = 0;
        ssize_t rc;
        char *dflt = size_to_human_string(0, *res);
 
-       DBG(UI, ul_debug("get_size (default=%ju)", *res));
+       DBG(UI, ul_debug("get_size (default=%"PRIu64")", *res));
 
        ui_clean_info();
 
@@ -1844,7 +1883,7 @@ static int ui_get_size(struct cfdisk *cf, const char *prompt, uintmax_t *res,
                if (rc == 0) {
                        ui_warnx(_("Please, specify size."));
                        continue;                       /* nothing specified */
-               } else if (rc == -CFDISK_ERR_ESC)
+               } if (rc == -CFDISK_ERR_ESC)
                        break;                          /* cancel dialog */
 
                if (strcmp(buf, dflt) == 0)
@@ -1855,16 +1894,16 @@ static int ui_get_size(struct cfdisk *cf, const char *prompt, uintmax_t *res,
                                insec = 1;
                                buf[len - 1] = '\0';
                        }
-                       rc = parse_size(buf, &user, &pwr);      /* parse */
+                       rc = parse_size(buf, (uintmax_t *)&user, &pwr); /* parse */
                }
 
                if (rc == 0) {
-                       DBG(UI, ul_debug("get_size user=%ju, power=%d, sectors=%s",
+                       DBG(UI, ul_debug("get_size user=%"PRIu64", power=%d, in-sectors=%s",
                                                user, pwr, insec ? "yes" : "no"));
                        if (insec)
                                user *= fdisk_get_sector_size(cf->cxt);
                        if (user < low) {
-                               ui_warnx(_("Minimum size is %ju bytes."), low);
+                               ui_warnx(_("Minimum size is %"PRIu64" bytes."), low);
                                rc = -ERANGE;
                        }
                        if (user > up && pwr && user < up + (1ULL << pwr * 10))
@@ -1873,7 +1912,7 @@ static int ui_get_size(struct cfdisk *cf, const char *prompt, uintmax_t *res,
                                user = up;
 
                        if (user > up) {
-                               ui_warnx(_("Maximum size is %ju bytes."), up);
+                               ui_warnx(_("Maximum size is %"PRIu64" bytes."), up);
                                rc = -ERANGE;
                        }
                        if (rc == 0 && insec && expsize)
@@ -1887,7 +1926,7 @@ static int ui_get_size(struct cfdisk *cf, const char *prompt, uintmax_t *res,
                *res = user;
        free(dflt);
 
-       DBG(UI, ul_debug("get_size (result=%ju, rc=%zd)", *res, rc));
+       DBG(UI, ul_debug("get_size (result=%"PRIu64", rc=%zd)", *res, rc));
        return rc;
 }
 
@@ -1942,10 +1981,12 @@ static struct fdisk_parttype *ui_get_parttype(struct cfdisk *cf,
        ui_draw_menu(cf);
        refresh();
 
-       do {
+       while (!sig_die) {
                int key = getch();
 
-               if (ui_resize)
+               if (sig_die)
+                       break;
+               if (sig_resize)
                        ui_menu_resize(cf);
                if (ui_menu_move(cf, key) == 0)
                        continue;
@@ -1963,8 +2004,10 @@ static struct fdisk_parttype *ui_get_parttype(struct cfdisk *cf,
                case 'Q':
                        goto done;
                }
-       } while (1);
+       }
 
+       if (sig_die)
+               die_on_signal();
 done:
        menu_pop(cf);
        if (codetypes) {
@@ -2088,7 +2131,9 @@ static int ui_create_label(struct cfdisk *cf)
                ui_info(_("Device does not contain a recognized partition table."));
 
 
-       do {
+       while (!sig_die) {
+               int key;
+
                if (refresh_menu) {
                        ui_draw_menu(cf);
                        ui_hint(_("Select a type to create a new label or press 'L' to load script file."));
@@ -2096,9 +2141,11 @@ static int ui_create_label(struct cfdisk *cf)
                        refresh_menu = 0;
                }
 
-               int key = getch();
+               key = getch();
 
-               if (ui_resize)
+               if (sig_die)
+                       break;
+               if (sig_resize)
                        ui_menu_resize(cf);
                if (ui_menu_move(cf, key) == 0)
                        continue;
@@ -2122,8 +2169,10 @@ static int ui_create_label(struct cfdisk *cf)
                        refresh_menu = 1;
                        break;
                }
-       } while (1);
+       }
 
+       if (sig_die)
+               die_on_signal();
 done:
        menu_pop(cf);
        free(cm);
@@ -2174,6 +2223,9 @@ static int ui_help(void)
        ui_info(_("Press a key to continue."));
 
        getch();
+
+       if (sig_die)
+               die_on_signal();
        return 0;
 }
 
@@ -2190,6 +2242,7 @@ static int main_menu_ignore_keys(struct cfdisk *cf, char *ignore,
                ignore[i++] = 'd';      /* delete */
                ignore[i++] = 't';      /* set type */
                ignore[i++] = 'b';      /* set bootable */
+               ignore[i++] = 'r';      /* resize */
                cf->menu->prefkey = 'n';
        } else {
                cf->menu->prefkey = 'q';
@@ -2275,7 +2328,7 @@ static int main_menu_action(struct cfdisk *cf, int key)
                break;
        case 'n': /* New */
        {
-               uint64_t start, size, dflt_size, secs;
+               uint64_t start, size, dflt_size, secs, max_size;
                struct fdisk_partition *npa;    /* the new partition */
                int expsize = 0;                /* size specified explicitly in sectors */
 
@@ -2284,11 +2337,11 @@ static int main_menu_action(struct cfdisk *cf, int key)
 
                /* free space range */
                start = fdisk_partition_get_start(pa);
-               size = dflt_size = fdisk_partition_get_size(pa) * fdisk_get_sector_size(cf->cxt);
+               size = max_size = dflt_size = fdisk_partition_get_size(pa) * fdisk_get_sector_size(cf->cxt);
 
                if (ui_get_size(cf, _("Partition size: "), &size,
                                fdisk_get_sector_size(cf->cxt),
-                               size, &expsize) == -CFDISK_ERR_ESC)
+                               max_size, &expsize) == -CFDISK_ERR_ESC)
                        break;
 
                secs = size / fdisk_get_sector_size(cf->cxt);
@@ -2332,6 +2385,43 @@ static int main_menu_action(struct cfdisk *cf, int key)
                        info = _("The type of partition %zu is unchanged.");
                break;
        }
+       case 'r': /* resize */
+       {
+               struct fdisk_partition *npa, *next;
+               uint64_t size, max_size, secs;
+
+               if (fdisk_partition_is_freespace(pa) || !fdisk_partition_has_start(pa))
+                       return -EINVAL;
+
+               size = fdisk_partition_get_size(pa);
+
+               /* is the next freespace? */
+               next = fdisk_table_get_partition(cf->table, cf->lines_idx + 1);
+               if (next && fdisk_partition_is_freespace(next))
+                       size += fdisk_partition_get_size(next);
+
+               size *= fdisk_get_sector_size(cf->cxt);
+               max_size = size;
+
+               if (ui_get_size(cf, _("New size: "), &size,
+                               fdisk_get_sector_size(cf->cxt),
+                               max_size, NULL) == -CFDISK_ERR_ESC)
+                       break;
+               secs = size / fdisk_get_sector_size(cf->cxt);
+               npa = fdisk_new_partition();
+               if (!npa)
+                       return -ENOMEM;
+
+               fdisk_partition_set_size(npa, secs);
+
+               rc = fdisk_set_partition(cf->cxt, n, npa);
+               fdisk_unref_partition(npa);
+               if (rc == 0) {
+                       ref = 1;
+                       info = _("Partition %zu resized.");
+               }
+               break;
+       }
        case 's': /* Sort */
                if (cf->wrong_order) {
                        fdisk_reorder_partitions(cf->cxt);
@@ -2366,7 +2456,10 @@ static int main_menu_action(struct cfdisk *cf, int key)
                if (rc)
                        warn = _("Failed to write disklabel.");
                else {
-                       fdisk_reread_partition_table(cf->cxt);
+                       if (cf->device_is_used)
+                               fdisk_reread_changes(cf->cxt, cf->original_layout);
+                       else
+                               fdisk_reread_partition_table(cf->cxt);
                        info = _("The partition table has been altered.");
                }
                cf->nwrites++;
@@ -2463,11 +2556,14 @@ static int ui_run(struct cfdisk *cf)
        else if (cf->wrong_order)
                ui_info(_("Note that partition table entries are not in disk order now."));
 
-       do {
+       while (!sig_die) {
                int key = getch();
 
                rc = 0;
-               if (ui_resize)
+
+               if (sig_die)
+                       break;
+               if (sig_resize)
                        /* Note that ncurses getch() returns ERR when interrupted
                         * by signal, but SLang does not interrupt at all. */
                        ui_resize_refresh(cf);
@@ -2498,6 +2594,7 @@ static int ui_run(struct cfdisk *cf)
                                ui_table_goto(cf, (int) cf->lines_idx - cf->page_sz);
                                break;
                        }
+                       /* fallthrough */
                case KEY_HOME:
                        ui_table_goto(cf, 0);
                        break;
@@ -2506,6 +2603,7 @@ static int ui_run(struct cfdisk *cf)
                                ui_table_goto(cf, cf->lines_idx + cf->page_sz);
                                break;
                        }
+                       /* fallthrough */
                case KEY_END:
                        ui_table_goto(cf, (int) cf->nlines - 1);
                        break;
@@ -2527,7 +2625,7 @@ static int ui_run(struct cfdisk *cf)
 
                if (rc == 1)
                        break; /* quit */
-       } while (1);
+       }
 
        menu_pop(cf);
 
@@ -2535,8 +2633,9 @@ static int ui_run(struct cfdisk *cf)
        return 0;
 }
 
-static void __attribute__ ((__noreturn__)) usage(FILE *out)
+static void __attribute__((__noreturn__)) usage(void)
 {
+       FILE *out = stdout;
        fputs(USAGE_HEADER, out);
        fprintf(out,
              _(" %1$s [options] <disk>\n"), program_invocation_short_name);
@@ -2545,43 +2644,48 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
        fputs(_("Display or manipulate a disk partition table.\n"), out);
 
        fputs(USAGE_OPTIONS, out);
-       fputs(_(" -L, --color[=<when>]     colorize output (auto, always or never)\n"), out);
+       fprintf(out,
+             _(" -L, --color[=<when>]     colorize output (%s, %s or %s)\n"), "auto", "always", "never");
        fprintf(out,
                "                            %s\n", USAGE_COLORS_DEFAULT);
        fputs(_(" -z, --zero               start with zeroed partition table\n"), out);
+       fprintf(out,
+             _("     --lock[=<mode>]      use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock");
 
        fputs(USAGE_SEPARATOR, out);
-       fputs(USAGE_HELP, out);
-       fputs(USAGE_VERSION, out);
+       printf(USAGE_HELP_OPTIONS(26));
 
-       fprintf(out, USAGE_MAN_TAIL("cfdisk(8)"));
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       printf(USAGE_MAN_TAIL("cfdisk(8)"));
+       exit(EXIT_SUCCESS);
 }
 
 int main(int argc, char *argv[])
 {
-       const char *diskpath = NULL;
+       const char *diskpath = NULL, *lockmode = NULL;
        int rc, c, colormode = UL_COLORMODE_UNDEF;
        struct cfdisk _cf = { .lines_idx = 0 },
                      *cf = &_cf;
-
+       enum {
+               OPT_LOCK        = CHAR_MAX + 1
+       };
        static const struct option longopts[] = {
                { "color",   optional_argument, NULL, 'L' },
+               { "lock",    optional_argument, NULL, OPT_LOCK },
                { "help",    no_argument,       NULL, 'h' },
                { "version", no_argument,       NULL, 'V' },
                { "zero",    no_argument,       NULL, 'z' },
-               { NULL, 0, 0, 0 },
+               { NULL, 0, NULL, 0 },
        };
 
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        while((c = getopt_long(argc, argv, "L::hVz", longopts, NULL)) != -1) {
                switch(c) {
                case 'h':
-                       usage(stdout);
+                       usage();
                        break;
                case 'L':
                        colormode = UL_COLORMODE_AUTO;
@@ -2590,11 +2694,18 @@ int main(int argc, char *argv[])
                                                _("unsupported color mode"));
                        break;
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
+                       print_version(EXIT_SUCCESS);
                case 'z':
                        cf->zero_start = 1;
                        break;
+               case OPT_LOCK:
+                       lockmode = "1";
+                       if (optarg) {
+                               if (*optarg == '=')
+                                       optarg++;
+                               lockmode = optarg;
+                       }
+                       break;
                default:
                        errtryhelp(EXIT_FAILURE);
                }
@@ -2631,6 +2742,14 @@ int main(int argc, char *argv[])
        if (rc != 0)
                err(EXIT_FAILURE, _("cannot open %s"), diskpath);
 
+       if (!fdisk_is_readonly(cf->cxt)) {
+               if (blkdev_lock(fdisk_get_devfd(cf->cxt), diskpath, lockmode) != 0)
+                       return EXIT_FAILURE;
+
+               cf->device_is_used = fdisk_device_is_used(cf->cxt);
+               fdisk_get_partitions(cf->cxt, &cf->original_layout);
+       }
+
        /* Don't use err(), warn() from this point */
        ui_init(cf);
        ui_run(cf);
@@ -2638,6 +2757,7 @@ int main(int argc, char *argv[])
 
        cfdisk_free_lines(cf);
        free(cf->linesbuf);
+       free(cf->fields);
 
        fdisk_unref_table(cf->table);
 #ifdef HAVE_LIBMOUNT