-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlfs.sh
More file actions
executable file
·176 lines (147 loc) · 3.84 KB
/
lfs.sh
File metadata and controls
executable file
·176 lines (147 loc) · 3.84 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
set -euo pipefail
# Check if running as root
if [ "$EUID" -ne 0 ] ; then
echo "Run as root!"
exit 1
fi
# Functions
usage() {
printf "Usage: %s [OPTIONS] \n" "$(basename $0)" 1>&2
printf "Get everything ready to work with LFS.\n"
printf "\tOptions:\n"
printf "\t-a\t\tRun all operations (mount lfs directories, check ownership, mount vkfs)\n"
printf "\t-o operation\tDo only this operation\n"
exit 2
}
initialize() {
# Check if LFS variable is set
if [ -z "$LFS" ] ; then
export LFS=/mnt/lfs
echo -e "Set LFS variable: $LFS\n"
fi
lfs_partitions=(
"$LFS"
"$LFS/boot"
"$LFS/home"
"$LFS/usr/src"
)
lfs_vkfs=(
"$LFS/dev"
"$LFS/proc"
"$LFS/sys"
"$LFS/run"
)
}
check_args() {
optstr=":o:ah"
while getopts ${optstr} arg; do
case "${arg}" in
h)
usage
;;
o)
# Do only this operation
initialize
${OPTARG}
;;
a)
# Do everithing
initialize
mount_fs
check_owner
check_vkfs
mount_vkfs
;;
:)
echo "$0: Must supply an argument to -$OPTARG." >&2
exit 3
;;
?)
echo "Invalid option: -${OPTARG}." >&2
exit 4
;;
*)
echo "Unknown error occurred" >&2
exit 5
;;
esac
done
#shift $((OPTIND-1))
}
mount_fs() {
echo "Mounting LFS partitions..."
for dir in "${lfs_partitions[@]}" ; do
# Check that directories exist before attempting the mount
if [ ! -d "$dir" ] ; then
echo "Creating LFS dir $dir"
mkdir -p "$dir"
echo -e "Done.\n"
fi
# Check that directories are mounted
if ! grep -q "$(readlink -f "$dir") " /proc/mounts ; then
echo "Mounting $dir on $(get_fsmp "$dir")"
mount "$(get_fsmp "$dir")" "$dir"
fi
done
echo -e "Done.\n"
}
get_fsmp() {
case "${1}" in
"$LFS") echo "/dev/sdb3" ;;
"$LFS/boot") echo "/dev/sdb1" ;;
"$LFS/home") echo "/dev/sdb2" ;;
"$LFS/usr/src") echo "/dev/sdb4" ;;
*) echo "Unknown Partition" && exit 3 ;;
esac
}
check_owner() {
local uid
uid=$(stat -c '%u' "$LFS"/)
if [ ! "$uid" -eq 0 ] ; then
echo "Changing ownership of the $LFS directories back to root..."
chown -R root "$LFS"
echo -e "Done.\n"
fi
}
check_vkfs() {
echo "Preparing Virtual Kernel File System..."
# Check if LFS subdirs exist
for dir in "${lfs_vkfs[@]}" ; do
# Check that directories exist before attempting the mount
if [ ! -d "$dir" ] ; then
echo "Creating LFS VKFS $dir"
mkdir -p "$dir"
echo -e "Done.\n"
fi
done
#echo "Creating Initial Devices Nodes..."
#mknod -m 600 $LFS/dev/console c 5 1
#mknod -m 666 $LFS/dev/null c 1 3
echo -e "Done.\n"
}
mount_vkfs() {
echo "Mounting VKFS..."
mount -v --bind /dev "$LFS"/dev
mount -v --bind /dev/pts "$LFS"/dev/pts
mount -vt proc proc "$LFS"/proc
mount -vt sysfs sysfs "$LFS"/sys
mount -vt tmpfs tmpfs "$LFS"/run
if [ -h "$LFS/dev/shm" ]; then
mkdir -p "$LFS"/"$(readlink $LFS/dev/shm)"
fi
echo -e "Done.\n"
}
# Check if 0 arguments were given
if [[ ${#} -eq 0 ]]; then
usage
fi
# Check command-line arguments
check_args "$@"
# Final steps
echo "All pre-steps completed, remember to:"
echo -e "\t1) Change user to lfs"
echo -e "\t2) Chroot in the new environment!\n"
read -rn 1 -p "Press any key to continue: "
echo
exit 0