|
|
發表於 2013-4-18 19:57:49
|
顯示全部樓層
Steps to prepare the read-only root FS:
Assumption:
- running Debian Wheezy
- login as root
1. install cron, mpd & nginx (optional, for serving cover arts for MPod/MPad), update configuration file and do tests as desired
2. stop mpd & nginx from automatic start up (I used "rcconf")
3. update /etc/fstab, mount / as read-only, then reboot
4. login and check whether / is mounted as read-only
5. remount / as writable ( mount -o remount,rw,noatime / )
6. save readonlyroot to /etc/init.d/, chmod 755 /etc/init.d/readonlyroot
7. run command: update-rc.d readonlyroot start 36 S . stop 99 0 6 .
8. run command: service readonlyroot init
9. run command: service readonlyroot save
10. check whether you have /var/persist/current.tar.gz
11. remount / as read-only
12. service start mpd
13. service start nginx
14. if no error, reboot
15. mount / as writable, run: crontab -e
# daily at 4 a.m.
0 4 * * * /etc/init.d/readonlyroot save >/dev/null
OR,
# hourly
0 * * * * /etc/init.d/readonlyroot save >/dev/null
16. start up mpd & nginx automatically on reboot (I used "rcconf")
readonlyroot:- #! /bin/bash
- # readonlyroot.sh -- Setup dirs on the tmpfs to keep the root partition read-only
- # See-Also: http://roland.entierement.nu/pages/debian-on-soekris-howto.html
- # Available-At: https://gist.github.com/1216392
- #
- # Place this file in /etc/init.d and run:
- # update-rc.d readonlyroot start 36 S . stop 99 0 6 .
- #
- # To maintain persistent data, place a file at /etc/cron.d/readonlyroot as:
- # * 4 * * * root /etc/init.d/readonlyroot save >/dev/null
- #
- # 2013.04.11: modified by Pink Cheung for PogoPlug v2
- #
- ### BEGIN INIT INFO
- # Provides: readonlyroot
- # Required-Start: $local_fs
- # Required-Stop: $local_fs
- # X-Stop-After: $network
- # Should-Start:
- # Should-Stop:
- # Default-Start: S
- # Default-Stop: 0 6
- # Short-Description: Prepare read-only root filesystem.
- # Description:
- ### END INIT INFO
- set -e
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- DESC="read-only root"
- PREFIX=/mnt/ro-root
- DIRS=(
- /var/backups
- /var/log
- /var/lib/{alsa,dhcpcd,ntpdate,urandom}
- /var/lib/mpd/playlists
- /var/lib/nginx/{body,fastcgi,proxy,scgi,uwsgi}
- /var/tmp
- )
- FILES=(
- /etc/adjtime
- /var/lib/mpd/tag_cache
- /var/lib/mpd/state
- /var/lib/mpd/sticker.sql
- )
- MKDIRS=(
- /var/log/{apt,fsck,mpd,nginx}
- )
- TOUCHFILES=(
- /var/log/wtmp
- )
- PERSIST=/var/persist
- PERSISTPATHS=(
- /var/backups
- /var/lib/mpd/playlists
- /var/lib/mpd/tag_cache
- /var/lib/mpd/state
- /var/lib/mpd/sticker.sql
- /var/lib/nginx
- )
- TMPFS_OPTS="size=64m,nosuid,nodev"
- ROFSFLAG=/run/lock/readonlyroot_ready
- # ============================================================================
- # ============================================================================
- case "$1" in
- init)
- echo -n "Initializing $DESC... "
- mkdir -p ${PREFIX}
- mountpoint -q ${PREFIX} || mount -t tmpfs -o "$TMPFS_OPTS" tmpfs ${PREFIX}
- for i in "${DIRS[@]}" "${FILES[@]}"; do
- [ ! -L $i ] || [ x"`readlink $i`" != x${PREFIX}$i ] || continue
- echo $i
- mkdir -p `dirname ${PREFIX}$i`
- mv -f $i ${PREFIX}$i || rm -rf $i
- ln -sfn ${PREFIX}$i $i
- done
- echo "done."
- ;;
- start)
- if [ -f "$ROFSFLAG" ]; then
- echo "$DESC in use... "
- exit 0
- fi
- echo -n "Setting up $DESC... "
- mkdir -p ${PREFIX}
- mountpoint -q ${PREFIX} || mount -t tmpfs -o "$TMPFS_OPTS" tmpfs ${PREFIX}
- # skeleton of $PREFIX
- for i in "${DIRS[@]}" "${MKDIRS[@]}" ; do
- mkdir -p ${PREFIX}$i
- done
- # files in $PREFIX
- for i in "${TOUCHFILES[@]}" ; do
- touch ${PREFIX}$i
- done
- # # fix stuffs
- # chown daemon:daemon ${PREFIX}/var/spool/cron/at{jobs,spool}
- # chmod ug=rwx,o=,+t ${PREFIX}/var/spool/cron/at{jobs,spool}
- # chown root:crontab ${PREFIX}/var/spool/cron/crontabs
- # chmod ug=rwx,g-r,o=,+t ${PREFIX}/var/spool/cron/crontabs
- #chown ntp:ntp ${PREFIX}/var/lib/ntp
- chown mpd:audio ${PREFIX}/var/lib/mpd/playlists
- #chown mpd:audio ${PREFIX}/var/log/mpd
- echo "done."
- # load persistent data
- "$0" load
- touch "$ROFSFLAG"
- ;;
- stop)
- #"$0" save
- echo -n "Stopping $DESC... "
- echo "done."
- ;;
- load)
- lastTS=`stat -c %Y "$PERSIST"/current.tar.gz`
- currTS=`date +%s`
- if [ $lastTS -gt $currTS ] ; then
- lastTS=`stat -c %y "$PERSIST"/current.tar.gz | cut -d"." -f 1`
- echo -n "Set system time to $lastTS... "
- date -s "$lastTS"
- fi
- echo -n "Loading persistent data... "
- cd "$PREFIX"
- tar xzpf "$PERSIST"/current.tar.gz
- #rsync -a -q --delete-after "$PERSIST" "$PREFIX"
- echo "done."
- ;;
- save)
- echo -n "Saving persistent data... "
- next=tmp.tar.gz
- fs=`df "$PERSIST" | tail -n +2 | sed 's/.*% *//' | head -n 1`
- : ${fs:=/}
- trap 'mount -o remount,ro "$fs"' EXIT
- mount -o remount,rw "$fs"
- mkdir -p "$PERSIST"
- cd "$PREFIX"
- rm -rf "$PERSIST"/$next
- tar cpzf "$PERSIST"/$next "${PERSISTPATHS[@]#/}" || rm -f "$PERSIST"/current.tar.gz
- mv -f "$PERSIST"/$next "$PERSIST"/current.tar.gz
- #rsync -a -q --delete-after "$PREFIX" "$PERSIST"
- sync
- echo "done."
- ;;
- *)
- echo "Usage: $SCRIPTNAME {start|stop|init|save|load}" >&2
- exit 1
- ;;
- esac
- exit 0
複製代碼 |
|