xref: /aosp_15_r20/kernel/tests/net/test/rootfs/bullseye-common.sh (revision 2f2c4c7ab4226c71756b9c31670392fdd6887c4f)
1*2f2c4c7aSAndroid Build Coastguard Worker#!/bin/bash
2*2f2c4c7aSAndroid Build Coastguard Worker#
3*2f2c4c7aSAndroid Build Coastguard Worker# Copyright (C) 2021 The Android Open Source Project
4*2f2c4c7aSAndroid Build Coastguard Worker#
5*2f2c4c7aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*2f2c4c7aSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*2f2c4c7aSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*2f2c4c7aSAndroid Build Coastguard Worker#
9*2f2c4c7aSAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
10*2f2c4c7aSAndroid Build Coastguard Worker#
11*2f2c4c7aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*2f2c4c7aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*2f2c4c7aSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*2f2c4c7aSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*2f2c4c7aSAndroid Build Coastguard Worker# limitations under the License.
16*2f2c4c7aSAndroid Build Coastguard Worker#
17*2f2c4c7aSAndroid Build Coastguard Worker
18*2f2c4c7aSAndroid Build Coastguard Worker. $SCRIPT_DIR/common.sh
19*2f2c4c7aSAndroid Build Coastguard Worker
20*2f2c4c7aSAndroid Build Coastguard Workeriptables=iptables-1.8.7
21*2f2c4c7aSAndroid Build Coastguard Workerdebian_iptables=1.8.7-1
22*2f2c4c7aSAndroid Build Coastguard Workercuttlefish=android-cuttlefish
23*2f2c4c7aSAndroid Build Coastguard Worker
24*2f2c4c7aSAndroid Build Coastguard Workersetup_and_build_iptables() {
25*2f2c4c7aSAndroid Build Coastguard Worker  get_installed_packages >/root/originally-installed
26*2f2c4c7aSAndroid Build Coastguard Worker
27*2f2c4c7aSAndroid Build Coastguard Worker  # Install everything needed from bullseye to build iptables
28*2f2c4c7aSAndroid Build Coastguard Worker  apt-get install -y \
29*2f2c4c7aSAndroid Build Coastguard Worker    build-essential \
30*2f2c4c7aSAndroid Build Coastguard Worker    autoconf \
31*2f2c4c7aSAndroid Build Coastguard Worker    automake \
32*2f2c4c7aSAndroid Build Coastguard Worker    bison \
33*2f2c4c7aSAndroid Build Coastguard Worker    debhelper \
34*2f2c4c7aSAndroid Build Coastguard Worker    devscripts \
35*2f2c4c7aSAndroid Build Coastguard Worker    fakeroot \
36*2f2c4c7aSAndroid Build Coastguard Worker    flex \
37*2f2c4c7aSAndroid Build Coastguard Worker    libmnl-dev \
38*2f2c4c7aSAndroid Build Coastguard Worker    libnetfilter-conntrack-dev \
39*2f2c4c7aSAndroid Build Coastguard Worker    libnfnetlink-dev \
40*2f2c4c7aSAndroid Build Coastguard Worker    libnftnl-dev \
41*2f2c4c7aSAndroid Build Coastguard Worker    libtool
42*2f2c4c7aSAndroid Build Coastguard Worker
43*2f2c4c7aSAndroid Build Coastguard Worker  # Construct the iptables source package to build
44*2f2c4c7aSAndroid Build Coastguard Worker  mkdir -p /usr/src/$iptables
45*2f2c4c7aSAndroid Build Coastguard Worker
46*2f2c4c7aSAndroid Build Coastguard Worker  cd /usr/src/$iptables
47*2f2c4c7aSAndroid Build Coastguard Worker    # Download a specific revision of iptables from AOSP
48*2f2c4c7aSAndroid Build Coastguard Worker    wget -qO - \
49*2f2c4c7aSAndroid Build Coastguard Worker      https://android.googlesource.com/platform/external/iptables/+archive/master.tar.gz | \
50*2f2c4c7aSAndroid Build Coastguard Worker      tar -zxf -
51*2f2c4c7aSAndroid Build Coastguard Worker    # Download a compatible 'debian' overlay from Debian salsa
52*2f2c4c7aSAndroid Build Coastguard Worker    # We don't want all of the sources, just the Debian modifications
53*2f2c4c7aSAndroid Build Coastguard Worker    # NOTE: This will only work if Android always uses a version of iptables
54*2f2c4c7aSAndroid Build Coastguard Worker    #       that exists for Debian as well.
55*2f2c4c7aSAndroid Build Coastguard Worker    debian_iptables_dir=pkg-iptables-debian-$debian_iptables
56*2f2c4c7aSAndroid Build Coastguard Worker    wget -qO - \
57*2f2c4c7aSAndroid Build Coastguard Worker      https://salsa.debian.org/pkg-netfilter-team/pkg-iptables/-/archive/debian/$debian_iptables/$debian_iptables_dir.tar.gz | \
58*2f2c4c7aSAndroid Build Coastguard Worker      tar --strip-components 1 -zxf - \
59*2f2c4c7aSAndroid Build Coastguard Worker      $debian_iptables_dir/debian
60*2f2c4c7aSAndroid Build Coastguard Worker  cd -
61*2f2c4c7aSAndroid Build Coastguard Worker
62*2f2c4c7aSAndroid Build Coastguard Worker  cd /usr/src
63*2f2c4c7aSAndroid Build Coastguard Worker    # Generate a source package to leave in the filesystem. This is done for
64*2f2c4c7aSAndroid Build Coastguard Worker    # license compliance and build reproducibility.
65*2f2c4c7aSAndroid Build Coastguard Worker    tar --exclude=debian -cf - $iptables | \
66*2f2c4c7aSAndroid Build Coastguard Worker      xz -9 >$(echo $iptables | tr -s '-' '_').orig.tar.xz
67*2f2c4c7aSAndroid Build Coastguard Worker  cd -
68*2f2c4c7aSAndroid Build Coastguard Worker
69*2f2c4c7aSAndroid Build Coastguard Worker  cd /usr/src/$iptables
70*2f2c4c7aSAndroid Build Coastguard Worker    # Build debian packages from the integrated iptables source
71*2f2c4c7aSAndroid Build Coastguard Worker    dpkg-buildpackage -F -d -us -uc
72*2f2c4c7aSAndroid Build Coastguard Worker  cd -
73*2f2c4c7aSAndroid Build Coastguard Worker
74*2f2c4c7aSAndroid Build Coastguard Worker  get_installed_packages >/root/installed
75*2f2c4c7aSAndroid Build Coastguard Worker  remove_installed_packages /root/originally-installed /root/installed
76*2f2c4c7aSAndroid Build Coastguard Worker  apt-get clean
77*2f2c4c7aSAndroid Build Coastguard Worker}
78*2f2c4c7aSAndroid Build Coastguard Worker
79*2f2c4c7aSAndroid Build Coastguard Workerinstall_and_cleanup_iptables() {
80*2f2c4c7aSAndroid Build Coastguard Worker  cd /usr/src
81*2f2c4c7aSAndroid Build Coastguard Worker    # Find any packages generated, resolve to the debian package name, then
82*2f2c4c7aSAndroid Build Coastguard Worker    # exclude any compat, header or symbol packages
83*2f2c4c7aSAndroid Build Coastguard Worker    packages=$(find -maxdepth 1 -name '*.deb' | colrm 1 2 | cut -d'_' -f1 |
84*2f2c4c7aSAndroid Build Coastguard Worker               grep -ve '-compat$\|-dbg$\|-dbgsym$\|-dev$' | xargs)
85*2f2c4c7aSAndroid Build Coastguard Worker    # Install the patched iptables packages, and 'hold' then so
86*2f2c4c7aSAndroid Build Coastguard Worker    # "apt-get dist-upgrade" doesn't replace them
87*2f2c4c7aSAndroid Build Coastguard Worker    apt-get install --allow-downgrades -y -f \
88*2f2c4c7aSAndroid Build Coastguard Worker      $(for package in $packages; do echo ./${package}_*.deb; done | xargs)
89*2f2c4c7aSAndroid Build Coastguard Worker    for package in $packages; do
90*2f2c4c7aSAndroid Build Coastguard Worker      echo "$package hold" | LANG=C dpkg --set-selections
91*2f2c4c7aSAndroid Build Coastguard Worker    done
92*2f2c4c7aSAndroid Build Coastguard Worker    update-alternatives --set iptables /usr/sbin/iptables-legacy
93*2f2c4c7aSAndroid Build Coastguard Worker
94*2f2c4c7aSAndroid Build Coastguard Worker    # Tidy up the mess we left behind, leaving just the source tarballs
95*2f2c4c7aSAndroid Build Coastguard Worker    rm -rf $iptables *.{buildinfo,changes,deb,dsc}
96*2f2c4c7aSAndroid Build Coastguard Worker  cd -
97*2f2c4c7aSAndroid Build Coastguard Worker}
98*2f2c4c7aSAndroid Build Coastguard Worker
99*2f2c4c7aSAndroid Build Coastguard Workersetup_and_build_cuttlefish() {
100*2f2c4c7aSAndroid Build Coastguard Worker  if [ "$(uname -m)" = "aarch64" ]; then
101*2f2c4c7aSAndroid Build Coastguard Worker    apt-get install -y libc6:amd64
102*2f2c4c7aSAndroid Build Coastguard Worker  fi
103*2f2c4c7aSAndroid Build Coastguard Worker
104*2f2c4c7aSAndroid Build Coastguard Worker  get_installed_packages >/root/originally-installed
105*2f2c4c7aSAndroid Build Coastguard Worker
106*2f2c4c7aSAndroid Build Coastguard Worker  # Install everything needed from bullseye to build android-cuttlefish
107*2f2c4c7aSAndroid Build Coastguard Worker  apt-get install -y \
108*2f2c4c7aSAndroid Build Coastguard Worker    cdbs \
109*2f2c4c7aSAndroid Build Coastguard Worker    debhelper \
110*2f2c4c7aSAndroid Build Coastguard Worker    devscripts \
111*2f2c4c7aSAndroid Build Coastguard Worker    dpkg-dev \
112*2f2c4c7aSAndroid Build Coastguard Worker    equivs \
113*2f2c4c7aSAndroid Build Coastguard Worker    git
114*2f2c4c7aSAndroid Build Coastguard Worker
115*2f2c4c7aSAndroid Build Coastguard Worker  # Fetch android-cuttlefish and build it
116*2f2c4c7aSAndroid Build Coastguard Worker  git clone https://github.com/google/android-cuttlefish.git /usr/src/$cuttlefish
117*2f2c4c7aSAndroid Build Coastguard Worker  for subdir in base frontend; do
118*2f2c4c7aSAndroid Build Coastguard Worker    cd /usr/src/$cuttlefish/$subdir
119*2f2c4c7aSAndroid Build Coastguard Worker      mk-build-deps --install --tool='apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends --yes' debian/control
120*2f2c4c7aSAndroid Build Coastguard Worker      dpkg-buildpackage -d -uc -us
121*2f2c4c7aSAndroid Build Coastguard Worker    cd -
122*2f2c4c7aSAndroid Build Coastguard Worker  done
123*2f2c4c7aSAndroid Build Coastguard Worker
124*2f2c4c7aSAndroid Build Coastguard Worker  get_installed_packages >/root/installed
125*2f2c4c7aSAndroid Build Coastguard Worker  remove_installed_packages /root/originally-installed /root/installed
126*2f2c4c7aSAndroid Build Coastguard Worker  apt-get clean
127*2f2c4c7aSAndroid Build Coastguard Worker}
128*2f2c4c7aSAndroid Build Coastguard Worker
129*2f2c4c7aSAndroid Build Coastguard Workerinstall_and_cleanup_cuttlefish() {
130*2f2c4c7aSAndroid Build Coastguard Worker  # Install and clean up cuttlefish host packages
131*2f2c4c7aSAndroid Build Coastguard Worker  cd /usr/src/$cuttlefish
132*2f2c4c7aSAndroid Build Coastguard Worker    apt-get install -y -f ./cuttlefish-base_*.deb
133*2f2c4c7aSAndroid Build Coastguard Worker    apt-get install -y -f ./cuttlefish-user_*.deb
134*2f2c4c7aSAndroid Build Coastguard Worker    apt-get install -y -f ./cuttlefish-integration_*.deb
135*2f2c4c7aSAndroid Build Coastguard Worker    apt-get install -y -f ./cuttlefish-common_*.deb
136*2f2c4c7aSAndroid Build Coastguard Worker  cd -
137*2f2c4c7aSAndroid Build Coastguard Worker  rm -rf /usr/src/$cuttlefish
138*2f2c4c7aSAndroid Build Coastguard Worker}
139*2f2c4c7aSAndroid Build Coastguard Worker
140*2f2c4c7aSAndroid Build Coastguard Workerbullseye_cleanup() {
141*2f2c4c7aSAndroid Build Coastguard Worker  # SELinux is supported by our kernels, but we don't install the policy files
142*2f2c4c7aSAndroid Build Coastguard Worker  # which causes an error to be printed by systemd. Disable selinux.
143*2f2c4c7aSAndroid Build Coastguard Worker  echo "SELINUX=disabled" >/etc/selinux/config
144*2f2c4c7aSAndroid Build Coastguard Worker
145*2f2c4c7aSAndroid Build Coastguard Worker  # Switch binfmt misc over to a static mount, to avoid an autofs4 dependency
146*2f2c4c7aSAndroid Build Coastguard Worker  systemctl mask proc-sys-fs-binfmt_misc.automount
147*2f2c4c7aSAndroid Build Coastguard Worker  systemctl enable proc-sys-fs-binfmt_misc.mount
148*2f2c4c7aSAndroid Build Coastguard Worker
149*2f2c4c7aSAndroid Build Coastguard Worker  # This package gets pulled in as a phantom dependency. Remove it
150*2f2c4c7aSAndroid Build Coastguard Worker  apt-get purge -y gcc-9-base
151*2f2c4c7aSAndroid Build Coastguard Worker
152*2f2c4c7aSAndroid Build Coastguard Worker  cleanup
153*2f2c4c7aSAndroid Build Coastguard Worker}
154