-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbuildchroot.sh
More file actions
executable file
·159 lines (136 loc) · 4.17 KB
/
buildchroot.sh
File metadata and controls
executable file
·159 lines (136 loc) · 4.17 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
#!/bin/bash
#sudo apt-get install qemu-user-static debootstrap binfmt-support
#debian
distro_debian=(buster bullseye bookworm trixie)
name=debian
#distro=bullseye
distro=bookworm
#ubuntu
distro_ubuntu=(focal jammy noble)
#name=ubuntu
#distro=jammy #22.04
#arch=armhf
arch=arm64
#arch=amd64
#arch=x86_64
ramdisksize=1G
#sudo apt install debootstrap qemu-user-static
function checkpkg(){
echo "checking for needed packages..."
#fix for debian where /usr/sbin/ is not in path for normal users (correctly)
export PATH=$PATH:/usr/sbin/
for pkg in debootstrap qemu-arm-static qemu-aarch64-static; do
which $pkg >/dev/null;
if [[ $? -ne 0 ]];then
echo "$pkg missing";
exit 1;
fi;
done
}
checkpkg
if [[ -n "$1" ]];then
echo "\$1:"$1
if [[ "$1" =~ armhf|arm64 ]];then
echo "setting arch"
arch=$1
fi
fi
if [[ -n "$2" ]];then
echo "\$2:"$2
isdebian=$(echo ${distro_debian[@]} | grep -o "$2" | wc -w)
isubuntu=$(echo ${distro_ubuntu[@]} | grep -o "$2" | wc -w)
echo "isdebian:$isdebian,isubuntu:$isubuntu"
if [[ $isdebian -ne 0 ]] || [[ $isubuntu -ne 0 ]];then
echo "setting distro"
distro=$2
if [[ $isubuntu -ne 0 ]];then
name="ubuntu"
fi
else
echo "invalid distro $2"
exit 1
fi
fi
echo "create chroot '${name} ${distro}' for ${arch}"
#set -x
targetdir=$(pwd)/${name}_${distro}_${arch}
content=$(ls -A $targetdir 2>/dev/null)
if [[ -e $targetdir ]] && [[ "$content" ]]; then echo "$targetdir already exists - aborting";exit;fi
mkdir -p $targetdir
sudo chown root:root $targetdir
if [[ "$ramdisksize" != "" ]];
then
mount | grep '\s'$targetdir'\s' &>/dev/null #$?=0 found;1 not found
if [[ $? -ne 0 ]];then
echo "mounting tmpfs for building..."
sudo mount -t tmpfs -o size=$ramdisksize none $targetdir
fi
fi
#mount | grep 'proc\|sys'
sudo debootstrap --arch=$arch --foreign $distro $targetdir
case "$arch" in
"armhf")
sudo cp /usr/bin/qemu-arm-static $targetdir/usr/bin/
;;
"arm64")
#for r64 use
sudo cp /usr/bin/qemu-aarch64-static $targetdir/usr/bin/
;;
"amd64")
;;
*) echo "unsupported arch $arch";;
esac
sudo cp /etc/resolv.conf $targetdir/etc
LANG=C
#sudo mount -t proc none $targetdir/proc/
#sudo mount -t sysfs sys $targetdir/sys/
#sudo mount -o bind /dev $targetdir/dev/
sudo chroot $targetdir /debootstrap/debootstrap --second-stage
ret=$?
if [[ $ret -ne 0 ]];then
#sudo umount $targetdir/proc/
#sudo umount $targetdir/sys/
#sudo rm -rf $targetdir/*
exit $ret;
fi
echo 'root:bananapi' | sudo chroot $targetdir /usr/sbin/chpasswd
langcode=de
if [[ "$name" == "debian" ]];then
trees="main contrib non-free non-free-firmware"
if [[ "$distro" =~ bookworm ]];then trees="$trees non-free-firmware"; fi
sudo chroot $targetdir tee "/etc/apt/sources.list" > /dev/null <<EOF
deb http://ftp.$langcode.debian.org/debian $distro $trees
deb-src http://ftp.$langcode.debian.org/debian $distro $trees
deb http://ftp.$langcode.debian.org/debian $distro-updates $trees
deb-src http://ftp.$langcode.debian.org/debian $distro-updates $trees
deb http://security.debian.org/debian-security ${distro}-security $trees
deb-src http://security.debian.org/debian-security ${distro}-security $trees
EOF
else
trees="main universe restricted multiverse"
sudo chroot $targetdir tee "/etc/apt/sources.list" > /dev/null <<EOF
deb http://ports.ubuntu.com/ubuntu-ports/ $distro $trees
deb-src http://ports.ubuntu.com/ubuntu-ports/ $distro $trees
deb http://ports.ubuntu.com/ubuntu-ports/ $distro-security $trees
deb-src http://ports.ubuntu.com/ubuntu-ports/ $distro-security $trees
deb http://ports.ubuntu.com/ubuntu-ports/ $distro-updates $trees
deb-src http://ports.ubuntu.com/ubuntu-ports/ $distro-updates $trees
deb http://ports.ubuntu.com/ubuntu-ports/ $distro-backports $trees
deb-src http://ports.ubuntu.com/ubuntu-ports/ $distro-backports $trees
EOF
fi
#sudo chroot $targetdir cat "/etc/apt/sources.list"
sudo chroot $targetdir bash -c "apt update; apt install --no-install-recommends -y openssh-server"
echo 'PermitRootLogin=yes'| sudo tee -a $targetdir/etc/ssh/sshd_config
echo 'bpi'| sudo tee $targetdir/etc/hostname
(
cd $targetdir
sudo tar -czf ../${distro}_${arch}.tar.gz .
)
if [[ "$ramdisksize" != "" ]];
then
echo "umounting tmpfs..."
sudo umount $targetdir
else
sudo rm -rf $targetdir/.
fi