summaryrefslogtreecommitdiff
path: root/recipes-core/initscripts/initscripts-1.0/umountfs
blob: 1c9a9cee6f1c0fda26172e435f6cc62f2c05f5ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
### BEGIN INIT INFO
# Provides:          umountfs
# Required-Start:
# Required-Stop:     
# Default-Start:
# Default-Stop:      0 6
# Short-Description: Turn off swap and unmount all local file systems.
# Description:
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

umount_all() {
  echo "Deactivating swap..."
  swapoff -a

  # We leave /proc mounted.
  echo "Unmounting local filesystems..."
  grep -q /mnt/ram /proc/mounts && mount -o remount,ro /mnt/ram
  umount -f -a -r > /dev/null 2>&1

  mount -o remount,ro /
}

blink_leds() {
  leds=/sys/class/leds/*
  nleds=$(echo $leds | wc -w)

  # turn LEDs off
  for led in $leds; do
      echo "timer" > $led/trigger
      echo 0  > $led/delay_off
  done

  int=200
  total=$(( int * (nleds+1) ))
  on=$(( total - int ))
  off=$(( total - on ))

  # make a pattern
  for led in $leds; do
      echo "$on"  > $led/delay_on
      echo "$off"  > $led/delay_off
      on=$(( on - int ))
      off=$(( total - on ))
  done
}

flash_upgrade() {
  if [ $# -ne 1 ]; then
    echo "need to specify flash-root"
    return
  fi
  # flash_root must be a mountpoint that is not the rootfs and be mounted rw
  local flash_root=${1}

  local flash_dir=${flash_root}/flash-upgrade
  local uImage_file=${flash_dir}/uImage.bin
  local rootfs_file=${flash_dir}/rootfs.jffs2

  local reboot_cmd=/usr/sbin/upgrade-reboot
  local nandwrite_cmd=/usr/bin/nandwrite.static
  local mode=
  local uImage_mtd=
  local rootfs_mtd=

  if [ ! -d "${flash_dir}" ]; then
    echo "${flash_dir} not present, skipping"
    return
  fi

  if ! mountpoint -q "${flash_root}"; then
    echo "${flash_root} is not a mountpoint"
    return
  fi

  mode=$(grep "${flash_root}" /proc/mounts | cut -d ' ' -f 4 | cut -d ',' -f 1)
  if [ "${mode}" != "rw" ]; then
    echo "${flash_root} is not mounted rw"
    return
  fi

  if [ ! -x "${reboot_cmd}" ]; then
    echo "${reboot_cmd} is not installed"
    return
  fi

  if [ ! -x "${nandwrite_cmd}" ]; then
    echo "${nandwrite_cmd} is not installed"
    return
  fi

  uImage_mtd="/dev/$(cat /proc/mtd | grep uImage | cut -d : -f 1)"
  if [ ! -c "${uImage_mtd}" ]; then
    echo "No valid MTD partition is labeled uImage"
    return
  fi

  rootfs_mtd="/dev/$(cat /proc/mtd | grep Rootfs | cut -d : -f 1)"
  if [ ! -c "${rootfs_mtd}" ]; then
    echo "No valid MTD partition is labeled Rootfs"
    return
  fi

  echo ""
  echo "Starting flash upgrade from ${flash_dir}..."
  
  blink_leds

  if [ -f ${uImage_file} ]; then
    echo "Flashing ${uImage_mtd} (uImage) with ${uImage_file}..."

    flash_erase ${uImage_mtd} 0 0
    nandwrite -p ${uImage_mtd} ${uImage_file}
  else
    echo "uImage file ${uImage_file} not found"
  fi

  if [ -f ${rootfs_file} ]; then
    echo "Flashing ${rootfs_mtd} (rootfs) with ${rootfs_file}..."

    cp ${reboot_cmd} ${flash_dir}/upgrade-reboot
    cp ${nandwrite_cmd} ${flash_dir}/nandwrite.static

    sync
    sleep 2
    mount -o remount,ro ${flash_root}

    # flash_root is not going to be umounted
    sed -i -e "\\|${flash_root}| d" /etc/mtab

    umount_all

    flash_erase -j ${rootfs_mtd} 0 0
    ${flash_dir}/nandwrite.static -p ${rootfs_mtd} ${rootfs_file}

    echo "Rebooting..."

    ${flash_dir}/upgrade-reboot

    # Should not get here normally
    echo "upgrade-reboot failed"
    exit 1
  else
    echo "rootfs file ${rootfs_file} not found"
  fi
}

# do flash on reboot if do_flash_upgrade exists
if [ -f /var/volatile/do_flash_upgrade ]; then
    flash_upgrade /var/volatile
    flash_upgrade /media/card
fi

umount_all

: exit 0