-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·157 lines (141 loc) · 3.49 KB
/
install.sh
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
#!/bin/bash
if [ `id -u` != 0 ]; then
echo
echo "The installation should be done as root or via the 'sudo' command."
echo
echo "Aborting."
echo
exit 1
fi
usage()
{
cat <<EOF
Usage: $0 [options]
--help : show this help message
--backupdir dir : define location where to store the backup file
(default: '$BACKUPDIR')
--bootdir dir : specify a different location of the /boot folder
--libdir dir : specify a different location of the /lib folder
EOF
exit 1
}
WD=`pwd`
BOOTDIR=""
LIBDIR=""
BACKUPDIR=`dirname $WD`
while true
do
case "$1" in
--help)
usage
;;
--backupdir)
BACKUPDIR="$2"
shift 2
;;
--bootdir)
BOOTDIR="$2"
shift 2
;;
--libdir)
LIBDIR="$2"
shift 2
;;
-*)
echo Unrecognized option: "$1"
usage
;;
*)
break
;;
esac
done
if [ ! -d "$BACKUPDIR" ]; then
echo "The backup directory '$BACKUPDIR' does not exist!"
exit 1
fi
if [ -z "$BOOTDIR" ]; then
BOOTDIR="/boot"
fi
if [ ! -d "$BOOTDIR" ]; then
echo "The boot directory '$BOOTDIR' does not exist!"
exit 1
fi
if [ ! -e "$BOOTDIR/cmdline.txt" ]; then
echo "The directory '$BOOTDIR' does not seem to be the"
echo "Raspberry Pi boot folder! Aborting installation!"
exit 1
fi
if [ -z "$LIBDIR" ]; then
LIBDIR="/lib"
fi
if [ ! -d "$LIBDIR" ]; then
echo "The lib directory '$LIBDIR' does not exist!"
exit 1
fi
if [ ! -d "$LIBDIR/arm-linux-gnueabihf" ]; then
echo "The directory '$LIBDIR' does not seem to be the"
echo "Raspberry Pi lib folder! Aborting installation!"
exit 1
fi
VERSION=`cat version.txt`
KERNELFILE="kernel_$VERSION.img"
KERNELIMG="$BOOTDIR/$KERNELFILE"
CONFIGFILE="$BOOTDIR/config.txt"
FIRMWAREDIR="$LIBDIR/firmware"
MODULESDIR="$LIBDIR/modules"
TMPDIR=`mktemp -d`
BACKUPPKGDIR="$TMPDIR/rpi_kernel_backup_$VERSION"
mkdir $BACKUPPKGDIR
echo "Creating backup of existing components"
if [ -f "$KERNELIMG" ]; then
mv "$KERNELIMG" "$BACKUPPKGDIR"
fi
cp "$CONFIGFILE" "$BACKUPPKGDIR"
if [ -d "$FIRMWAREDIR" ]; then
mv "$FIRMWAREDIR" "$BACKUPPKGDIR"
fi
if [ -d "$MODULESDIR/$VERSION" ]; then
mv "$MODULESDIR/$VERSION" "$BACKUPPKGDIR"
fi
OUTFILE="$BACKUPPKGDIR/rollback.sh"
(
cat <<EOF
#!/bin/bash
read -p "WARNING: the kernel file $KERNELFILE, the boot configuration $CONFIGFILE and the contents of $LIBDIR/firmware and $LIBDIR/modules will be rolled back. Hit ENTER to continue!"
rm -rf "$FIRMWAREDIR"
rm -rf "$MODULESDIR/$VERSION"
if [ -f "$KERNELFILE" ]; then
cp "$KERNELFILE" "$BOOTDIR"
fi
cp config.txt "$BOOTDIR"
cp -R firmware "$LIBDIR"
if [ -d "$VERSION" ]; then
cp -R "$VERSION" "$MODULESDIR"
fi
EOF
) > $OUTFILE
chmod a+x $OUTFILE
BACKUPFILE="$BACKUPDIR/rpi_kernel_backup_$VERSION.tar.gz"
echo "Creating backup file $BACKUPFILE"
cd "$TMPDIR"
tar czf "$BACKUPFILE" `basename $BACKUPPKGDIR`
echo "Installing new kernel, firmware and kernel modules"
cd "$WD"
chown -R root:root .
cp "$KERNELFILE" "$BOOTDIR"
cp -R ./firmware "$LIBDIR"
cp -R ./modules "$LIBDIR"
if [ `grep -c "^kernel=$KERNELFILE$" $CONFIGFILE` -eq 0 ]; then
# only touch config.txt if the new entry does not exist yet
echo "Registering the new kernel in $CONFIGFILE"
if [ `grep -c "^kernel=" $CONFIGFILE` -gt 0 ]; then
# comment the old kernel entry and attach the new one
sed -i 's|^kernel=.*$|# &\nkernel='$KERNELFILE'|g' $CONFIGFILE
else
echo "kernel=$KERNELFILE" >> "$CONFIGFILE"
fi
fi
echo "Cleaning temporary folder"
rm -rf "$TMPDIR"
echo "Installation complete."