base-files: add support for restoring config from tmpfs
[openwrt/openwrt.git] / package / base-files / files / lib / preinit / 80_mount_root
1 # Copyright (C) 2006 OpenWrt.org
2 # Copyright (C) 2010 Vertical Communications
3
4 missing_lines() {
5 local file1 file2 line
6 file1="$1"
7 file2="$2"
8 oIFS="$IFS"
9 IFS=":"
10 while read line; do
11 set -- $line
12 grep -q "^$1:" "$file2" || echo "$*"
13 done < "$file1"
14 IFS="$oIFS"
15 }
16
17 do_mount_root() {
18 mount_root
19 boot_run_hook preinit_mount_root
20 [ -f /sysupgrade.tgz -o -f /tmp/sysupgrade.tar ] && {
21 echo "- config restore -"
22 cp /etc/passwd /etc/group /etc/shadow /tmp
23 cd /
24 [ -f /sysupgrade.tgz ] && tar xzf /sysupgrade.tgz
25 [ -f /tmp/sysupgrade.tar ] && tar xf /tmp/sysupgrade.tar
26 missing_lines /tmp/passwd /etc/passwd >> /etc/passwd
27 missing_lines /tmp/group /etc/group >> /etc/group
28 missing_lines /tmp/shadow /etc/shadow >> /etc/shadow
29 rm /tmp/passwd /tmp/group /tmp/shadow
30 # Prevent configuration corruption on a power loss
31 sync
32 }
33 }
34
35 [ "$INITRAMFS" = "1" ] || boot_hook_add preinit_main do_mount_root