sfdisk: incremental kernel updates for -N and --append
cover sfdisk: incremental kernel updates for -N and --append
Hi Karel,
This implements #4634: -N and --append update only changed kernel partitions. Two preparatory libfdisk fixes cover error returns and DOS extended-partition sizes.
In an isolated x86_64 VM, all 111 sfdisk/libfdisk subtests passed. The new regression fails on the baseline and passes with GCC and Clang ASan/UBSan. I also checked 4096-byte sectors and appending alongside an active swap partition.
Thanks,
Tianyi
Tianyi Chen (3):
libfdisk: propagate errors from fdisk_reread_changes()
libfdisk: limit resized DOS extended partitions to two sectors
sfdisk: update only changed partitions with -N and --append
44 files changed, 146 insertions(+), 45 deletions(-) create mode 100644 tests/expected/sfdisk/reread-dos create mode 100644 tests/expected/sfdisk/reread-gpt create mode 100755 tests/ts/sfdisk/reread
Signature · 1 line
2.55.0
PATCH 1/3 libfdisk: propagate errors from fdisk_reread_changes()
Failed BLKPG operations emit warnings but leave the return value at zero, so callers cannot detect that the kernel partition layout was not fully updated. Return -EIO if any operation fails, while still attempting the remaining updates.
Also stop if reading the current partition layout fails instead of proceeding with an incomplete table.
--- libfdisk/src/context.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
libfdisk/src/context.c +6 -2
@@ -985,7 +985,9 @@ int fdisk_reread_changes(struct fdisk_context *cxt, struct fdisk_table *org)fdisk_reset_iter (&itr,FDISK_ITER_FORWARD );/* the current layout */ -fdisk_get_partitions (cxt, &tb); + rc =fdisk_get_partitions (cxt, &tb); +if (rc) + goto done;/* maximal number of partitions */ nparts =max (fdisk_table_get_nents (tb),fdisk_table_get_nents (org));
@@ -1052,10 +1054,12 @@ int fdisk_reread_changes(struct fdisk_context *cxt, struct fdisk_table *org) err++; } } -if (err) +if (err) {fdisk_info (cxt,_ ("The kernel still uses the old partitions. The new " "table will be used at the next reboot. " )); + rc = -EIO ; + } done:free (rem);free (add);
Signature · 1 line
2.55.0
PATCH 2/3 libfdisk: limit resized DOS extended partitions to two sectors
In reply to Tianyi ChenThe kernel represents a DOS extended partition with at most two 512-byte sectors. Passing its full on-disk size to BLKPG_RESIZE_PARTITION can therefore overlap its logical partitions and fail with EBUSY.
Apply the same size limit when resizing as when adding an extended partition, keeping its kernel representation consistent.
--- libfdisk/src/context.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
libfdisk/src/context.c +7 -1
@@ -1026,10 +1026,16 @@ int fdisk_reread_changes(struct fdisk_context *cxt, struct fdisk_table *org) } }for (i =0 ; i < nupds; i++) { +uint64_t sz; + pa = upd[i]; + sz = pa->size * ssf; +if (fdisk_is_label (cxt,DOS ) &&fdisk_partition_is_container (pa)) + sz =min (sz, (uint64_t )2 ); +DBG_OBJ (PART , pa,ul_debug ("#%zu calling BLKPG_RESIZE_PARTITION" , pa->partno));if (partx_resize_partition (cxt->dev_fd, pa->partno +1 , - pa->start * ssf, pa->size * ssf) !=0 ) { + pa->start * ssf, sz) !=0 ) {fdisk_warn (cxt,_ ("Failed to update system information about partition %zu" ), pa->partno +1 ); err++; }
Signature · 1 line
2.55.0
PATCH 3/3 sfdisk: update only changed partitions with -N and --append
In reply to Tianyi ChenBLKRRPART fails when an unchanged partition is open, leaving the kernel layout stale after an otherwise successful -N or --append operation.
Save the original layout and use fdisk_reread_changes() for these edits on block devices. Keep the full reread path for table replacement and nested labels, and preserve --no-act and --no-tell-kernel. Report incremental update failures without skipping device synchronization and close.
Add DOS and GPT loop-device tests that keep an unchanged partition open, check kernel geometry after additions, resizing and moves, and cover suppressed updates and a failed move of the open partition. Adjust existing expected output for the absence of the full-reread message.
--- disk-utils/sfdisk.8.adoc | 4 +- disk-utils/sfdisk.c | 20 ++++- .../expected/sfdisk/dos-reduce-empty-extended | 1 - tests/expected/sfdisk/dos-resize | 1 - .../expected/sfdisk/dos-resize-empty-extended | 1 - tests/expected/sfdisk/dos-resize-extended | 1 - tests/expected/sfdisk/dos-resize-logical | 1 - tests/expected/sfdisk/gpt-resize | 1 - tests/expected/sfdisk/movedata-down | 1 - tests/expected/sfdisk/movedata-non-overlay | 1 - tests/expected/sfdisk/movedata-up | 1 - tests/expected/sfdisk/reread-dos | 19 +++++ tests/expected/sfdisk/reread-gpt | 19 +++++ .../sfdisk/resize-dos-0-move-up-absolute | 1 - .../sfdisk/resize-dos-1-move-down-absolute | 1 - .../sfdisk/resize-dos-10-down-enlarge | 1 - .../sfdisk/resize-dos-11-absolute-move-resize | 1 - .../sfdisk/resize-dos-12-reduce-midle | 1 - tests/expected/sfdisk/resize-dos-13-max-last | 1 - .../sfdisk/resize-dos-2-move-up-relative | 1 - .../sfdisk/resize-dos-3-move-down-relative | 1 - .../sfdisk/resize-dos-4-enlarge-absolute | 1 - .../sfdisk/resize-dos-5-reduce-absolute | 1 - .../sfdisk/resize-dos-6-enlarge-relative | 1 - .../sfdisk/resize-dos-7-reduce-relative | 1 - .../expected/sfdisk/resize-dos-8-enlarge-all | 1 - tests/expected/sfdisk/resize-dos-9-up-preduce | 1 - tests/expected/sfdisk/resize-dos-unsorted | 1 - .../sfdisk/resize-gpt-0-move-up-absolute | 1 - .../sfdisk/resize-gpt-1-move-down-absolute | 1 - .../sfdisk/resize-gpt-10-down-enlarge | 1 - .../sfdisk/resize-gpt-11-absolute-move-resize | 1 - .../sfdisk/resize-gpt-12-reduce-midle | 1 - tests/expected/sfdisk/resize-gpt-13-max-last | 1 - .../sfdisk/resize-gpt-2-move-up-relative | 1 - .../sfdisk/resize-gpt-3-move-down-relative | 1 - .../sfdisk/resize-gpt-4-enlarge-absolute | 1 - .../sfdisk/resize-gpt-5-reduce-absolute | 1 - .../sfdisk/resize-gpt-6-enlarge-relative | 1 - .../sfdisk/resize-gpt-7-reduce-relative | 1 - .../expected/sfdisk/resize-gpt-8-enlarge-all | 1 - tests/expected/sfdisk/resize-gpt-9-up-preduce | 1 - tests/ts/sfdisk/reread | 75 +++++++++++++++++++ 43 files changed, 133 insertions(+), 42 deletions(-) create mode 100644 tests/expected/sfdisk/reread-dos create mode 100644 tests/expected/sfdisk/reread-gpt create mode 100755 tests/ts/sfdisk/reread
disk-utils/sfdisk.8.adoc +3 -1
@@ -188,7 +188,9 @@ Do not print device names in *--dump* output. Do not check through the re-read-partition-table ioctl whether the device is in use. *--no-tell-kernel*:: -Don't tell the kernel about partition changes. This option is recommended together with *--no-reread* to modify a partition on used disk. The modified partition should not be used (e.g., mounted). +Don't tell the kernel about partition changes. ++ +Without this option, *-N* and *--append* update only changed partitions of an existing, non-nested partition table. Unchanged partitions may remain in use; use *--no-reread* to suppress the initial in-use check. The modified partition should not be used (e.g., mounted). *-O*, *--backup-file* _path_:: Override the default backup filename. Note that the device name and offset are always appended to the filename.
disk-utils/sfdisk.c +17 -3
@@ -107,6 +107,7 @@ struct sfdisk {struct fdisk_context *cxt;/* libfdisk context */ struct fdisk_partition *orig_pa;/* -N <partno> before the change */ +struct fdisk_table *original_layout; unsigned int verify : 1, /* call fdisk_verify_disklabel() */ quiet : 1, /* suppress extra messages */
@@ -261,6 +262,7 @@ static int sfdisk_deinit(struct sfdisk *sf) }fdisk_unref_context (sf->cxt); +fdisk_unref_table (sf->original_layout);free (sf->prompt);memset (sf,0 ,sizeof (*sf));
@@ -651,7 +653,7 @@ done:static int write_changes (struct sfdisk *sf) { -int rc =0 ; +int rc =0 , reread_rc =0 ;if (sf->noact)fdisk_info (sf->cxt,_ ("The partition table is unchanged (--no-act)." ));
@@ -670,14 +672,17 @@ static int write_changes(struct sfdisk *sf)* related to the write to the device. */ xusleep (250000 ); -fdisk_reread_partition_table (sf->cxt); +if (sf->original_layout) + reread_rc =fdisk_reread_changes (sf->cxt, sf->original_layout); +else +fdisk_reread_partition_table (sf->cxt); } }if (!rc) rc =fdisk_deassign_device (sf->cxt, sf->noact || sf->notell);/* no-sync */ -return rc; +return rc ? rc : reread_rc; }/*
@@ -1939,6 +1944,15 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv)fputs (_ (" OK\n\n" ), stdout); } +/* Nested labels do not describe the kernel's partition layout. */ +if ((partno >=0 || sf->append) && !sf->noact && !sf->notell + &&fdisk_has_label (sf->cxt) && !fdisk_is_regfile (sf->cxt) + && !fdisk_get_parent (sf->cxt)) { + rc =fdisk_get_partitions (sf->cxt, &sf->original_layout); +if (rc) +errx (EXIT_FAILURE ,_ ("failed to read partition table" )); + } +if (fdisk_get_collision (sf->cxt))follow_wipe_mode (sf);
tests/expected/sfdisk/dos-reduce-empty-extended +0 -1
@@ -22,5 +22,4 @@ Device Boot Start End Sectors Size Id Type <removed>2 4096 10239 6144 3M 5 Extended The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/dos-resize +0 -1
@@ -20,5 +20,4 @@ Device Boot Start End Sectors Size Id Type <removed>1 * 2048 102399 100352 49M 82 Linux swap / Solaris The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/dos-resize-empty-extended +0 -1
@@ -22,5 +22,4 @@ Device Boot Start End Sectors Size Id Type <removed>2 4096 102399 98304 48M 5 Extended The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/dos-resize-extended +0 -1
@@ -30,5 +30,4 @@ Device Boot Start End Sectors Size Id Type <removed>6 14336 16383 2048 1M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/dos-resize-logical +0 -1
@@ -30,5 +30,4 @@ Device Boot Start End Sectors Size Id Type <removed>6 14336 28671 14336 7M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/gpt-resize +0 -1
@@ -20,5 +20,4 @@ Device Start End Sectors Size Type <removed>1 2048 102366 100319 49M Linux swap The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/movedata-down +0 -1
@@ -27,5 +27,4 @@ Data move: The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/movedata-non-overlay +0 -1
@@ -27,5 +27,4 @@ Data move: The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/movedata-up +0 -1
@@ -27,5 +27,4 @@ Data move: The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/reread-dos new file +19 -0
@@ -0,0 +1,19 @@ +append: 0 +appended: 16384 2048 +resize: 0 +resized: 8192 6144 +move: 0 +moved: 6144 6144 +add with -N: 0 +added: 20480 2048 +no-act: 0 +unchanged on disk: 0 +no-act: 6144 6144 +no-tell-kernel: 0 +size on disk: 8192 +no-tell-kernel: 6144 6144 +unchanged: 2048 2048 +busy move: 1 +kernel update failed: 0 +start on disk: 4096 +busy: 2048 2048
tests/expected/sfdisk/reread-gpt new file +19 -0
@@ -0,0 +1,19 @@ +append: 0 +appended: 16384 2048 +resize: 0 +resized: 8192 6144 +move: 0 +moved: 6144 6144 +add with -N: 0 +added: 20480 2048 +no-act: 0 +unchanged on disk: 0 +no-act: 6144 6144 +no-tell-kernel: 0 +size on disk: 8192 +no-tell-kernel: 6144 6144 +unchanged: 2048 2048 +busy move: 1 +kernel update failed: 0 +start on disk: 4096 +busy: 2048 2048
tests/expected/sfdisk/resize-dos-0-move-up-absolute +0 -1
@@ -22,5 +22,4 @@ Device Boot Start End Sectors Size Id Type <removed>1 3000 23479 20480 10M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-dos-1-move-down-absolute +0 -1
@@ -22,5 +22,4 @@ Device Boot Start End Sectors Size Id Type <removed>1 2048 22527 20480 10M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-dos-10-down-enlarge +0 -1
@@ -22,5 +22,4 @@ Device Boot Start End Sectors Size Id Type <removed>1 * 2048 204799 202752 99M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-dos-11-absolute-move-resize +0 -1
@@ -22,5 +22,4 @@ Device Boot Start End Sectors Size Id Type <removed>1 * 2048 22527 20480 10M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-dos-12-reduce-midle +0 -1
@@ -26,5 +26,4 @@ Device Boot Start End Sectors Size Id Type <removed>3 43008 63487 20480 10M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-dos-13-max-last +0 -1
@@ -26,5 +26,4 @@ Device Boot Start End Sectors Size Id Type <removed>3 32768 204799 172032 84M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-dos-2-move-up-relative +0 -1
@@ -22,5 +22,4 @@ Device Boot Start End Sectors Size Id Type <removed>1 4096 24575 20480 10M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-dos-3-move-down-relative +0 -1
@@ -22,5 +22,4 @@ Device Boot Start End Sectors Size Id Type <removed>1 2048 22527 20480 10M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-dos-4-enlarge-absolute +0 -1
@@ -22,5 +22,4 @@ Device Boot Start End Sectors Size Id Type <removed>1 2048 32767 30720 15M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-dos-5-reduce-absolute +0 -1
@@ -22,5 +22,4 @@ Device Boot Start End Sectors Size Id Type <removed>1 2048 12287 10240 5M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-dos-6-enlarge-relative +0 -1
@@ -22,5 +22,4 @@ Device Boot Start End Sectors Size Id Type <removed>1 2048 32767 30720 15M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-dos-7-reduce-relative +0 -1
@@ -22,5 +22,4 @@ Device Boot Start End Sectors Size Id Type <removed>1 2048 12287 10240 5M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-dos-8-enlarge-all +0 -1
@@ -22,5 +22,4 @@ Device Boot Start End Sectors Size Id Type <removed>1 2048 204799 202752 99M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-dos-9-up-preduce +0 -1
@@ -22,5 +22,4 @@ Device Boot Start End Sectors Size Id Type <removed>1 22528 204799 182272 89M 83 Linux The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-dos-unsorted +0 -1
@@ -70,5 +70,4 @@ Device Boot Start End Sectors Size Id Type Partition table entries are not in disk order. The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-gpt-0-move-up-absolute +0 -1
@@ -22,5 +22,4 @@ Device Start End Sectors Size Type <removed>1 3000 23479 20480 10M Linux filesystem The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-gpt-1-move-down-absolute +0 -1
@@ -22,5 +22,4 @@ Device Start End Sectors Size Type <removed>1 2048 22527 20480 10M Linux filesystem The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-gpt-10-down-enlarge +0 -1
@@ -22,5 +22,4 @@ Device Start End Sectors Size Type <removed>1 2048 204766 202719 99M Linux filesystem The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-gpt-11-absolute-move-resize +0 -1
@@ -22,5 +22,4 @@ Device Start End Sectors Size Type <removed>1 2048 22527 20480 10M Linux filesystem The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-gpt-12-reduce-midle +0 -1
@@ -26,5 +26,4 @@ Device Start End Sectors Size Type <removed>3 43008 63487 20480 10M Linux filesystem The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-gpt-13-max-last +0 -1
@@ -26,5 +26,4 @@ Device Start End Sectors Size Type <removed>3 32768 204766 171999 84M Linux filesystem The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-gpt-2-move-up-relative +0 -1
@@ -22,5 +22,4 @@ Device Start End Sectors Size Type <removed>1 4096 24575 20480 10M Linux filesystem The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-gpt-3-move-down-relative +0 -1
@@ -22,5 +22,4 @@ Device Start End Sectors Size Type <removed>1 2048 22527 20480 10M Linux filesystem The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-gpt-4-enlarge-absolute +0 -1
@@ -22,5 +22,4 @@ Device Start End Sectors Size Type <removed>1 2048 32767 30720 15M Linux filesystem The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-gpt-5-reduce-absolute +0 -1
@@ -22,5 +22,4 @@ Device Start End Sectors Size Type <removed>1 2048 12287 10240 5M Linux filesystem The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-gpt-6-enlarge-relative +0 -1
@@ -22,5 +22,4 @@ Device Start End Sectors Size Type <removed>1 2048 32767 30720 15M Linux filesystem The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-gpt-7-reduce-relative +0 -1
@@ -22,5 +22,4 @@ Device Start End Sectors Size Type <removed>1 2048 12287 10240 5M Linux filesystem The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-gpt-8-enlarge-all +0 -1
@@ -22,5 +22,4 @@ Device Start End Sectors Size Type <removed>1 2048 204766 202719 99M Linux filesystem The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/expected/sfdisk/resize-gpt-9-up-preduce +0 -1
@@ -22,5 +22,4 @@ Device Start End Sectors Size Type <removed>1 22528 204766 182239 89M Linux filesystem The partition table has been altered. -Calling ioctl() to re-read partition table. Syncing disks.
tests/ts/sfdisk/reread new file +75 -0
75 lines not on this page · raw message
Signature · 1 line
2.55.0