-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathbuildInitramFs.sh
More file actions
executable file
·177 lines (141 loc) · 4.36 KB
/
buildInitramFs.sh
File metadata and controls
executable file
·177 lines (141 loc) · 4.36 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
177
#!/bin/bash
set -x
set -e
#Build initramfs image
# This file is part of PrawnOS (https://www.prawnos.com)
# Copyright (c) 2018 Hal Emmerich <hal@halemmerich.com>
# PrawnOS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
# PrawnOS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with PrawnOS. If not, see <https://www.gnu.org/licenses/>.
if [ -z "$1" ]
then
echo "No base file system image supplied"
exit 1
fi
if [ -z "$2" ]
then
echo "No initramfs resources dir supplied"
exit 1
fi
if [ -z "$3" ]
then
echo "No output location supplied"
exit 1
fi
BASE=$1
RESOURCES=$2
OUT_DIR=$3
TARGET=$4
ARCH_ARMHF=armhf
ARCH_ARM64=arm64
outmnt=$(mktemp -d -p "$(pwd)")
outdev=$(losetup -f)
if [ ! -f $BASE ]
then
echo "No base filesystem, run 'make filesystem' first"
exit 1
fi
#A hacky way to ensure the loops are properly unmounted and the temp files are properly deleted.
#Without this, a reboot is sometimes required to properly clean the loop devices and ensure a clean build
cleanup() {
set +e
umount -l $outmnt > /dev/null 2>&1
rmdir $outmnt > /dev/null 2>&1
losetup -d $outdev > /dev/null 2>&1
set +e
umount -l $outmnt > /dev/null 2>&1
rmdir $outmnt > /dev/null 2>&1
losetup -d $outdev > /dev/null 2>&1
}
function chroot_get_libs
{
set +e
set -x
[ $# -lt 2 ] && return
dest=$1
shift
for i in "$@"
do
# Get an absolute path for the file
[ "${i:0:1}" == "/" ] || i=$(which $i)
# Skip files that already exist at target.
[ -f "$dest/$i" ] && continue
if [ -e "$i" ]
then
# Create destination path
d=`echo "$i" | grep -o '.*/'` &&
mkdir -p "$dest/$d" &&
# Copy file
cat "$i" > "$dest/$i" &&
chmod +x "$dest/$i"
else
echo "Not found: $i"
fi
# Recursively copy shared libraries' shared libraries.
chroot_get_libs "$dest" $(ldd "$i" | egrep -o '/.* ')
done
}
trap cleanup INT TERM EXIT
[ ! -d build ] && mkdir build
losetup -P $outdev $BASE
#mount the root filesystem
mount -o noatime ${outdev}p2 $outmnt
#make a skeleton filesystem
initramfs_src=$outmnt/InstallResources/initramfs_src
rm -rf $initramfs_src*
mkdir -p $initramfs_src
mkdir $initramfs_src/bin
mkdir $initramfs_src/dev
mkdir $initramfs_src/etc
mkdir $initramfs_src/newroot
mkdir $initramfs_src/boot
mkdir $initramfs_src/proc
mkdir $initramfs_src/sys
mkdir $initramfs_src/sbin
mkdir $initramfs_src/run
mkdir $initramfs_src/run/cryptsetup
mkdir $initramfs_src/lib
mknod $initramfs_src/dev/console c 5 1
mknod $initramfs_src/dev/null c 1 3
mknod $initramfs_src/dev/tty c 5 0
mknod $initramfs_src/dev/urandom c 1 9
mknod $initramfs_src/dev/random c 1 8
mknod $initramfs_src/dev/zero c 1 5
#install the few tools we need, and the supporting libs
initramfs_binaries='/bin/busybox /sbin/cryptsetup /sbin/blkid'
#do so **automatigically**
export -f chroot_get_libs
export initramfs_binaries
chroot $outmnt /bin/bash -c "chroot_get_libs /InstallResources/initramfs_src $initramfs_binaries"
#have to add libgcc manually since ldd doesn't see it as a requirement :/
armhf_libs=arm-linux-gnueabihf
arm64_libs=aarch64-linux-gnu
if [ "$TARGET" == "$ARCH_ARMHF" ]; then
LIBS_DIR=$armhf_libs
elif [ "$TARGET" == "$ARCH_ARM64" ]; then
LIBS_DIR=$arm64_libs
else
echo "no valid target arch specified"
exit 1
fi
cp $outmnt/lib/$LIBS_DIR/libgcc_s.so.1 $initramfs_src/lib/$LIBS_DIR/
#add the init script
cp $RESOURCES/initramfs-init $initramfs_src/init
chmod +x $initramfs_src/init
cp $initramfs_src/init $initramfs_src/sbin/init
#compress and install
rm -rf $outmnt/boot/PrawnOS-initramfs.cpio
cd $initramfs_src
ln -s busybox bin/cat
ln -s busybox bin/mount
ln -s busybox bin/sh
ln -s busybox bin/switch_root
ln -s busybox bin/umount
# store for kernel building. gzip is not needed becase kernel + initramfs are gzipped together
find . -print0 | cpio --null --create --verbose --format=newc > $OUT_DIR/PrawnOS-initramfs.cpio