fixrtc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/bin/sh -e
  2. # initramfs local-premount script for fixrtc
  3. PREREQ=""
  4. # Output pre-requisites
  5. prereqs()
  6. {
  7. echo "$PREREQ"
  8. }
  9. case "$1" in
  10. prereqs)
  11. prereqs
  12. exit 0
  13. ;;
  14. esac
  15. # use the fixrtc cmdline option in your bootloader to
  16. # automatically set the hardware clock to the date of
  17. # the last mount of your root filesystem to avoid fsck
  18. # to get confused by the superblock being in the future
  19. BROKEN_CLOCK=""
  20. ROOTDEV=""
  21. # System partition is currently used by ubuntu touch only
  22. SYSTEMPART=""
  23. for x in $(cat /proc/cmdline); do
  24. case ${x} in
  25. root=*)
  26. value=${x#*=}
  27. # Find the device node path depending on the form of root= :
  28. case ${value} in
  29. UUID=*)
  30. ROOTDEV=/dev/disk/by-uuid/${value#UUID=}
  31. ;;
  32. LABEL=*)
  33. ROOTDEV=/dev/disk/by-label/${value#LABEL=}
  34. ;;
  35. *)
  36. ROOTDEV=${value}
  37. ;;
  38. esac
  39. ;;
  40. systempart=*)
  41. value=${x#*=}
  42. # Find the device node path depending on the form of root= :
  43. case ${value} in
  44. UUID=*)
  45. SYSTEMPART=/dev/disk/by-uuid/${value#UUID=}
  46. ;;
  47. LABEL=*)
  48. SYSTEMPART=/dev/disk/by-label/${value#LABEL=}
  49. ;;
  50. *)
  51. SYSTEMPART=${value}
  52. ;;
  53. esac
  54. # if systempart= is defined we do not want root=/dev/ram
  55. # been taken into account
  56. [ "$ROOTDEV" = "/dev/ram" ] && ROOTDEV=""
  57. ;;
  58. fixrtc)
  59. BROKEN_CLOCK=1
  60. ;;
  61. esac
  62. done
  63. # Touch devices might not have a valid 'root', so use system part if available
  64. if [ -z "$ROOTDEV" -a -n "$SYSTEMPART" ]; then
  65. ROOTDEV=$SYSTEMPART
  66. fi
  67. if [ -n "$BROKEN_CLOCK" -a -n "$ROOTDEV" ]; then
  68. # need udev settle for /dev/disk/by-* symlinks to be added
  69. udevadm settle
  70. wait-for-root "$ROOTDEV" "${ROOTDELAY:-180}"
  71. ROOTDISK=$(readlink -f "$ROOTDEV")
  72. MOUNTDATESTR=$(dumpe2fs -h "$ROOTDISK" 2>/dev/null|grep "Last mount time")
  73. # Trim whitespace for busybox
  74. MOUNTDATE=`echo ${MOUNTDATESTR#*:}`
  75. CREATEDATESTR=$(dumpe2fs -h "$ROOTDISK" 2>/dev/null|grep "Filesystem created") || true
  76. # Trim whitespace for busybox
  77. CREATEDATE=`echo ${CREATEDATESTR#*:}`
  78. hwclock -s || true
  79. # make sure we're also compatible with busybox, if available
  80. DATE="/bin/date -D%c"
  81. date -D%c 2>/dev/null || DATE=/bin/date
  82. TIMESTR=$MOUNTDATE
  83. TIME=$(${DATE} --utc --date "${TIMESTR}" +%s)
  84. CURTIME=$(${DATE} --utc +%s)
  85. # in case the created time is newer than last mount, use that instead
  86. if [ -n "$CREATEDATE" ]; then
  87. CREATETIME=$(${DATE} --utc --date "${CREATEDATE}" +%s)
  88. if [ "$CREATETIME" -gt "$TIME" ]; then
  89. TIMESTR=$CREATEDATE
  90. TIME=$CREATETIME
  91. fi
  92. fi
  93. if [ "$TIME" -gt "$CURTIME" ]; then
  94. ${DATE} --set="${TIMESTR} 1 minute" >/dev/null 2>&1
  95. fi
  96. fi
  97. # This script is best-effort. If we couldn't fudge the clock as desired,
  98. # just try to carry on boot anyway:
  99. # It will probably fail, but we won't have made the situation any worse.
  100. exit 0