-
Notifications
You must be signed in to change notification settings - Fork 12
/
kerndev-create
executable file
·136 lines (105 loc) · 3.23 KB
/
kerndev-create
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
#!/bin/bash
set -e; set -o pipefail; source kerndev-shared.sh
# If an argument is provided, it specifies a cross-compile target architecture.
target_arch=${1:-"x86_64"}
case $target_arch in
"arm")
exec kerndev-create-foreign arm $@
exit $?
;;
"aarch64")
exec kerndev-create-foreign aarch64 $@
exit $?
;;
"x86_64")
;;
*)
fatal Unknown arch $1.
;;
esac
# Sanity checks.
[[ ! -f "/etc/arch-release" ]] && \
fatal This script is designed for arch linux only, sorry!
check_exists kerndev-create.chroot.sh kerndev-build kerndev-install pacstrap \
truncate mkfs.ext4 arch-chroot
[[ -n $USE_EXISTING_IMAGE ]] && [[ ! -f $KERNDEV_PATH/rootfs.img ]] && \
fatal "can't find existing rootfs image"
chroot_script_path=$(which kerndev-create.chroot.sh)
# So many commands need sudo, so just force the issue.
elevate $@
# We want access to the underlying user.
[[ -z "$SUDO_USER" ]] && fatal please run this using sudo!
# If this fails due to being unable to unmount /mnt, mount below will fail so we
# can get away with ||true here.
unmount
mkdir -p $KERNDEV_PATH
push_kerndev
if [[ -z "$USE_EXISTING_IMAGE" ]]; then
echo Creating and formatting rootfs image file...
rm -f rootfs.img
truncate -s $IMAGE_SIZE rootfs.img
mkfs.ext4 -q rootfs.img
else
echo Using existing rootfs...
fi
mount_image rootfs.img
trap "umount /mnt" EXIT
if [[ -z "$USE_EXISTING_IMAGE" ]]; then
echo Downloading and installing rootfs into image...
pacstrap /mnt base base-devel &>/dev/null
fi
echo Running chroot-ed rootfs image config script...
cp $chroot_script_path /mnt/chroot.sh
# Copy ssh keys + config.
cp -R /home/$SUDO_USER/.ssh /mnt/
arch-chroot /mnt /chroot.sh $SUDO_USER $(id -u $SUDO_USER) $ROOT_PASSWORD
rm /mnt/chroot.sh
# arch-chroot mounts the system's /etc/resolv.conf, so do this outside the
# chroot.
cat >/mnt/etc/resolv.conf <<EOF
nameserver 8.8.8.8
nameserver 8.8.4.4
EOF
echo ...chroot-ed script complete!
if [[ -z "$USE_EXISTING_IMAGE" ]] && [[ -n "$ENABLE_GCOV" ]]; then
echo Generating gcov script...
# Create 'cov' tool to check coverage for a specific file in the kernel tree.
cov_path=/mnt/usr/bin/cov
cat >$cov_path <<-EOF
#!/bin/bash
[[ -z "\$1" ]] && echo "usage: \$(basename \$0) [relative path to source file]" >&2 && exit 1
# ref: https://goo.gl/rnd6yo
args=\${@:1:-1}
f=\${@: -1}
local_linux=/home/$SUDO_USER/linux/
pushd \$local_linux >/dev/null
sudo gcov \$args -o /sys/kernel/debug/gcov/$LINUX_DEV_PATH/\$(dirname \$f) \$local_linux/\$f
sudo chown $SUDO_USER *.gcov
popd >/dev/null
EOF
chmod +x $cov_path
give_back $cov_path
pop
fi
if [[ -n "$ACCESS_CHROOT" ]]; then
echo Entering chroot shell...
arch-chroot /mnt
echo ...chroot shell done!
fi
# We don't need /mnt any more.
unmount
trap - EXIT
if [[ -n "$KERNEL_BUILD_ON_CREATE" ]]; then
echo Building linux...
# Install separately so we can update user on progress separately, also this
# means we can run as the user and thus not write root owned files!
NO_DONE=y DONT_INSTALL=y sudo -E -u $SUDO_USER kerndev-build
echo Installing modules into image...
NO_DONE=y kerndev-install
fi
pop
# Pass ownership back to the user.
# Executing this outside of the chroot probably means uids need to be
# synced. TODO: Fix.
give_back $KERNDEV_PATH
say_done