forked from lorenzo-stoakes/kernel-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kerndev-create-foreign
executable file
·125 lines (96 loc) · 3.36 KB
/
kerndev-create-foreign
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
#!/bin/bash
set -e; set -o pipefail; source kerndev-shared.sh
# TODO: Duplicates kerndev-create, lots of copy-pasta, de-duplicate!!
[[ -z "$1" ]] && fatal missing arch.
target_arch=$1
shift
case $target_arch in
"arm")
debootstrap_arch="armhf"
binfmt_sig=":arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\
\xff\xfe\xff\xff\xff:/usr/bin/qemu-arm-static:"
;;
"aarch64")
debootstrap_arch="arm64"
binfmt_sig=":aarch64:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x02\x00\xb7:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\
\xff\xfe\xff\xff:/usr/bin/qemu-aarch64-static:"
;;
*)
fatal Unknown arch $target_arch.
;;
esac
rootfs="rootfs_${target_arch}.img"
binfmt_path="/proc/sys/fs/binfmt_misc"
chroot_path="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
# Functions.
function cleanup()
{
[[ -f $binfmt_path/$target_arch ]] && echo -1 > $binfmt_path/$target_arch
rm -f /mnt/usr/bin/qemu-$target_arch-static
unmount
}
# Sanity checks.
check_exists debootstrap qemu-system-$target_arch qemu-$target_arch-static
[[ ! -d $binfmt_path ]] && fatal "binfmt not available."
[[ -n $USE_EXISTING_IMAGE ]] && [[ ! -f $KERNDEV_PATH/$rootfs ]] && \
fatal "can't find existing rootfs image"
# So many commands need sudo, so just force the issue.
elevate $@
chroot_script_path=$(which kerndev-create-foreign.chroot.sh)
unmount
mkdir -p $KERNDEV_PATH
push_kerndev
if [[ -z "$USE_EXISTING_IMAGE" ]]; then
echo Creating and formatting rootfs image file...
rm -f $rootfs
truncate -s $IMAGE_SIZE $rootfs
mkfs.ext4 -q $rootfs
fi
mount_image $rootfs
trap cleanup EXIT
if [[ -z "$USE_EXISTING_IMAGE" ]]; then
echo "Downloading and installing first stage rootfs into image..."
debootstrap --arch=$debootstrap_arch --variant=minbase --foreign \
--include=$DEBIAN_PACKAGES $DEBIAN_VERSION /mnt $DEBIAN_MIRROR \
>/dev/null
fi
echo Enabling binfmt for chroot...
cp $(which qemu-${target_arch}-static) /mnt/usr/bin/
echo "$binfmt_sig" > $binfmt_path/register
if [[ -z "$USE_EXISTING_IMAGE" ]]; then
echo Entering chroot, downloading and installing second stage rootfs into image...
chroot /mnt /debootstrap/debootstrap --second-stage >/dev/null
fi
echo Running pre-chroot tasks...
cp $chroot_script_path /mnt/chroot.sh
# Copy ssh keys + config.
cp -R /home/$SUDO_USER/.ssh /mnt/
# Git + qemu/arm don't play nice, so clone oh-my-zsh here.
rm -rf /mnt/oh-my-zsh && mkdir /mnt/oh-my-zsh
git clone --depth=1 https://github.com/robbyrussell/oh-my-zsh.git /mnt/oh-my-zsh &>/dev/null
echo Running chroot-ed rootfs image config script...
arch-chroot /mnt /chroot.sh $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
if [[ -n "$ACCESS_CHROOT" ]]; then
echo Entering chroot shell...
arch-chroot /mnt env -i TERM=ansi PATH=$chroot_path /bin/bash
echo ...chroot shell done!
fi
echo Building linux...
# Install separately so we can update user on progress separately + not write
# root owned files!
NO_DONE=y DONT_INSTALL=y sudo -E -u $SUDO_USER kerndev-build $target_arch
echo Installing modules into image...
NO_DONE=y source kerndev-install $target_arch
pop
# Pass ownership back to the user.
give_back $KERNDEV_PATH
say_done