From patchwork Sun Jan 17 01:22:16 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: nyt-openwrt@countercultured.net X-Patchwork-Id: 569198 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (arrakis.dune.hu [78.24.191.176]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id A835E140B98 for ; Sun, 17 Jan 2016 12:22:38 +1100 (AEDT) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 898F228C60B; Sun, 17 Jan 2016 02:21:46 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD autolearn=unavailable version=3.3.2 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 87A5628C60B for ; Sun, 17 Jan 2016 02:21:41 +0100 (CET) X-policyd-weight: using cached result; rate: -7.6 Received: from countercultured.net (countercultured.net [209.51.175.25]) by arrakis.dune.hu (Postfix) with SMTP for ; Sun, 17 Jan 2016 02:21:41 +0100 (CET) Received: (qmail 901 invoked by uid 1000); 17 Jan 2016 01:22:16 -0000 From: Rob Mosher To: nbd@openwrt.org Date: Sat, 16 Jan 2016 20:22:16 -0500 Message-Id: <1452993736-864-1-git-send-email-nyt-openwrt@countercultured.net> X-Mailer: git-send-email 2.1.4 Cc: Rob Mosher , openwrt-devel@lists.openwrt.org Subject: [OpenWrt-Devel] [PATCH][RFC] x86: Save and restore partition table during upgrade X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" This patch will save the partition table before an upgrade. If the installed image has not changed the partition structure the saved table will be restored, ensuring any user created partitions will be present after the upgrade. An option is added to sysupgrade to disable this feature. Signed-off-by: Rob Mosher --- package/base-files/files/lib/upgrade/common.sh | 1 + package/base-files/files/sbin/sysupgrade | 3 ++ target/linux/x86/Makefile | 2 + .../linux/x86/base-files/lib/upgrade/platform.sh | 45 ++++++++++++++++++++++ 4 files changed, 51 insertions(+) diff --git a/package/base-files/files/lib/upgrade/common.sh b/package/base-files/files/lib/upgrade/common.sh index 761b4c1..aed7f8e 100644 --- a/package/base-files/files/lib/upgrade/common.sh +++ b/package/base-files/files/lib/upgrade/common.sh @@ -67,6 +67,7 @@ run_ramfs() { # [...] install_bin /usr/sbin/ubirsvol install_bin /usr/sbin/ubirmvol install_bin /usr/sbin/ubimkvol + install_bin /usr/sbin/sfdisk for file in $RAMFS_COPY_BIN; do install_bin ${file//:/ } done diff --git a/package/base-files/files/sbin/sysupgrade b/package/base-files/files/sbin/sysupgrade index 93f0749..2f441f8 100755 --- a/package/base-files/files/sbin/sysupgrade +++ b/package/base-files/files/sbin/sysupgrade @@ -10,6 +10,7 @@ export INTERACTIVE=0 export VERBOSE=1 export SAVE_CONFIG=1 export SAVE_OVERLAY=0 +export SAVE_PARTITIONS=1 export DELAY= export CONF_IMAGE= export CONF_BACKUP_LIST=0 @@ -29,6 +30,7 @@ while [ -n "$1" ]; do -q) export VERBOSE="$(($VERBOSE - 1))";; -n) export SAVE_CONFIG=0;; -c) export SAVE_OVERLAY=1;; + -p) export SAVE_PARTITIONS=0;; -b|--create-backup) export CONF_BACKUP="$2" NEED_IMAGE=1; shift;; -r|--restore-backup) export CONF_RESTORE="$2" NEED_IMAGE=1; shift;; -l|--list-backup) export CONF_BACKUP_LIST=1; break;; @@ -62,6 +64,7 @@ upgrade-option: -i interactive mode -c attempt to preserve all changed files in /etc/ -n do not save configuration over reflash + -p do not attempt to restore the partition table after flash. -T | --test Verify image and config .tar.gz but do not actually flash. -F | --force diff --git a/target/linux/x86/Makefile b/target/linux/x86/Makefile index e4bc0d9..e5b4378 100644 --- a/target/linux/x86/Makefile +++ b/target/linux/x86/Makefile @@ -13,6 +13,8 @@ FEATURES:=squashfs ext4 vdi vmdk pcmcia targz SUBTARGETS=generic xen_domu ep80579 geode kvm_guest 64 MAINTAINER:=Felix Fietkau +DEFAULT_PACKAGES += sfdisk + KERNEL_PATCHVER:=4.4 KERNELNAME:=bzImage diff --git a/target/linux/x86/base-files/lib/upgrade/platform.sh b/target/linux/x86/base-files/lib/upgrade/platform.sh index 73ab5ef..7f5a2b1 100644 --- a/target/linux/x86/base-files/lib/upgrade/platform.sh +++ b/target/linux/x86/base-files/lib/upgrade/platform.sh @@ -55,12 +55,57 @@ platform_copy_config() { fi } +save_bootparts() { + disk=${BOOTPART%[0-9]} + if [ -b $disk ]; then + echo "Backing up partition table..." + sfdisk -d $disk > /tmp/sfdisk.before + grep size= /tmp/sfdisk.before | grep -E -v 'size=\s+0,' > /tmp/sfdisk.before.nonempty + fi +} + +restore_bootparts() { + disk=${BOOTPART%[0-9]} + if [ -b $disk ]; then + sfdisk -d $disk > /tmp/sfdisk.after + grep size= /tmp/sfdisk.after | grep -E -v 'size=\s+0,' > /tmp/sfdisk.after.nonempty + before=$(cat /tmp/sfdisk.before.nonempty) + after=$(cat /tmp/sfdisk.after.nonempty) + + #ensure we have both partition tables + if [ -z "$before" -o -z "$after" ]; then + echo "Could not read partition table" + return 1 + fi + + #if nothing changed, we do not need to restore + if [ "$before" = "$after" ]; then + echo "Parition layout unchanged" + return 0 + fi + + diff=$(grep -F -x -v -f /tmp/sfdisk.before.nonempty /tmp/sfdisk.after.nonempty) + + #if partition layout changed, do not restore + if [ -n "$diff" ]; then + echo "Partition layout changed, not restoring." + return 1 + fi + + echo "Restoring partition table..." + sfdisk $disk --force < /tmp/sfdisk.before >&/dev/null + fi +} + platform_do_upgrade() { platform_export_bootpart if [ -b "${BOOTPART%[0-9]}" ]; then sync + [ $SAVE_PARTITIONS = "1" ] && save_bootparts get_image "$@" | dd of="${BOOTPART%[0-9]}" bs=4096 conv=fsync sleep 1 + [ $SAVE_PARTITIONS = "1" ] && restore_bootparts fi } +