File: mkramdisk

package info (click to toggle)
fai 6.5.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,084 kB
  • sloc: sh: 6,774; perl: 5,665; makefile: 138
file content (93 lines) | stat: -rwxr-xr-x 2,431 bytes parent folder | download | duplicates (2)
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
#! /bin/bash

#*********************************************************************
#
# mkramdisk -- mount a ramdisk on top of a directory
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 2003-2022 by Thomas Lange, lange@cs.uni-koeln.de
# Universitaet zu Koeln
#
#*********************************************************************

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
mount_ramdisk() {

    # save all dirs and files under directory $1
    # mount tmpfs on top of dir $1
    # extract dirs and files into tmpfs

    local n=

    if [ ! -d "$1" ]; then
        echo "WARNING: $1 is not a directory. Cannot make it writeable." >&2
        return
    fi

    set -e
    local tmp1=$(mktemp) || exit 12
    tar -C $1 -cf $tmp1 .
    mount $2 $3 -onoatime,size=80% -t tmpfs tmpfs $1 || exit 13
    [ X$verbose = X1 ]  && echo "Ramdisk mounted onto $1"
    tar -C $1 -xf $tmp1
    rm -f $tmp1
    set +e
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
umount_ramdisk() {

    # put contents of ramdisk back to original location on harddisk

    local disk=$(readlink -f $1)
    local ram=${disk}-in-ramdisk

    # test, if a ramdisk is mounted to this directory
    mount | grep -q " $disk type tmpfs"
    if [ $? -eq 1 ]; then
        echo "$disk is not a mounted ramdisk." >&2
        return
    fi

    set -e
    mkdir $ram
    # move current ramdisk to a new location
    mount --bind $disk $ram
    umount $disk
    rm -rf ${disk:?}/* ${disk:?}/.??* || true
    cp -a  $ram/. $disk/.   # copy ramdisk contents to disk
    umount $ram
    [ X$debug = X1 ]  && echo "Ramdisk on $disk umounted"
    rmdir  $ram
    set +e
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

size= # no default size
umount=0
useall=0

# mkramdisk



while getopts ans:u opt ; do
    case "$opt" in
    # -a: mount all dirs listed in FAI_RAMDISKS instead of dirs given as arguments
        a) useall=1 ;;
        u) umount=1 ;;       # umount instead of mount
        s) size="-o size=$OPTARG" ;; # give size of ramdisk
        n) mtab="-n" ;;      # do not wirte to /etc/mtab
        esac
done
shift $((OPTIND - 1))

# use arguments $@ unless -a was specified
[ $useall -eq 0 ] && FAI_RAMDISKS=$@

for d in $FAI_RAMDISKS; do
    if [ $umount -eq 1 ]; then
        umount_ramdisk $d
    else
        [ -d "$d" ] && mount_ramdisk $d $size $mtab
    fi
done