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