Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright 2019 Google Inc. All rights reserved. |
| 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 | source "${ANDROID_BUILD_TOP}/external/shflags/src/shflags" |
| 18 | |
| 19 | FLAGS_HELP="USAGE: $0 <KERNEL_DIR> [IMAGE]" |
| 20 | |
| 21 | FLAGS "$@" || exit $? |
| 22 | eval set -- "${FLAGS_ARGV}" |
| 23 | |
| 24 | for arg in "$@" ; do |
| 25 | if [ -z $KERNEL_DIR ]; then |
| 26 | KERNEL_DIR=$arg |
| 27 | elif [ -z $IMAGE ]; then |
| 28 | IMAGE=$arg |
| 29 | else |
| 30 | flags_help |
| 31 | exit 1 |
| 32 | fi |
| 33 | done |
| 34 | |
| 35 | if [ -z $KERNEL_DIR ] || [ -z $IMAGE ]; then |
| 36 | flags_help |
| 37 | exit 1 |
| 38 | fi |
| 39 | if [ -e "${IMAGE}" ]; then |
| 40 | echo "error: ${IMAGE} already exists" |
| 41 | exit 1 |
| 42 | fi |
| 43 | if [ ! -e "${KERNEL_DIR}" ]; then |
| 44 | echo "error: can't find '${KERNEL_DIR}'. aborting..." |
| 45 | exit 1 |
| 46 | fi |
| 47 | |
| 48 | # escalate to superuser |
| 49 | if [ $UID -ne 0 ]; then |
| 50 | cd ${ANDROID_BUILD_TOP} |
| 51 | . ./build/envsetup.sh |
| 52 | lunch ${TARGET_PRODUCT}-${TARGET_BUILD_VARIANT} |
| 53 | mmma external/u-boot |
| 54 | cd - |
| 55 | exec sudo -E "${0}" ${@} |
| 56 | fi |
| 57 | |
| 58 | cd ${ANDROID_BUILD_TOP}/external/arm-trusted-firmware |
| 59 | CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rk3399 DEBUG=0 ERROR_DEPRECATED=1 bl31 |
| 60 | export BL31="${ANDROID_BUILD_TOP}/external/arm-trusted-firmware/build/rk3399/release/bl31/bl31.elf" |
| 61 | cd - |
| 62 | |
| 63 | idbloader=`mktemp` |
| 64 | cd ${ANDROID_BUILD_TOP}/external/u-boot |
| 65 | make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- rock-pi-4-rk3399_defconfig |
| 66 | make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- -j`nproc` |
| 67 | ${ANDROID_HOST_OUT}/bin/mkimage -n rk3399 -T rksd -d tpl/u-boot-tpl.bin ${idbloader} |
| 68 | cat spl/u-boot-spl.bin >> ${idbloader} |
| 69 | cd - |
| 70 | |
| 71 | ${ANDROID_BUILD_TOP}/kernel/tests/net/test/build_rootfs.sh -a arm64 -s buster -n ${IMAGE} |
| 72 | if [ $? -ne 0 ]; then |
| 73 | echo "error: failed to build rootfs. exiting..." |
| 74 | exit 1 |
| 75 | fi |
| 76 | truncate -s +3G ${IMAGE} |
| 77 | e2fsck -f ${IMAGE} |
| 78 | resize2fs ${IMAGE} |
| 79 | |
| 80 | mntdir=`mktemp -d` |
| 81 | if [ $? -ne 0 ]; then |
| 82 | echo "error: failed to mkdir tmp. exiting..." |
| 83 | exit 1 |
| 84 | fi |
| 85 | mount ${IMAGE} ${mntdir} |
| 86 | if [ $? != 0 ]; then |
| 87 | echo "error: unable to mount ${IMAGE} ${mntdir}" |
| 88 | exit 1 |
| 89 | fi |
| 90 | |
| 91 | cat > ${mntdir}/boot/boot.cmd << "EOF" |
| 92 | load mmc ${devnum}:${distro_bootpart} 0x02080000 /boot/Image |
| 93 | load mmc ${devnum}:${distro_bootpart} 0x04000000 /boot/uInitrd |
| 94 | load mmc ${devnum}:${distro_bootpart} 0x01f00000 /boot/dtb/rockchip/rk3399-rock-pi-4.dtb |
| 95 | setenv finduuid "part uuid mmc ${devnum}:${distro_bootpart} uuid" |
| 96 | run finduuid |
| 97 | setenv bootargs "earlycon=uart8250,mmio32,0xff1a0000 console=ttyS2,1500000n8 loglevel=7 root=PARTUUID=${uuid} rootwait rootfstype=ext4 sdhci.debug_quirks=0x20000000" |
| 98 | booti 0x02080000 0x04000000 0x01f00000 |
| 99 | EOF |
| 100 | ${ANDROID_HOST_OUT}/bin/mkimage \ |
| 101 | -C none -A arm -T script -d ${mntdir}/boot/boot.cmd ${mntdir}/boot/boot.scr |
| 102 | |
| 103 | cd ${KERNEL_DIR} |
| 104 | export PATH=${ANDROID_BUILD_TOP}/prebuilts/clang/host/linux-x86/clang-r353983c/bin:$PATH |
| 105 | export PATH=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin:$PATH |
| 106 | make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \ |
| 107 | CLANG_TRIPLE=aarch64-linux-gnu- rockpi4_defconfig |
| 108 | make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \ |
| 109 | CLANG_TRIPLE=aarch64-linux-gnu- -j`nproc` |
| 110 | |
| 111 | cp ${KERNEL_DIR}/arch/arm64/boot/Image ${mntdir}/boot/ |
| 112 | mkdir -p ${mntdir}/boot/dtb/rockchip/ |
| 113 | cp ${KERNEL_DIR}/arch/arm64/boot/dts/rockchip/rk3399-rock-pi-4.dtb ${mntdir}/boot/dtb/rockchip/ |
| 114 | cd - |
| 115 | |
Tristan Muntsinger | 5ea7c6a | 2019-10-15 16:53:08 -0700 | [diff] [blame^] | 116 | mount -o bind /proc ${mntdir}/proc |
| 117 | mount -o bind /sys ${mntdir}/sys |
| 118 | mount -o bind /dev ${mntdir}/dev |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 119 | |
| 120 | echo "Installing required packages..." |
| 121 | chroot ${mntdir} /bin/bash <<EOF |
| 122 | apt-get update |
Tristan Muntsinger | 5ea7c6a | 2019-10-15 16:53:08 -0700 | [diff] [blame^] | 123 | apt-get install -y -f initramfs-tools u-boot-tools network-manager openssh-server sudo man-db vim git dpkg-dev cdbs debhelper config-package-dev gdisk eject lzop binfmt-support ntpdate |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 124 | EOF |
| 125 | |
| 126 | echo "Turning on DHCP client..." |
| 127 | cat >${mntdir}/etc/systemd/network/dhcp.network <<EOF |
| 128 | [Match] |
| 129 | Name=en* |
| 130 | |
| 131 | [Network] |
| 132 | DHCP=yes |
| 133 | EOF |
| 134 | |
Tristan Muntsinger | 5ea7c6a | 2019-10-15 16:53:08 -0700 | [diff] [blame^] | 135 | chroot ${mntdir} /bin/bash << "EOT" |
| 136 | echo "Adding user vsoc-01 and groups..." |
| 137 | useradd -m -G kvm,sudo -d /home/vsoc-01 --shell /bin/bash vsoc-01 |
| 138 | echo -e "cuttlefish\ncuttlefish" | passwd |
| 139 | echo -e "cuttlefish\ncuttlefish" | passwd vsoc-01 |
| 140 | EOT |
| 141 | |
| 142 | echo "Cloning android-cuttlefish..." |
| 143 | cd ${mntdir}/home/vsoc-01 |
| 144 | git clone https://github.com/google/android-cuttlefish.git |
| 145 | cd - |
| 146 | |
Tristan Muntsinger | d32ee2b | 2019-10-15 15:07:00 -0700 | [diff] [blame] | 147 | echo "Creating cleanup script..." |
| 148 | cat > ${mntdir}/usr/local/bin/install-cleanup << "EOF" |
| 149 | #!/bin/bash |
Tristan Muntsinger | 5ea7c6a | 2019-10-15 16:53:08 -0700 | [diff] [blame^] | 150 | echo "Installing cuttlefish-common package..." |
| 151 | echo "nameserver 8.8.8.8" > /etc/resolv.conf |
| 152 | MAC=`ip link | grep eth0 -A1 | grep ether | sed 's/.*\(..:..:..:..:..:..\) .*/\1/'` |
| 153 | sed -i " 1 s/.*/& rockpi-${MAC}/" /etc/hosts |
| 154 | sudo hostnamectl set-hostname "rockpi-${MAC}" |
| 155 | |
| 156 | dpkg --add-architecture amd64 |
| 157 | until ping -c1 ftp.debian.org; do sleep 1; done |
| 158 | ntpdate time.google.com |
| 159 | while true; do |
| 160 | apt-get -o Acquire::Check-Valid-Until=false update |
| 161 | if [ $? != 0 ]; then sleep 1; continue; fi |
| 162 | apt-get install -y -f libc6:amd64 qemu-user-static |
| 163 | if [ $? != 0 ]; then sleep 1; continue; fi |
| 164 | break |
| 165 | done |
| 166 | cd /home/vsoc-01/android-cuttlefish |
| 167 | dpkg-buildpackage -d -uc -us |
| 168 | apt-get install -y -f ../cuttlefish-common_*_arm64.deb |
| 169 | apt-get clean |
| 170 | usermod -aG cvdnetwork vsoc-01 |
| 171 | chown -R vsoc-01:vsoc-01 /home/vsoc-01 |
| 172 | chmod 660 /dev/vhost-vsock |
| 173 | chown root:cvdnetwork /dev/vhost-vsock |
| 174 | |
Tristan Muntsinger | d32ee2b | 2019-10-15 15:07:00 -0700 | [diff] [blame] | 175 | rm /etc/machine-id |
| 176 | rm /var/lib/dbus/machine-id |
| 177 | dbus-uuidgen --ensure |
| 178 | systemd-machine-id-setup |
| 179 | |
| 180 | systemctl disable cleanup |
| 181 | rm /usr/local/bin/install-cleanup |
| 182 | EOF |
| 183 | chmod +x ${mntdir}/usr/local/bin/install-cleanup |
| 184 | |
| 185 | echo "Creating cleanup service..." |
| 186 | cat > ${mntdir}/etc/systemd/system/cleanup.service << EOF |
| 187 | [Unit] |
| 188 | Description=cleanup service |
| 189 | ConditionPathExists=/usr/local/bin/install-cleanup |
| 190 | |
| 191 | [Service] |
| 192 | Type=simple |
| 193 | ExecStart=/usr/local/bin/install-cleanup |
| 194 | TimeoutSec=0 |
| 195 | StandardOutput=tty |
| 196 | |
| 197 | [Install] |
| 198 | WantedBy=multi-user.target |
| 199 | EOF |
| 200 | |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 201 | chroot ${mntdir} /bin/bash << "EOT" |
Tristan Muntsinger | d32ee2b | 2019-10-15 15:07:00 -0700 | [diff] [blame] | 202 | echo "Enabling services..." |
| 203 | systemctl enable cleanup |
| 204 | |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 205 | echo "Creating Initial Ramdisk..." |
| 206 | update-initramfs -c -t -k "5.2.0" |
| 207 | mkimage -A arm -O linux -T ramdisk -C none -a 0 -e 0 -n uInitrd -d /boot/initrd.img-5.2.0 /boot/uInitrd-5.2.0 |
| 208 | ln -s /boot/uInitrd-5.2.0 /boot/uInitrd |
| 209 | EOT |
| 210 | |
| 211 | umount ${mntdir}/sys |
| 212 | umount ${mntdir}/dev |
| 213 | umount ${mntdir}/proc |
| 214 | umount ${mntdir} |
| 215 | |
| 216 | # Turn on journaling |
| 217 | tune2fs -O ^has_journal ${IMAGE} |
| 218 | e2fsck -fy ${IMAGE} >/dev/null 2>&1 |
| 219 | |
| 220 | # Minimize rootfs filesystem |
| 221 | while true; do |
| 222 | out=`sudo resize2fs -M ${IMAGE} 2>&1` |
| 223 | if [[ $out =~ "Nothing to do" ]]; then |
| 224 | break |
| 225 | fi |
| 226 | done |
| 227 | |
| 228 | # Minimize rootfs file size |
| 229 | block_count=`sudo tune2fs -l ${IMAGE} | grep "Block count:" | sed 's/.*: *//'` |
| 230 | block_size=`sudo tune2fs -l ${IMAGE} | grep "Block size:" | sed 's/.*: *//'` |
| 231 | sector_size=512 |
| 232 | start_sector=32768 |
| 233 | fs_size=$(( block_count*block_size )) |
| 234 | fs_sectors=$(( fs_size/sector_size )) |
| 235 | part_sectors=$(( ((fs_sectors-1)/2048+1)*2048 )) # 1MB-aligned |
| 236 | end_sector=$(( start_sector+part_sectors-1 )) |
| 237 | secondary_gpt_sectors=33 |
| 238 | fs_end=$(( (end_sector+secondary_gpt_sectors+1)*sector_size )) |
| 239 | image_size=$(( part_sectors*sector_size )) |
| 240 | truncate -s ${image_size} ${IMAGE} |
| 241 | e2fsck -fy ${IMAGE} >/dev/null 2>&1 |
| 242 | |
| 243 | # Create final image |
| 244 | tmpimg=`mktemp` |
| 245 | truncate -s ${fs_end} ${tmpimg} |
| 246 | mknod -m640 /dev/loop100 b 7 100 |
| 247 | chown root:disk /dev/loop100 |
| 248 | |
| 249 | # Create GPT |
| 250 | sgdisk -Z -a1 ${tmpimg} |
| 251 | sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${tmpimg} |
| 252 | sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${tmpimg} |
| 253 | sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${tmpimg} |
| 254 | sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${tmpimg} |
| 255 | sgdisk -a1 -n:5:32768:${end_sector} -A:5:set:2 -t:5:8305 -c:5:rootfs ${tmpimg} |
| 256 | |
| 257 | device=$(losetup -f) |
| 258 | losetup ${device} ${tmpimg} |
| 259 | partx -v --add ${device} |
| 260 | |
| 261 | # copy over data |
| 262 | dd if=${idbloader} of=${device}p1 |
| 263 | dd if=${ANDROID_BUILD_TOP}/external/u-boot/u-boot.itb of=${device}p3 |
| 264 | dd if=${IMAGE} of=${device}p5 bs=1M |
| 265 | |
| 266 | chown $SUDO_USER:`id -ng $SUDO_USER` ${tmpimg} |
| 267 | mv ${tmpimg} ${IMAGE} |
| 268 | partx -v --delete ${device} |
| 269 | losetup -d ${device} |