Skip to content

Commit 01975ed

Browse files
committed
debootstrap-ubuntu-qemu-arm.sh
1 parent 4a15040 commit 01975ed

4 files changed

+130
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ Includes Linux concepts and utilities that work on Linux, not necessarily in the
290290
1. [Docker](docker/)
291291
1. [QEMU](qemu.md)
292292
1. [debootstrap-ubuntu-qemu.sh](debootstrap-ubuntu-qemu.sh)
293+
1. [debootstrap-ubuntu-qemu-arm.sh](debootstrap-ubuntu-qemu-arm.sh)
293294
1. [debootstrap-debian-qemu.sh](debootstrap-debian-qemu.sh)
294295
1. [Xen](xen.md)
295296
1. [Vagrant](vagrang/)

debootstrap-debian-qemu.sh

+4-3
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@ if [ ! -f "$linux_img" ]; then
7676
cd -
7777
fi
7878

79+
linux_img=/home/ciro/bak/git/linux-kernel-module-cheat/submodules/linux/debian/build/./build-generic/arch/x86_64/boot/bzImage
7980
qemu-system-x86_64 \
8081
-append 'console=ttyS0 root=/dev/sda' \
82+
-device rtl8139,netdev=net0 \
8183
-drive "file=${root_filesystem},format=qcow2" \
8284
-enable-kvm \
83-
-serial mon:stdio \
84-
-m 2G \
8585
-kernel "$linux_img" \
86-
-device rtl8139,netdev=net0 \
86+
-m 2G \
8787
-netdev user,id=net0 \
88+
-serial mon:stdio \
8889
;

debootstrap-ubuntu-qemu-arm.sh

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
;

debootstrap-ubuntu-qemu.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bash
22

33
# https://askubuntu.com/questions/281763/is-there-any-prebuilt-qemu-ubuntu-image32bit-online/1081171#1081171
4+
# https://askubuntu.com/questions/802701/kernel-build-cross-compile
45

56
set -eux
67

@@ -16,7 +17,6 @@ sudo apt-get install \
1617
if [ ! -d "$debootstrap_dir" ]; then
1718
# Create debootstrap directory.
1819
# - linux-image-generic: downloads the kernel image we will use under /boot
19-
# - network-manager: automatically starts the network at boot for us
2020
sudo debootstrap \
2121
--include linux-image-generic \
2222
bionic \
@@ -75,6 +75,7 @@ EOF
7575
sudo chmod 666 "$root_filesystem"
7676
fi
7777

78+
linux_image=/home/ciro/bak/git/linux-kernel-module-cheat/submodules/linux/debian/build/./build-generic/arch/x86_64/boot/bzImage
7879
qemu-system-x86_64 \
7980
-append 'console=ttyS0 root=/dev/sda' \
8081
-drive "file=${root_filesystem},format=qcow2" \

0 commit comments

Comments
 (0)