• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2#
3# Copyright (C) 2021 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18trap "echo 3 >${exitcode}" ERR
19
20# $1 - Suite name for apt sources
21update_apt_sources() {
22  # Add the needed debian sources
23  cat >/etc/apt/sources.list <<EOF
24deb http://ftp.debian.org/debian bullseye main
25deb-src http://ftp.debian.org/debian bullseye main
26EOF
27
28  # Disable the automatic installation of recommended packages
29  cat >/etc/apt/apt.conf.d/90recommends <<EOF
30APT::Install-Recommends "0";
31EOF
32
33  # On the ARM64, allow packages from AMD64 to be installed
34  dpkg --add-architecture amd64
35
36  # Update for the above changes
37  apt-get update
38}
39
40# $1 - Output file for currently installed packages
41get_installed_packages() {
42  LANG=C dpkg --get-selections | sort
43}
44
45# $1 - File containing package selections to restore to
46# $2 - File containing currently installed packages list
47remove_installed_packages() {
48  apt-get purge --allow-remove-essential -y \
49    $(comm -3 "$1" "$2" | sed -e 's,install,,' -e 's,\t,,' | xargs)
50  rm -f "$1" "$2"
51}
52
53setup_static_networking() {
54  # Temporarily bring up static QEMU SLIRP networking (no DHCP)
55  ip link set dev eth0 up
56  ip addr add 10.0.2.15/24 broadcast 10.0.2.255 dev eth0
57  ip route add default via 10.0.2.2 dev eth0
58
59  # Permanently update the resolv.conf with the Google DNS servers
60  echo "nameserver 8.8.8.8"  >/etc/resolv.conf
61  echo "nameserver 8.8.4.4" >>/etc/resolv.conf
62}
63
64# $1 - Network interface for bridge (or NetworkManager DHCP)
65# $2 - Bridge name. If set to the empty string, NetworkManager is used
66setup_dynamic_networking() {
67  # So isc-dhcp-client can work with a read-only rootfs..
68  cat >>/etc/fstab <<EOF
69tmpfs      /var/lib/dhcp tmpfs defaults 0 0
70EOF
71
72  # Bring up networking one time with dhclient
73  mount /var/lib/dhcp
74  dhclient eth0
75  echo "nameserver 8.8.8.8"  >/run/resolvconf/resolv.conf
76  echo "nameserver 8.8.4.4" >>/run/resolvconf/resolv.conf
77
78  # Set up automatic DHCP for *future* boots
79  if [ -z "$2" ]; then
80    cat >/etc/systemd/network/dhcp.network <<EOF
81[Match]
82Name=$1
83
84[Network]
85DHCP=yes
86EOF
87    # Mask the NetworkManager-wait-online service to prevent hangs
88    systemctl mask NetworkManager-wait-online.service
89  else
90    cat >/etc/network/interfaces.d/$2.conf <<EOF
91auto $2
92iface $2 inet dhcp
93	bridge_ports $1
94	bridge_stp off
95	bridge_fd 0
96EOF
97  fi
98}
99
100setup_cuttlefish_user() {
101  # Add a default user and put them in the right group
102  addgroup --system cvdnetwork
103  useradd -m -G cvdnetwork,kvm,render,sudo,video \
104    -d /home/vsoc-01 --shell /bin/bash vsoc-01
105  echo -e "cuttlefish\ncuttlefish" | passwd vsoc-01
106
107  # Enable unlimited memory locking for vsoc-01, which is needed by protected
108  # KVM, which is enabled by default on arm64 devices
109  echo "vsoc-01 - memlock unlimited" >>/etc/security/limits.conf
110}
111
112# $* - One or more device names for getty spawns
113create_systemd_getty_symlinks() {
114  for device in $*; do
115    ln -s /lib/systemd/system/serial-getty\@.service \
116      /etc/systemd/system/getty.target.wants/serial-getty\@"${device}".service
117  done
118}
119
120# $1 - Additional default command line
121setup_grub() {
122  if [ -n "${embed_kernel_initrd_dtb}" ]; then
123    # For testing the image with a virtual device
124    apt-get install -y grub2-common
125    cat >/etc/default/grub <<EOF
126GRUB_DEFAULT=0
127GRUB_TIMEOUT=5
128GRUB_DISTRIBUTOR=Debian
129GRUB_CMDLINE_LINUX_DEFAULT="quiet"
130GRUB_CMDLINE_LINUX="\\\$cmdline $1"
131EOF
132    mkdir /boot/grub
133    update-grub
134  fi
135}
136
137cleanup() {
138  # Prevents systemd boot issues with read-only rootfs
139  mkdir -p /var/lib/systemd/{coredump,linger,rfkill,timesync}
140  chown systemd-timesync:systemd-timesync /var/lib/systemd/timesync
141
142  # If embedding isn't enabled, remove the embedded modules and initrd and
143  # uninstall the tools to regenerate the initrd, as they're unlikely to
144  # ever be used
145  if [ -z "${embed_kernel_initrd_dtb}" ]; then
146    apt-get purge -y initramfs-tools initramfs-tools-core klibc-utils kmod
147    rm -f "/boot/initrd.img-$(uname -r)"
148    rm -rf "/lib/modules/$(uname -r)"
149  fi
150
151  # Miscellaneous cleanup
152  rm -rf /var/lib/apt/lists/* || true
153  rm -f /root/* || true
154  apt-get clean
155
156  echo 0 >"${exitcode}"
157  sync && poweroff -f
158}
159