@@ -346,8 +346,15 @@ function dump_image {
346346 dump=dump_local_image
347347 fi
348348
349+ # preserve pre-dump partition table
350+ if getargbool 0 rd.kiwi.install.retain_past; then
351+ fetch_local_partition_table " ${image_target} " /tmp/parttable_pre
352+ fi
353+
349354 # setup blocks and blocksize to retain last
350- if getargbool 0 rd.kiwi.install.retain_last; then
355+ if getargbool 0 rd.kiwi.install.retain_last || \
356+ getargbool 0 rd.kiwi.install.retain_past
357+ then
351358 if [ -n " ${image_from_remote} " ]; then
352359 image_size=$(( blocks * blocksize))
353360 parttable=$(
@@ -409,15 +416,49 @@ function dump_image {
409416 report_and_quit " Failed to install image"
410417 fi
411418 fi
419+
420+ # recreate last partition from pre-dump table
421+ if getargbool 0 rd.kiwi.install.retain_past; then
422+ recreate_last_partition " ${image_target} "
423+ fi
412424}
413425
414426function fetch_local_partition_table {
415427 local image_source=$1
416- local parttable=/tmp/parttable
428+ local parttable=$2
429+ if [ -z " ${parttable} " ]; then
430+ parttable=/tmp/parttable
431+ fi
417432 sfdisk -d " ${image_source} " > " ${parttable} " 2> /dev/null
418433 echo " ${parttable} "
419434}
420435
436+ function recreate_last_partition {
437+ # recreate last partition as it existed prior dump
438+ local image_target=$1
439+ local table_type
440+ table_type=$( get_partition_table_type " ${image_target} " )
441+ if [ " ${table_type} " = " gpt" ]; then
442+ relocate_gpt_at_end_of_disk " ${image_target} "
443+ fi
444+ # parttable after image dump, the OS
445+ sfdisk -d " ${image_target} " > /tmp/parttable_1 2> /dev/null
446+ # last partition of table as it existed prior dump
447+ tail -n 1 /tmp/parttable_pre > /tmp/parttable_2
448+ # combine table snippets. Please note there can now be a gap
449+ # between the end of the OS table and the start of the last
450+ # partition of the table prior dump. Please also note, if the
451+ # table prior dump had more partitions than only the last
452+ # one to retain, this table concat will not retain those.
453+ # and the OS dump might have overwritten them. A more
454+ # sophisticated concat procedure would be needed to support
455+ # this case here in this code and also in the
456+ # compatible_to_retain() method
457+ cat /tmp/parttable_1 /tmp/parttable_2 > /tmp/parttable
458+ set_device_lock " ${image_target} " \
459+ sfdisk -f " ${image_target} " < /tmp/parttable
460+ }
461+
421462function fetch_remote_partition_table {
422463 local image_source=$1
423464 local image_size=$2
@@ -452,8 +493,14 @@ function compatible_to_retain {
452493 touch /tmp/retain_not_applicable
453494 return 1
454495 fi
455- if [ ! " ${source_start} " = " ${target_start} " ]; then
456- report_and_quit " Cannot retain partition, start address mismatch"
496+ if getargbool 0 rd.kiwi.install.retain_last; then
497+ if [ ! " ${source_start} " = " ${target_start} " ]; then
498+ report_and_quit " Cannot retain partition, start address mismatch"
499+ fi
500+ elif getargbool 0 rd.kiwi.install.retain_past; then
501+ if [ " ${source_start} " -gt " ${target_start} " ]; then
502+ report_and_quit " Cannot retain partition, image overlaps"
503+ fi
457504 fi
458505 return 0
459506}
@@ -550,7 +597,9 @@ function check_image_integrity {
550597 # no verification wanted
551598 return
552599 fi
553- if getargbool 0 rd.kiwi.install.retain_last; then
600+ if getargbool 0 rd.kiwi.install.retain_last || \
601+ getargbool 0 rd.kiwi.install.retain_past
602+ then
554603 if [ ! -e /tmp/retain_not_applicable ]; then
555604 # no verification possible as only a portion of
556605 # the image got deployed intentionally
0 commit comments