slugos-init: add init script to handle USB device moving in 0.10
authorJohn Bowler <jbowler@nslu2-linux.org>
Wed, 1 Feb 2006 08:25:30 +0000 (08:25 +0000)
committerOpenEmbedded Project <openembedded-devel@lists.openembedded.org>
Wed, 1 Feb 2006 08:25:30 +0000 (08:25 +0000)
 - the init.d script fixfstab uses blkid to build a partition/uuid
   mapping table and compares this to the version from the previous
   boot, if anything changes /etc/fstab is updated

packages/slugos-init/files/functions
packages/slugos-init/files/initscripts/fixfstab [new file with mode: 0644]
packages/slugos-init/files/turnup
packages/slugos-init/slugos-init_0.10.bb

index d631579..ef10d65 100644 (file)
@@ -372,3 +372,17 @@ umountflash(){
 
        return 0
 }
+
+#
+# uuid_by_partition
+#  output a list of partitions and their UUIDs
+uuid_by_partition() {
+       blkid -c /dev/null -s UUID | sed -n 's/^\([^:]*\): .*UUID="\([^"]*\)".*$/\1 \2/p'
+}
+
+#
+# partition_of uuid
+#  return the partition corresponding to the UUID
+partition_of() {
+       sed -n 's/^\([^ ]*\) '"$1"'$/\1/p'
+}
diff --git a/packages/slugos-init/files/initscripts/fixfstab b/packages/slugos-init/files/initscripts/fixfstab
new file mode 100644 (file)
index 0000000..67116a1
--- /dev/null
@@ -0,0 +1,91 @@
+#!/bin/sh
+# validate /etc/fstab against the current UUID list in
+# /etc/uuid_by_partition
+#
+. /etc/default/functions
+pfile=/etc/uuid_by_partition
+
+#
+# use debug to find out what is going on
+test "$1" = start -o "$1" = debug || exit 0
+
+#
+# obtain the current list of parititions with UUIDs
+newlist="$(uuid_by_partition)"
+
+if test -r "$pfile"
+then
+       # read the old list
+       oldlist="$(cat "$pfile")"
+       #
+       # if it hasn't changed nothing need be done
+       test "$newlist" = "$oldlist" && exit 0
+       #
+       # it has changed, but this only matters if
+       # a previously existing uuid has moved, build
+       # a list of old device vs new device for every
+       # uuid which has moved
+       changedlist="$(
+               {       echo "$oldlist"
+                       echo "$newlist"
+               } | awk 'device[$2] == ""{device[$2] = $1}
+                       device[$2] != $1{print device[$2], $1}')"
+
+       if test -n "$changedlist"
+       then
+               # at least one partition has moved, scan the
+               # current fstab to see if it has a reference
+               # to this partition
+               changedfstab="$(
+                       {       echo "$changedlist"
+                               echo '#fstab'
+                               cat /etc/fstab
+                       } | awk 'BEGIN{list=1}
+                               list==1 && $0=="#fstab"{list=0; continue}
+                               list==1{new[$1] = $2; continue}
+                               new[$1] != ""{print $1, new[$1]}')"
+
+               # if this list is not empty edit the fstab
+               if test -n "$changedfstab"
+               then
+                       rm -f /tmp/fstab.$$
+                       # if the edit fails then do not overwrite the old
+                       # partition list - just exit with an error
+                       {       echo "$changedlist"
+                               echo '#fstab'
+                               cat /etc/fstab
+                       } | awk 'BEGIN{list=1}
+                               list==1 && $0=="#fstab"{list=0; continue}
+                               list==1{new[$1] = $2; continue}
+                               new[$1] != ""{$1 = new[$1]}
+                               {print}' >/tmp/fstab.$$ || {
+                               if test "$1" = start
+                               then
+                                       logger -s "/etc/init.d/fixfstab: /tmp/fstab.$$: awk failed"
+                               else
+                                       echo "debug: awk script failed with:" >&2
+                                       echo "$changedlist" >&2
+                                       echo "output in /tmp/fstab.$$" >&2
+                               fi
+                               exit 1
+                       }
+
+                       if test "$1" = start
+                       then
+                               mv /tmp/fstab.$$ /etc/fstab || {
+                                       logger -s "/etc/init.d/fixfstab: /tmp/fstab.$$: update failed"
+                                       exit 1
+                               }
+                       else
+                               echo "debug: fstab changed:"
+                               diff -u /etc/fstab /tmp/fstab.$$
+                       fi
+               fi
+       fi
+fi
+
+# write the new list to the file, only if we
+# are doing something...
+test "$1" = start && echo "$newlist" >"$pfile"
+
+exit 0
index d276486..f7fe0e7 100644 (file)
@@ -16,6 +16,10 @@ INRAM_DISK=""
 # force: override certain checks
 force=
 
+#
+# pfile: the uuid/partition file
+pfile=/etc/uuid_by_partition
+
 #
 # fstype new
 #  The type of the file system mounted on "new"  Outputs the last
@@ -312,7 +316,7 @@ setup_rootfs() {
 # setup_fstab new fsdev fstype fsoptions
 #  Alters the /etc/fstab entry for / to refer to the correct device and
 #  have the correct type and options.  Essential for checkroot to remount
-#  / with the correct options.
+#  / with the correct options.  Writes the initial uuid file.
 #  bad, since sed won't fail even if it changes nothing.
 setup_fstab() {
        sed -i '\@^[^   ]*[     ][      ]*/[    ]@s@^.*$@'"$2   /       $3      $4      1  1"'@' "$1"/etc/fstab
@@ -322,6 +326,11 @@ setup_fstab() {
                echo "  to ensure that the root partition is mounted correctly" >&2
                return 1
        }
+       #
+       # build $pfile
+       uuid_by_partition >"$1""$pfile" ||
+               echo "turnup: $pfile: blkid failed (ignored)" >&2
+       return 0
 }
 
 #
index 179e5a7..6a31fca 100644 (file)
@@ -4,7 +4,7 @@ PRIORITY = "required"
 LICENSE = "GPL"
 DEPENDS = "base-files devio"
 RDEPENDS = "busybox devio"
-PR = "r51"
+PR = "r52"
 
 SRC_URI = "file://boot/flash \
           file://boot/disk \
@@ -12,6 +12,7 @@ SRC_URI = "file://boot/flash \
           file://boot/ram \
           file://boot/network \
           file://boot/udhcpc.script \
+          file://initscripts/fixfstab \
           file://initscripts/syslog.buffer \
           file://initscripts/syslog.file \
           file://initscripts/syslog.network \
@@ -35,7 +36,8 @@ CPROGS = "${USRSBINPROGS} ${SBINPROGS}"
 SCRIPTS = "turnup reflash leds sysconf"
 BOOTSCRIPTS = "flash disk nfs ram network udhcpc.script"
 INITSCRIPTS = "syslog.buffer syslog.file syslog.network zleds\
-       leds_startup rmrecovery sysconfsetup umountinitrd.sh"
+       leds_startup rmrecovery sysconfsetup umountinitrd.sh\
+       fixfstab"
 
 # This just makes things easier...
 S="${WORKDIR}"
@@ -120,6 +122,7 @@ pkg_postinst_slugos-init() {
        test -n "$D" && opt="-r $D"
        update-rc.d $opt hwclock.sh             start  8 S . start 45 0 6 .
        update-rc.d $opt umountinitrd.sh        start  9 S .
+       update-rc.d $opt fixfstab               start 10 S .
        update-rc.d $opt syslog.buffer          start 11 S . start 49 0 6 .
        update-rc.d $opt sysconfsetup           start 12 S .
        update-rc.d $opt syslog.file            start 39 S . start 47 0 6 .