-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrent-modules-tmpfs.sh
More file actions
executable file
·50 lines (43 loc) · 1.74 KB
/
current-modules-tmpfs.sh
File metadata and controls
executable file
·50 lines (43 loc) · 1.74 KB
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
#!/bin/bash
function die {
msg="$1"
shift
printf "$msg\n" "$@" 1>&2
exit 1
}
module_base_dir="$(pkg-config --variable module_directory kmod 2>/dev/null)"
: ${module_base_dir:=/lib/modules}
if ! [[ "$module_base_dir" =~ ^/ ]]; then
die 'Module directory "%s" was not an absolute path' "$module_base_dir"
fi
tmpfs_dir="$module_base_dir/../current-modules"
if mountpoint -q "$tmpfs_dir"; then
# if the tmpfs already exists, assume it's set up properly and succeed without doing any work
exit 0
fi
kernel_version="$(uname -r)"
if [[ -z "$kernel_version" ]]; then
die 'Could not determine kernel version'
fi
current_modules_dir="$module_base_dir/$kernel_version"
if ! [[ -d "$current_modules_dir" ]]; then
if [[ -n "$CURRENT_MODULES_SKIP_MISSING" ]]; then
exit 0
fi
die 'Current modules directory "%s" does not exist' "$current_modules_dir"
fi
# Using -B M suffixes the output with M, so use the raw byte count to just get the number
size_mb=$(du -B $((1024*1024)) -s "$current_modules_dir" | cut -f 1)
if ! [[ "$size_mb" =~ ^[0-9]+$ ]]; then
die 'Invalid number "%s" for module directory size' "$size_mb"
fi
mkdir -p "$tmpfs_dir" || exit $?
# Add 2 to account for rounding and any internal tmpfs bookkeeping
mount -t tmpfs -o size=$((size_mb+2))M current-modules "$tmpfs_dir" || exit $?
# Concatenation is safe because we know module_base_dir starts with a slash
tmpfs_module_base_dir="$tmpfs_dir$module_base_dir"
mkdir -p "$tmpfs_module_base_dir" || exit $?
cp -a -t "$tmpfs_module_base_dir" "$current_modules_dir" || exit $?
# Mark the modules as immutable. This prevents the accidental creation of whiteout files when using the overlay. This is not strictly necessary, and so won't fail the script if it fails.
chattr -Rf +i "$tmpfs_module_base_dir"
exit 0