mkimg.base.sh 7.81 KB
Newer Older
1
build_kernel() {
2
	local _flavor="$2" _modloopsign=
3 4
	shift 3
	local _pkgs="$@"
5
	[ "$modloop_sign" = "yes" ] && _modloopsign="--modloopsign"
6 7
	update-kernel \
		$_hostkeys \
8
		${_abuild_pubkey:+--apk-pubkey $_abuild_pubkey} \
9
		$_modloopsign \
10 11 12 13 14
		--media \
		--flavor "$_flavor" \
		--arch "$ARCH" \
		--package "$_pkgs" \
		--feature "$initfs_features" \
15
		--modloopfw "$modloopfw" \
16 17 18 19 20 21 22
		--repositories-file "$APKROOT/etc/apk/repositories" \
		"$DESTDIR"
}

section_kernels() {
	local _f _a _pkgs
	for _f in $kernel_flavors; do
23
		_pkgs="linux-$_f linux-firmware wireless-regdb $modloop_addons"
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
		for _a in $kernel_addons; do
			_pkgs="$_pkgs $_a-$_f"
		done
		local id=$( (echo "$initfs_features::$_hostkeys" ; apk fetch --root "$APKROOT" --simulate alpine-base $_pkgs | sort) | checksum)
		build_section kernel $ARCH $_f $id $_pkgs
	done
}

build_apks() {
	local _apksdir="$DESTDIR/apks"
	local _archdir="$_apksdir/$ARCH"
	mkdir -p "$_archdir"

	apk fetch --root "$APKROOT" --link --recursive --output "$_archdir" $apks
	if ! ls "$_archdir"/*.apk >& /dev/null; then
		return 1
	fi

	apk index \
		--description "$RELEASE" \
		--rewrite-arch "$ARCH" \
		--index "$_archdir"/APKINDEX.tar.gz \
		--output "$_archdir"/APKINDEX.tar.gz \
		"$_archdir"/*.apk
	abuild-sign "$_archdir"/APKINDEX.tar.gz
	touch "$_apksdir/.boot_repository"
}

section_apks() {
	[ -n "$apks" ] || return 0
	build_section apks $ARCH $(apk fetch --root "$APKROOT" --simulate --recursive $apks | sort | checksum)
}

build_apkovl() {
58
	local _host="$1" _script=
59
	msg "Generating $_host.apkovl.tar.gz"
60 61 62 63 64 65 66 67 68
	for _script in "$PWD"/"$apkovl" $HOME/.mkimage/$apkovl \
		$(readlink -f "$scriptdir/$apkovl"); do

		if [ -f "$_script" ]; then
			break
		fi
	done
	[ -n "$_script" ] || die "could not find $apkovl"
	(cd "$DESTDIR"; fakeroot "$_script" "$_host")
69 70 71 72 73 74 75 76 77 78 79
}

section_apkovl() {
	[ -n "$apkovl" -a -n "$hostname" ] || return 0
	build_section apkovl $hostname $(checksum < "$apkovl")
}

build_syslinux() {
	local _fn
	mkdir -p "$DESTDIR"/boot/syslinux
	apk fetch --root "$APKROOT" --stdout syslinux | tar -C "$DESTDIR" -xz usr/share/syslinux
80
	for _fn in isohdpfx.bin isolinux.bin ldlinux.c32 libutil.c32 libcom32.c32 mboot.c32; do
81 82 83 84 85 86
		mv "$DESTDIR"/usr/share/syslinux/$_fn "$DESTDIR"/boot/syslinux/$_fn || return 1
	done
	rm -rf "$DESTDIR"/usr
}

section_syslinux() {
87
	[ "$ARCH" = x86 -o "$ARCH" = x86_64 ] || return 0
88 89 90 91 92 93 94 95 96 97
	[ "$output_format" = "iso" ] || return 0
	build_section syslinux $(apk fetch --root "$APKROOT" --simulate syslinux | sort | checksum)
}

syslinux_gen_config() {
	[ -z "$syslinux_serial" ] || echo "SERIAL $syslinux_serial"
	echo "TIMEOUT ${syslinux_timeout:-20}"
	echo "PROMPT ${syslinux_prompt:-1}"
	echo "DEFAULT ${kernel_flavors%% *}"

98
	local _f
99 100
	for _f in $kernel_flavors; do
		if [ -z "${xen_params+set}" ]; then
101 102 103 104
			cat <<- EOF

			LABEL $_f
				MENU LABEL Linux $_f
105
				KERNEL /boot/vmlinuz-$_f
106 107 108 109
				INITRD /boot/initramfs-$_f
				DEVICETREEDIR /boot/dtbs
				APPEND $initfs_cmdline $kernel_cmdline
			EOF
110
		else
111
			cat <<- EOF
112

113 114 115
			LABEL $_f
				MENU LABEL Xen/Linux $_f
				KERNEL /boot/syslinux/mboot.c32
116
				APPEND /boot/xen.gz ${xen_params} --- /boot/vmlinuz-$_f $initfs_cmdline $kernel_cmdline --- /boot/initramfs-$_f
117
			EOF
118 119 120 121
		fi
	done
}

122
grub_gen_config() {
123
	local _f
124 125
	echo "set timeout=2"
	for _f in $kernel_flavors; do
126 127 128 129 130 131 132 133 134 135
		if [ -z "${xen_params+set}" ]; then
			cat <<- EOF

			menuentry "Linux $_f" {
				linux	/boot/vmlinuz-$_f $initfs_cmdline $kernel_cmdline
				initrd	/boot/initramfs-$_f
			}
			EOF
		else
			cat <<- EOF
136

137 138 139 140 141 142 143
			menuentry "Xen/Linux $_f" {
				multiboot2	/boot/xen.gz ${xen_params}
				module2		/boot/vmlinuz-$_f $initfs_cmdline $kernel_cmdline
				module2		/boot/initramfs-$_f
			}
			EOF
		fi
144 145 146
	done
}

147 148 149 150 151 152 153 154
build_syslinux_cfg() {
	local syslinux_cfg="$1"
	mkdir -p "${DESTDIR}/$(dirname $syslinux_cfg)"
	syslinux_gen_config > "${DESTDIR}"/$syslinux_cfg
}

section_syslinux_cfg() {
	syslinux_cfg=""
155 156 157
	if [ "$ARCH" = x86 -o "$ARCH" = x86_64 ]; then
		[ ! "$output_format" = "iso" ] || syslinux_cfg="boot/syslinux/syslinux.cfg"
	fi
158 159 160 161 162
	[ ! -n "$uboot_install" ] || syslinux_cfg="extlinux/extlinux.conf"
	[ -n "$syslinux_cfg" ] || return 0
	build_section syslinux_cfg $syslinux_cfg $(syslinux_gen_config | checksum)
}

163 164 165 166 167 168 169 170
build_grub_cfg() {
	local grub_cfg="$1"
	mkdir -p "${DESTDIR}/$(dirname $grub_cfg)"
	grub_gen_config > "${DESTDIR}"/$grub_cfg
}

grub_gen_earlyconf() {
	cat <<- EOF
171
	search --no-floppy --set=root --label "alpine-${profile_abbrev:-$PROFILE} $RELEASE $ARCH"
172 173 174 175
	set prefix=(\$root)/boot/grub
	EOF
}

176
build_grub_efi() {
177 178
	local _format="$1"
	local _efi="$2"
179

180
	# Prepare grub-efi bootloader
181 182
	mkdir -p "$DESTDIR/efi/boot"
	grub_gen_earlyconf > "$WORKDIR/grub_early.$3.cfg"
183
	grub-mkimage \
184
		--config="$WORKDIR/grub_early.$3.cfg" \
185
		--prefix="/boot/grub" \
186
		--output="$DESTDIR/efi/boot/$_efi" \
187 188 189 190 191
		--format="$_format" \
		--compression="xz" \
		$grub_mod
}

192 193 194
section_grubieee1275() {
	[ "$ARCH" = ppc64le ] || return 0
	[ "$output_format" = "iso" ] || return 0
195
	kernel_cmdline="$kernel_cmdline console=hvc0"
196 197 198 199

	build_section grub_cfg boot/grub/grub.cfg $(grub_gen_config | checksum)
}

200
section_grub_efi() {
201 202 203
	[ -n "$grub_mod" ] || return 0
	local _format _efi
	case "$ARCH" in
204 205 206 207 208
	aarch64)_format="arm64-efi";  _efi="bootaa64.efi" ;;
	arm*)	_format="arm-efi";    _efi="bootarm.efi"  ;;
	x86)	_format="i386-efi";   _efi="bootia32.efi" ;;
	x86_64) _format="x86_64-efi"; _efi="bootx64.efi"  ;;
	*)	return 0 ;;
209 210 211
	esac

	build_section grub_cfg boot/grub/grub.cfg $(grub_gen_config | checksum)
212
	build_section grub_efi $_format $_efi $(grub_gen_earlyconf | checksum)
213 214
}

215 216
create_image_iso() {
	local ISO="${OUTDIR}/${output_filename}"
217 218 219 220 221 222 223 224 225 226 227 228 229 230
	local _isolinux
	local _efiboot

	if [ -e "${DESTDIR}/boot/syslinux/isolinux.bin" ]; then
		# isolinux enabled
		_isolinux="
			-isohybrid-mbr ${DESTDIR}/boot/syslinux/isohdpfx.bin
			-eltorito-boot boot/syslinux/isolinux.bin
			-eltorito-catalog boot/syslinux/boot.cat
			-no-emul-boot
			-boot-load-size 4
			-boot-info-table
			"
	fi
231 232 233 234 235 236
	if [ -e "${DESTDIR}/efi" -a -e "${DESTDIR}/boot/grub" ]; then
		# Create the EFI boot partition image
		mformat -i ${DESTDIR}/boot/grub/efi.img -C -f 1440 ::
		mcopy -i ${DESTDIR}/boot/grub/efi.img -s ${DESTDIR}/efi ::

		# Enable EFI boot
237 238 239 240 241
		if [ -z "$_isolinux" ]; then
			# efi boot only
			_efiboot="
				-efi-boot-part
				--efi-boot-image
242
				-e boot/grub/efi.img
243 244 245 246 247 248
				-no-emul-boot
				"
		else
			# hybrid isolinux+efi boot
			_efiboot="
				-eltorito-alt-boot
249
				-e boot/grub/efi.img
250 251 252 253 254
				-no-emul-boot
				-isohybrid-gpt-basdat
				"
		fi
	fi
255 256

	if [ "$ARCH" = ppc64le ]; then
257
		grub-mkrescue --output ${ISO} ${DESTDIR} -follow-links \
258
			-sysid LINUX \
259
			-volid "alpine-${profile_abbrev:-$PROFILE} $RELEASE $ARCH"
260
	else
261 262 263 264 265 266 267 268 269 270
		if [ "$ARCH" = s390x ]; then
			printf %s "$initfs_cmdline $kernel_cmdline " > ${WORKDIR}/parmfile
			for _f in $kernel_flavors; do
				mk-s390-cdboot -p ${WORKDIR}/parmfile \
					-i ${DESTDIR}/boot/vmlinuz-$_f \
					-r ${DESTDIR}/boot/initramfs-$_f \
					-o ${DESTDIR}/boot/merged.img
			done
			iso_opts="$iso_opts -no-emul-boot -eltorito-boot boot/merged.img"
		fi
271 272 273 274 275
		xorrisofs \
			-quiet \
			-output ${ISO} \
			-full-iso9660-filenames \
			-joliet \
276
			-rational-rock \
277
			-sysid LINUX \
278
			-volid "alpine-${profile_abbrev:-$PROFILE} $RELEASE $ARCH" \
279 280 281 282 283 284
			$_isolinux \
			$_efiboot \
			-follow-links \
			${iso_opts} \
			${DESTDIR}
	fi
285 286 287 288 289 290 291
}

create_image_targz() {
	tar -C "${DESTDIR}" -chzf ${OUTDIR}/${output_filename} .
}

profile_base() {
292
	kernel_flavors="vanilla"
293
	initfs_cmdline="modules=loop,squashfs,sd-mod,usb-storage quiet"
294
	initfs_features="ata base bootchart cdrom squashfs ext4 mmc raid scsi usb virtio"
295
	modloop_sign=yes
296 297 298 299
	grub_mod="all_video disk part_gpt part_msdos linux normal configfile search search_label efi_gop fat iso9660 cat echo ls test true help gzio"
	case "$ARCH" in
	x86*) grub_mod="$grub_mod multiboot2 efi_uga";;
	esac
300
	apks="alpine-base alpine-mirrors busybox kbd-bkeymaps chrony e2fsprogs haveged network-extras openssl openssh tzdata"
301
	apkovl=
302 303
	hostname="alpine"
}