<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <p>I considered a simpler approach without sfdisk, however
      /proc/partitions may not update after writing the image directly
      to the disk with dd, so an approach that actually read the
      partition table from the device was used.  Also, /proc/partitions
      just lists block count, and not partition begin/end info.</p>
    <p>As for the merit, this is quite useful on x86 systems.  For
      example, I'm booting using a 32gb usb drive and would prefer to
      keep the other partitions I have defined intact after an upgrade.<br>
    </p>
    <div class="moz-cite-prefix">On 1/16/2016 9:24 PM, Luiz Angelo Daros
      de Luca wrote:<br>
    </div>
    <blockquote
cite="mid:CAJq09z4dKf-9hKU0UQFiD=X5USJRb1dMFjwXyuZz6Shp7uBzrg@mail.gmail.com"
      type="cite">
      <p dir="ltr">Rob, I'll not judge the merit of keeping the previous
        position table, but I guess dd might be enough for
        saving/restore partitions (I guess only msdos is used). dd and
        /proc/partitions might replace sfdisk.</p>
      <br>
      <div class="gmail_quote">
        <div dir="ltr">Em sáb, 16 de jan de 2016 23:22, Rob Mosher <<a
            moz-do-not-send="true"
            href="mailto:nyt-openwrt@countercultured.net">nyt-openwrt@countercultured.net</a>>
          escreveu:<br>
        </div>
        <blockquote class="gmail_quote" style="margin:0 0 0
          .8ex;border-left:1px #ccc solid;padding-left:1ex">This patch
          will save the partition table before an upgrade.<br>
          If the installed image has not changed the partition structure<br>
          the saved table will be restored, ensuring any user created<br>
          partitions will be present after the upgrade.<br>
          <br>
          An option is added to sysupgrade to disable this feature.<br>
          <br>
          Signed-off-by: Rob Mosher <<a moz-do-not-send="true"
            href="mailto:nyt-openwrt@countercultured.net"
            target="_blank">nyt-openwrt@countercultured.net</a>><br>
          ---<br>
           package/base-files/files/lib/upgrade/common.sh     |  1 +<br>
           package/base-files/files/sbin/sysupgrade           |  3 ++<br>
           target/linux/x86/Makefile                          |  2 +<br>
           .../linux/x86/base-files/lib/upgrade/platform.sh   | 45
          ++++++++++++++++++++++<br>
           4 files changed, 51 insertions(+)<br>
          <br>
          diff --git a/package/base-files/files/lib/upgrade/common.sh
          b/package/base-files/files/lib/upgrade/common.sh<br>
          index 761b4c1..aed7f8e 100644<br>
          --- a/package/base-files/files/lib/upgrade/common.sh<br>
          +++ b/package/base-files/files/lib/upgrade/common.sh<br>
          @@ -67,6 +67,7 @@ run_ramfs() { # <command> [...]<br>
                  install_bin /usr/sbin/ubirsvol<br>
                  install_bin /usr/sbin/ubirmvol<br>
                  install_bin /usr/sbin/ubimkvol<br>
          +       install_bin /usr/sbin/sfdisk<br>
                  for file in $RAMFS_COPY_BIN; do<br>
                          install_bin ${file//:/ }<br>
                  done<br>
          diff --git a/package/base-files/files/sbin/sysupgrade
          b/package/base-files/files/sbin/sysupgrade<br>
          index 93f0749..2f441f8 100755<br>
          --- a/package/base-files/files/sbin/sysupgrade<br>
          +++ b/package/base-files/files/sbin/sysupgrade<br>
          @@ -10,6 +10,7 @@ export INTERACTIVE=0<br>
           export VERBOSE=1<br>
           export SAVE_CONFIG=1<br>
           export SAVE_OVERLAY=0<br>
          +export SAVE_PARTITIONS=1<br>
           export DELAY=<br>
           export CONF_IMAGE=<br>
           export CONF_BACKUP_LIST=0<br>
          @@ -29,6 +30,7 @@ while [ -n "$1" ]; do<br>
                          -q) export VERBOSE="$(($VERBOSE - 1))";;<br>
                          -n) export SAVE_CONFIG=0;;<br>
                          -c) export SAVE_OVERLAY=1;;<br>
          +               -p) export SAVE_PARTITIONS=0;;<br>
                          -b|--create-backup) export CONF_BACKUP="$2"
          NEED_IMAGE=1; shift;;<br>
                          -r|--restore-backup) export CONF_RESTORE="$2"
          NEED_IMAGE=1; shift;;<br>
                          -l|--list-backup) export CONF_BACKUP_LIST=1;
          break;;<br>
          @@ -62,6 +64,7 @@ upgrade-option:<br>
                  -i           interactive mode<br>
                  -c           attempt to preserve all changed files in
          /etc/<br>
                  -n           do not save configuration over reflash<br>
          +       -p           do not attempt to restore the partition
          table after flash.<br>
                  -T | --test<br>
                               Verify image and config .tar.gz but do
          not actually flash.<br>
                  -F | --force<br>
          diff --git a/target/linux/x86/Makefile
          b/target/linux/x86/Makefile<br>
          index e4bc0d9..e5b4378 100644<br>
          --- a/target/linux/x86/Makefile<br>
          +++ b/target/linux/x86/Makefile<br>
          @@ -13,6 +13,8 @@ FEATURES:=squashfs ext4 vdi vmdk pcmcia
          targz<br>
           SUBTARGETS=generic xen_domu ep80579 geode kvm_guest 64<br>
           MAINTAINER:=Felix Fietkau <<a moz-do-not-send="true"
            href="mailto:nbd@openwrt.org" target="_blank">nbd@openwrt.org</a>><br>
          <br>
          +DEFAULT_PACKAGES += sfdisk<br>
          +<br>
           KERNEL_PATCHVER:=4.4<br>
          <br>
           KERNELNAME:=bzImage<br>
          diff --git
          a/target/linux/x86/base-files/lib/upgrade/platform.sh
          b/target/linux/x86/base-files/lib/upgrade/platform.sh<br>
          index 73ab5ef..7f5a2b1 100644<br>
          --- a/target/linux/x86/base-files/lib/upgrade/platform.sh<br>
          +++ b/target/linux/x86/base-files/lib/upgrade/platform.sh<br>
          @@ -55,12 +55,57 @@ platform_copy_config() {<br>
                  fi<br>
           }<br>
          <br>
          +save_bootparts() {<br>
          +       disk=${BOOTPART%[0-9]}<br>
          +       if [ -b $disk ]; then<br>
          +               echo "Backing up partition table..."<br>
          +               sfdisk -d $disk > /tmp/sfdisk.before<br>
          +               grep size= /tmp/sfdisk.before | grep -E -v
          'size=\s+0,' > /tmp/sfdisk.before.nonempty<br>
          +       fi<br>
          +}<br>
          +<br>
          +restore_bootparts() {<br>
          +       disk=${BOOTPART%[0-9]}<br>
          +       if [ -b $disk ]; then<br>
          +               sfdisk -d $disk > /tmp/sfdisk.after<br>
          +               grep size= /tmp/sfdisk.after | grep -E -v
          'size=\s+0,' > /tmp/sfdisk.after.nonempty<br>
          +               before=$(cat /tmp/sfdisk.before.nonempty)<br>
          +               after=$(cat /tmp/sfdisk.after.nonempty)<br>
          +<br>
          +               #ensure we have both partition tables<br>
          +               if [ -z "$before" -o -z "$after" ]; then<br>
          +                       echo "Could not read partition table"<br>
          +                       return 1<br>
          +               fi<br>
          +<br>
          +               #if nothing changed, we do not need to restore<br>
          +               if [ "$before" = "$after" ]; then<br>
          +                       echo "Parition layout unchanged"<br>
          +                       return 0<br>
          +               fi<br>
          +<br>
          +               diff=$(grep -F -x -v -f
          /tmp/sfdisk.before.nonempty /tmp/sfdisk.after.nonempty)<br>
          +<br>
          +               #if partition layout changed, do not restore<br>
          +               if [ -n "$diff" ]; then<br>
          +                       echo "Partition layout changed, not
          restoring."<br>
          +                       return 1<br>
          +               fi<br>
          +<br>
          +               echo "Restoring partition table..."<br>
          +               sfdisk $disk --force < /tmp/sfdisk.before
          >&/dev/null<br>
          +       fi<br>
          +}<br>
          +<br>
           platform_do_upgrade() {<br>
                  platform_export_bootpart<br>
          <br>
                  if [ -b "${BOOTPART%[0-9]}" ]; then<br>
                          sync<br>
          +               [ $SAVE_PARTITIONS = "1" ] &&
          save_bootparts<br>
                          get_image "$@" | dd of="${BOOTPART%[0-9]}"
          bs=4096 conv=fsync<br>
                          sleep 1<br>
          +               [ $SAVE_PARTITIONS = "1" ] &&
          restore_bootparts<br>
                  fi<br>
           }<br>
          +<br>
          --<br>
          2.1.4<br>
          _______________________________________________<br>
          openwrt-devel mailing list<br>
          <a moz-do-not-send="true"
            href="mailto:openwrt-devel@lists.openwrt.org"
            target="_blank">openwrt-devel@lists.openwrt.org</a><br>
          <a moz-do-not-send="true"
            href="https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel"
            rel="noreferrer" target="_blank">https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel</a><br>
        </blockquote>
      </div>
      <div dir="ltr">-- <br>
      </div>
      <p dir="ltr">Luiz Angelo Daros de Luca<br>
        <a moz-do-not-send="true" href="mailto:luizluca@gmail.com">luizluca@gmail.com</a></p>
    </blockquote>
    <br>
  </body>
</html>