|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# https://askubuntu.com/questions/281763/is-there-any-prebuilt-qemu-ubuntu-image32bit-online/1081171#1081171 |
| 4 | + |
| 5 | +set -eux |
| 6 | + |
| 7 | +debootstrap_dir=debootstrap |
| 8 | +root_filesystem=debootstrap.ext2.qcow2 |
| 9 | + |
| 10 | +sudo apt-get install \ |
| 11 | + gcc-aarch64-linux-gnu \ |
| 12 | + debootstrap \ |
| 13 | + libguestfs-tools \ |
| 14 | + qemu-system-aarch64 \ |
| 15 | + qemu-user-static \ |
| 16 | +; |
| 17 | + |
| 18 | +if [ ! -d "$debootstrap_dir" ]; then |
| 19 | + sudo debootstrap \ |
| 20 | + --arch arm64 \ |
| 21 | + --foreign \ |
| 22 | + bionic \ |
| 23 | + "$debootstrap_dir" \ |
| 24 | + http://ports.ubuntu.com/ubuntu-ports \ |
| 25 | + ; |
| 26 | + sudo mkdir -p "${debootstrap_dir}/usr/bin" |
| 27 | + sudo cp "$(which qemu-aarch64-static)" "${debootstrap_dir}/usr/bin" |
| 28 | + sudo chroot "$debootstrap_dir" /debootstrap/debootstrap --second-stage |
| 29 | + sudo rm -f "$root_filesystem" |
| 30 | +fi |
| 31 | + |
| 32 | +linux_image="$(printf "${debootstrap_dir}/boot/vmlinuz-"*)" |
| 33 | + |
| 34 | +if [ ! -f "$root_filesystem" ]; then |
| 35 | + # Set root password. |
| 36 | + echo 'root:root' | sudo chroot "$debootstrap_dir" chpasswd |
| 37 | + |
| 38 | + # Remount root filesystem as rw. |
| 39 | + cat << EOF | sudo tee "${debootstrap_dir}/etc/fstab" |
| 40 | +/dev/sda / ext4 errors=remount-ro,acl 0 1 |
| 41 | +EOF |
| 42 | + |
| 43 | + # Automaticaly start networking. |
| 44 | + # Otherwise network commands fail with: |
| 45 | + # Temporary failure in name resolution |
| 46 | + # https://askubuntu.com/questions/1045278/ubuntu-server-18-04-temporary-failure-in-name-resolution/1080902#1080902 |
| 47 | + cat << EOF | sudo tee "${debootstrap_dir}/etc/systemd/system/dhclient.service" |
| 48 | +[Unit] |
| 49 | +Description=DHCP Client |
| 50 | +Documentation=man:dhclient(8) |
| 51 | +Wants=network.target |
| 52 | +Before=network.target |
| 53 | +
|
| 54 | +[Service] |
| 55 | +Type=forking |
| 56 | +PIDFile=/var/run/dhclient.pid |
| 57 | +ExecStart=/sbin/dhclient -4 -q |
| 58 | +
|
| 59 | +[Install] |
| 60 | +WantedBy=multi-user.target |
| 61 | +EOF |
| 62 | + sudo ln -sf "${debootstrap_dir}/etc/systemd/system/dhclient.service" \ |
| 63 | + "${debootstrap_dir}/etc/systemd/system/multi-user.target.wants/dhclient.service" |
| 64 | + |
| 65 | + # Why Ubuntu, why. |
| 66 | + # https://bugs.launchpad.net/ubuntu/+source/linux/+bug/759725 |
| 67 | + sudo chmod +r "${linux_image}" |
| 68 | + |
| 69 | + # Generate image file from debootstrap directory. |
| 70 | + # Leave 1Gb extra empty space in the image. |
| 71 | + sudo virt-make-fs \ |
| 72 | + --format qcow2 \ |
| 73 | + --size +1G \ |
| 74 | + --type ext2 \ |
| 75 | + "$debootstrap_dir" \ |
| 76 | + "$root_filesystem" \ |
| 77 | + ; |
| 78 | + sudo chmod 666 "$root_filesystem" |
| 79 | +fi |
| 80 | + |
| 81 | +# Build the Linux kernel. |
| 82 | +linux_image="$(pwd)/linux/debian/build/build-generic/arch/arm64/boot/Image" |
| 83 | +if [ ! -f "$linux_image" ]; then |
| 84 | + #git clone --branch Ubuntu-4.15.0-20.21 --depth 1 git://kernel.ubuntu.com/ubuntu/ubuntu-bionic.git linux |
| 85 | + cd linux |
| 86 | + cat << EOF | patch -p1 |
| 87 | +diff --git a/debian.master/config/config.common.ubuntu b/debian.master/config/config.common.ubuntu |
| 88 | +index 5ff32cb997e9..8a190d3a0299 100644 |
| 89 | +--- a/debian.master/config/config.common.ubuntu |
| 90 | ++++ b/debian.master/config/config.common.ubuntu |
| 91 | +@@ -10153,7 +10153,7 @@ CONFIG_VIDEO_ZORAN_ZR36060=m |
| 92 | + CONFIG_VIPERBOARD_ADC=m |
| 93 | + CONFIG_VIRTIO=y |
| 94 | + CONFIG_VIRTIO_BALLOON=y |
| 95 | +-CONFIG_VIRTIO_BLK=m |
| 96 | ++CONFIG_VIRTIO_BLK=y |
| 97 | + CONFIG_VIRTIO_BLK_SCSI=y |
| 98 | + CONFIG_VIRTIO_CONSOLE=y |
| 99 | + CONFIG_VIRTIO_INPUT=m |
| 100 | +EOF |
| 101 | + export ARCH=arm64 |
| 102 | + export $(dpkg-architecture -aarm64) |
| 103 | + export CROSS_COMPILE=aarch64-linux-gnu- |
| 104 | + fakeroot debian/rules clean |
| 105 | + debian/rules updateconfigs |
| 106 | + fakeroot debian/rules DEB_BUILD_OPTIONS=parallel=`nproc` build-generic |
| 107 | + cd - |
| 108 | +fi |
| 109 | + |
| 110 | +#linux_image=/home/ciro/bak/git/linux-kernel-module-cheat/out/linux/default/aarch64/arch/arm64/boot/Image |
| 111 | +#linux_image=/home/ciro/bak/git/linux-kernel-module-cheat/submodules/linux/debian/build/build-generic/arch/arm64/boot/Image |
| 112 | +qemu-system-aarch64 \ |
| 113 | + -append 'console=ttyAMA0 root=/dev/vda rootfstype=ext2' \ |
| 114 | + -device rtl8139,netdev=net0 \ |
| 115 | + -drive "file=${root_filesystem},format=qcow2" \ |
| 116 | + -kernel "${linux_image}" \ |
| 117 | + -m 2G \ |
| 118 | + -netdev user,id=net0 \ |
| 119 | + -serial mon:stdio \ |
| 120 | + -M virt,highmem=off \ |
| 121 | + -cpu cortex-a57 \ |
| 122 | + -nographic \ |
| 123 | +; |
0 commit comments