Skip to content

Instantly share code, notes, and snippets.

@sfionov
Created December 3, 2009 20:15
Show Gist options
  • Save sfionov/248465 to your computer and use it in GitHub Desktop.
Save sfionov/248465 to your computer and use it in GitHub Desktop.
#! /bin/sh
### BEGIN INIT INFO
# Provides: dpkg-squashfs
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Squashfsed /var/lib/dpkg
# Description: Use squashfs to speedup dpkg cold start.
# Required kernel modules: aufs, squashfs
# Required packages: squashfs-tools
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Squashfsed dpkg directory"
SCRIPTNAME=/etc/init.d/$NAME
. /lib/lsb/init-functions
DPKG_DIR=/var/lib/dpkg
DPKG_RO=/var/lib/.dpkg.ro
DPKG_RW=/var/lib/.dpkg.rw
SQUASHFS_FILE=/var/lib/dpkg.squashfs
mount_squashfs()
{
mkdir -p ${DPKG_RO}
mount -r -t squashfs -o loop ${SQUASHFS_FILE} ${DPKG_RO}
}
squashfs_mounted()
{
mount | grep -q ${DPKG_RO}
}
umount_squashfs()
{
umount ${DPKG_RO}
}
mount_aufs()
{
mkdir -p ${DPKG_RW}
mount -t aufs -o noplink,br:${DPKG_RW}=rw:${DPKG_RO}=rr dpkgfs ${DPKG_DIR} > /dev/null 2>&1
}
aufs_mounted()
{
mount | grep -q ^dpkgfs
}
umount_aufs()
{
umount ${DPKG_DIR}
}
make_squashfs()
{
rm -f ${SQUASHFS_FILE}.new
mksquashfs ${DPKG_DIR} ${SQUASHFS_FILE}.new -no-progress > /dev/null 2>&1
}
silent_move()
{
if [ -e $1 ]; then
rm -rf $2
mv $1 $2
fi
}
do_start()
{
if ! [ -e ${SQUASHFS_FILE} ]; then
make_squashfs
silent_move ${SQUASHFS_FILE}.new ${SQUASHFS_FILE}
fi
mount_squashfs && mount_aufs
}
do_stop()
{
if aufs_mounted; then
make_squashfs
fuser -k -m ${DPKG_DIR}
umount_aufs
fi
squashfs_mounted && umount_squashfs
silent_move ${SQUASHFS_FILE}.new ${SQUASHFS_FILE}
silent_move ${DPKG_RW} ${DPKG_RW}.old
rm -rf ${DPKG_RW}.old
rm -rf ${DPKG_RO}
return 0
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
*) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
*) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
mount | grep ${DPKG_DIR}
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment