-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain
executable file
·161 lines (117 loc) · 4.34 KB
/
main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/sh
# This helps to see what is going on
set -e -x
DESTDIR="$(mktemp -d /tmp/imagebuilder-qemu_aarch64.XXXXXX)"
# copy output artifacts to DESTDIR
cp -r /home/builder/output-dom0-builder ${DESTDIR}
cp -r /home/builder/output-viryaos-xen-boot ${DESTDIR}
cp -r /home/builder/output-viryaos-grub ${DESTDIR}
cp /home/builder/viryaos.dtb ${DESTDIR}
mkdir ${DESTDIR}/viryaos-xen
cd ${DESTDIR}/viryaos-xen
tar zxvf ${DESTDIR}/output-viryaos-xen-boot/viryaos-xen-boot.tar.gz
cd ${DESTDIR}
_vmlinuz=$(basename ${DESTDIR}/output-dom0-builder/vmlinuz-*)
_fullkver=${_vmlinuz#vmlinuz-}
# remove unnecessary symlinks and config file
find ${DESTDIR}/viryaos-xen/boot -type l -exec rm {} \;
rm ${DESTDIR}/viryaos-xen/boot/xen-*.config
_xen=$(basename ${DESTDIR}/viryaos-xen/boot/xen-*)
_fullxenver=${_xen#xen-}
# grub.cfg
cat <<-EOF > ${DESTDIR}/grub.cfg
set timeout=1
set gfxpayload=text
menuentry "Linux" {
insmod part_gpt
insmod ext2
set root=(hd2,gpt2)
xen_hypervisor /boot/xen-$_fullxenver placeholder dom0_mem=1G
xen_module /boot/vmlinuz-$_fullkver console="hvc0" modules=loop,squashfs rootfs_cpio
xen_module /boot/initramfs-$_fullkver
devicetree /boot/viryaos.dtb
}
EOF
OUTDIR="/tmp/img"
mkdir ${OUTDIR}
IMG="${OUTDIR}/qemu_aarch64.img"
# Both GPT partitions and VFAT filesystem uses 512 as the default
# sector size
_sector_size=512
_esp_file="${DESTDIR}/esp.img"
# For now we assume 16384 KiB, which is 32768 sectors
_esp_file_size_kib=16384
mkfs.vfat -v -C $_esp_file $_esp_file_size_kib
mmd -i $_esp_file ::/EFI
mmd -i $_esp_file ::/EFI/BOOT
mcopy -i $_esp_file -s ${DESTDIR}/output-viryaos-grub/BOOTAA64.EFI ::/EFI/BOOT/
mcopy -i $_esp_file -s ${DESTDIR}/grub.cfg ::/grub.cfg
# NOTE: Increase vos_a to 256 to accomodate rootfs
# 528 MiB (256 + 256 + 16)
truncate $IMG -s 528M
# create GPT partition table
sgdisk -og $IMG
_esp_file_size_bytes=$(( $_esp_file_size_kib * 1024 ))
_esp_file_size_sectors=$(( $_esp_file_size_bytes / $_sector_size ))
# start ESP at sector 2048
_esp_sector_start=2048
_esp_sector_end=$(( $_esp_sector_start + $_esp_file_size_sectors - 1 ))
# create EFI System Partition
sgdisk -n 1:$_esp_sector_start:$_esp_sector_end -c 1:"EFI System Partition" -t 1:ef00 $IMG
# copy ESP file over to IMG
dd if=$_esp_file of=$IMG bs=$_sector_size count=$_esp_file_size_sectors conv=notrunc seek=$_esp_sector_start
# _sector_first is obtained by running
# ```
# sgdisk -F /tmp/img/qemu_aarch64.img
# ```
_sector_first=34816
# _sector_last is obtained by running
# ```
# sgdisk -n 2:34816:+256M -c 2:"Linux" -t 2:8300 /tmp/img/qemu_aarch64.img
# ```
_sector_last=1081310
# create linux partition
sgdisk -n 2:$_sector_first:$_sector_last -c 2:"Linux" -t 2:8300 $IMG
# find the first available loop device
_loop_dev=$(sudo losetup -f)
# attach loopback device to $IMG
losetup $_loop_dev $IMG
_disksize=$(sudo blockdev --getsize $_loop_dev)
dmsetup create diskimage --table "0 $_disksize linear $_loop_dev 0"
# ensure that /dev/mapper/diskimage exists
while [ ! -b /dev/mapper/diskimage ]
do
sleep 2
done
kpartx -a /dev/mapper/diskimage
# ensure that /dev/mapper/{diskimage1,diskimage2,diskimage3} exists
while [ ! -b /dev/mapper/diskimage1 ] || [ ! -b /dev/mapper/diskimage2 ]
do
sleep 2
done
# format vos_a partition
# NOTE: vos_a and vos_b will have a dependency with initramfs-init script
mkfs.ext4 -L vos_a -F /dev/mapper/diskimage2
# create mount point and mount vos_a
mkdir -p ${DESTDIR}/part/vos_a
mount -t ext4 /dev/mapper/diskimage2 $DESTDIR/part/vos_a
# Extract rootfs cpio archive into `.../part/vos_a`
$(cd ${DESTDIR}/part/vos_a; cpio -idm < ${DESTDIR}/output-dom0-builder/rootfs.cpio)
# copy over files needed in `/boot`
mkdir ${DESTDIR}/part/vos_a/boot
# linux
cp ${DESTDIR}/output-dom0-builder/initramfs-${_fullkver} ${DESTDIR}/part/vos_a/boot
cp ${DESTDIR}/output-dom0-builder/modloop-${_fullkver} ${DESTDIR}/part/vos_a/boot
cp ${DESTDIR}/output-dom0-builder/vmlinuz-${_fullkver} ${DESTDIR}/part/vos_a/boot
# xen
cp ${DESTDIR}/viryaos-xen/boot/xen-${_fullxenver} ${DESTDIR}/part/vos_a/boot
# dtb
cp ${DESTDIR}/viryaos.dtb ${DESTDIR}/part/vos_a/boot
# unmount
sync
# This fails for some reason.
# fstrim $DESTDIR/part/vos_a
umount $DESTDIR/part/vos_a
kpartx -d /dev/mapper/diskimage
dmsetup remove diskimage
losetup -d $_loop_dev