Add restore features to the updater. 91/4191/2
authorWilliam Douglas <william.douglas@intel.com>
Fri, 21 Jun 2013 17:38:18 +0000 (10:38 -0700)
committerWilliam Douglas <william.douglas@intel.com>
Fri, 21 Jun 2013 19:56:43 +0000 (12:56 -0700)
Add targets for factory reset and system restore, also teach the
system-update script new tricks to do the restore or reset.

Change-Id: Ib2907731383a1407feae547d65081d0408fff71c
Signed-off-by: William Douglas <william.douglas@intel.com>
Makefile
factory-reset.target [new file with mode: 0644]
packaging/swup.spec
system-restore.target [new file with mode: 0644]
system-update.sh

index cb389c1..d9496e0 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,3 +4,5 @@ install:
        install -D -m 755 swup.py ${DESTDIR}/usr/bin/swup
        install -D -m 755 system-update.sh ${DESTDIR}/usr/bin/system-update
        install -D -m 644 system-update.service ${DESTDIR}/usr/lib/systemd/system/system-update.service
+       install -D -m 644 system-restore.target ${DESTDIR}/usr/lib/systemd/system/system-restore.target
+       install -D -m 644 system-update.service ${DESTDIR}/usr/lib/systemd/system/factory-reset.target
diff --git a/factory-reset.target b/factory-reset.target
new file mode 100644 (file)
index 0000000..e9d7ff8
--- /dev/null
@@ -0,0 +1,4 @@
+[Unit]
+Description=Factory Reset
+Conflicts=shutdown.target systemd-readahead-collect.service systemd-readahead-replay.service
+AllowIsolate=yes
index 6f75df2..e4b2f20 100644 (file)
@@ -11,6 +11,7 @@ Requires:       deltarpm
 Requires:       python-lxml
 Requires:       python-yaml
 Requires:       python-rpm
+Requires:       snapper
 
 %description
 Software Update Tool.
@@ -23,13 +24,20 @@ Software Update Tool.
 %install
 %make_install
 %install_service system-update.target.wants system-update.service
+%install_service system-restore.target.wants system-update.service
+%install_service factory-reset.target.wants system-update.service
+
 
 %files
 %defattr(-,root,root)
 %{_bindir}/swup
 %{_bindir}/system-update
 %{_unitdir}/system-update.service
+%{_unitdir}/system-restore.target
+%{_unitdir}/factory-reset.target
 %{_unitdir}/system-update.target.wants/system-update.service
+%{_unitdir}/system-restore.target.wants/system-update.service
+%{_unitdir}/factory-reset.target.wants/system-update.service
 
 %changelog
 
diff --git a/system-restore.target b/system-restore.target
new file mode 100644 (file)
index 0000000..e983704
--- /dev/null
@@ -0,0 +1,4 @@
+[Unit]
+Description=System Restore
+Conflicts=shutdown.target systemd-readahead-collect.service systemd-readahead-replay.service
+AllowIsolate=yes
index ef218bd..ada9ed9 100755 (executable)
@@ -1,23 +1,66 @@
 #!/bin/bash
 
-. /etc/os-release
-rm /system-update
+function system_update
+{
 
-# take btrfs snapshot
+       . /etc/os-release
+       rm /system-update
 
-# call updater
+       plymouth update --status="Installing Updates..."
 
-plymouth update --status="Installing Updates..."
+       for i in `ls /var/cache/updatemanager/install`; do
+               UPDATE=$(echo $i | sed -e 's/^[0-9]*-//')
+               mkdir -p /var/cache/zypp/packages/$UPDATE/rpms
+               /usr/bin/swup -i  $UPDATE 2>&1 | tee /var/log/system-update.log
+               rm /var/cache/updatemanager/install/$i
+       done
 
-for i in `ls /var/cache/updatemanager/install`; do
-       UPDATE=$(echo $i | sed -e 's/^[0-9]*-//')
-       mkdir -p /var/cache/zypp/packages/$UPDATE/rpms
-       /usr/bin/swup -i  $UPDATE 2>&1 | tee /var/log/system-update.log
-       rm /var/cache/updatemanager/install/$i
-done
+       # check update status
+       # update failed revert to snapshot
+}
 
-# check update status
-# update failed revert to snapshot
+function system_restore
+{
+       SNAPSHOT=$(cat /var/lib/snapshot-restore)
+
+       rm /var/lib/snapshot-restore
+
+       plymouth update --status="Restoring Snapshot..."
+       /usr/bin/snapper undochange ${SNAPSHOT}..0
+}
+
+function factory_restore
+{
+       VERIFY_ID=$(snapper list | tail -n1 | awk '{ print $3 }')
+       SNAPSHOT=$(cat /var/lib/factory-restore)
+
+       rm /var/lib/factory-restore
+
+       if [ $SNAPSHOT != $VERIFY_ID ]; then
+               echo "Factory reset verification failed"
+               return -1
+       fi
+
+       plymouth update --status="Restoring Factory Default..."
+
+       for i in $(snapper list | tail -n +3 | awk '{ print $3 }'); do
+               if [[ $i == 0 || $i == 1 ]]; then
+                       continue
+               fi
+
+               snapper delete $i
+       done
+
+       snapper undochange 1..0
+}
+
+if [ -f /var/lib/snapshot-restore ]; then
+       system_restore
+elif [ -f /var/lib/factory-restore ]; then
+       factory_restore
+elif [ -e /system-update ]; then
+       system_update
+fi
 
 # reboot
 /sbin/reboot