Automount Swap Early

From Gentoo Linux Wiki

Jump to: navigation, search

Filesystems TOC

Filesystem.png


[edit] Purpose

The script proposed here is a box independent swap mounter. It automatically detects the swap partition on any available hard disk drive (incl. swap on LVM) and turns it on. It can be useful to simplify /etc/fstab or more usefully for a portable system (e.g., USB pen drive).

[edit] Script

File: /etc/init.d/autoswap
#!/sbin/runscript
# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $
# 
# Autodetect swap partition
# author: Mathias Laurin <mathias_laurin@users.sourceforge.net>
# from the script found at http://gentoo-wiki.com/TIP_automount_swap
# left by an anonymous contributor.
#
# 2006-12-26, v.0.2.0

depend() {
        after checkfs
}

do_swap() {
        local CMD="$1"
        local DEVLIST
        local DEV

        DEVLIST="$(ls /dev/[h-s]d[a-z][0-9]*)"
        # LVM2 support
        [ -c /dev/mapper/control ] && DEVLIST="$DEVLIST $(ls /dev/mapper/*)"

        for DEV in $DEVLIST
        do
                FS="$(file -sL "$DEV"| cut -d' ' -f3)"
                if [ "$FS" = "swap" ]
                then
                        einfo "    $DEV"
                        $CMD "$DEV"
                fi
        done
        return $?
}

start() {
        ebegin "Swap on"
        do_swap /sbin/swapon
        eend $?
}

stop() {
        ebegin "Remove swap from"
        do_swap /sbin/swapoff
        eend $?
}

[edit] Installation

Making the script executable and adding it to the boot runlevel.

Note: Adding this script to the boot runlevel is recommended but not mandatory
chmod 755 /etc/init.d/my-autoswap rc-update add my-autoswap boot
Personal tools