Debian Codegraph @

lib/mkramdisk

raw
1 #! /bin/bash
2
3 #*********************************************************************
4 #
5 # mkramdisk -- mount a ramdisk on top of a directory
6 #
7 # This script is part of FAI (Fully Automatic Installation)
8 # (c) 2003-2022 by Thomas Lange, [email protected]
9 # Universitaet zu Koeln
10 #
11 #*********************************************************************
12
13 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
14 mount_ramdisk() {
15
16 # save all dirs and files under directory $1
17 # mount tmpfs on top of dir $1
18 # extract dirs and files into tmpfs
19
20 local n=
21
22 if [ ! -d "$1" ]; then
23 echo "WARNING: $1 is not a directory. Cannot make it writeable." >&2
24 return
25 fi
26
27 set -e
28 local tmp1=$(mktemp) || exit 12
29 tar -C $1 -cf $tmp1 .
30 mount $2 $3 -onoatime,size=80% -t tmpfs tmpfs $1 || exit 13
31 [ X$verbose = X1 ] && echo "Ramdisk mounted onto $1"
32 tar -C $1 -xf $tmp1
33 rm -f $tmp1
34 set +e
35 }
36 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
37 umount_ramdisk() {
38
39 # put contents of ramdisk back to original location on harddisk
40
41 local disk=$(readlink -f $1)
42 local ram=${disk}-in-ramdisk
43
44 # test, if a ramdisk is mounted to this directory
45 mount | grep -q " $disk type tmpfs"
46 if [ $? -eq 1 ]; then
47 echo "$disk is not a mounted ramdisk." >&2
48 return
49 fi
50
51 set -e
52 mkdir $ram
53 # move current ramdisk to a new location
54 mount --bind $disk $ram
55 umount $disk
56 rm -rf ${disk:?}/* ${disk:?}/.??* || true
57 cp -a $ram/. $disk/. # copy ramdisk contents to disk
58 umount $ram
59 [ X$debug = X1 ] && echo "Ramdisk on $disk umounted"
60 rmdir $ram
61 set +e
62 }
63 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
64
65 size= # no default size
66 umount=0
67 useall=0
68
69 # mkramdisk
70
71
72
73 while getopts ans:u opt ; do
74 case "$opt" in
75 # -a: mount all dirs listed in FAI_RAMDISKS instead of dirs given as arguments
76 a) useall=1 ;;
77 u) umount=1 ;; # umount instead of mount
78 s) size="-o size=$OPTARG" ;; # give size of ramdisk
79 n) mtab="-n" ;; # do not wirte to /etc/mtab
80 esac
81 done
82 shift $((OPTIND - 1))
83
84 # use arguments $@ unless -a was specified
85 [ $useall -eq 0 ] && FAI_RAMDISKS=$@
86
87 for d in $FAI_RAMDISKS; do
88 if [ $umount -eq 1 ]; then
89 umount_ramdisk $d
90 else
91 [ -d "$d" ] && mount_ramdisk $d $size $mtab
92 fi
93 done