File image-qemu.spec of Package image-qemu

#
# spec file for package image-qemu
#
# Copyright (c) 2023 SUSE LLC
# Copyright (c) 2024 Siemens AG
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#

# XXX: bs_worker is smart enough to disable preinstallimages when
# it sees the needsbinariesforbuild tag. Osc build needs
# --nopreinstallimage though

# needsbinariesforbuild
# needsrootforbuild

Name:           image-qemu
Version:        0
Release:        0
Summary:        Image for qemu
License:        MIT
Source0:        solve_and_install
Source1:        boot.env.in
Source2:        kernel.its
Source3:        creatediskimage
Source4:        bootargs.env
Source5:        makerepo
Source6:        functions
### host deps
BuildRequires:  u-boot-tools
BuildRequires:  device-tree-compiler
BuildRequires:  dosfstools
BuildRequires:  mtools
BuildRequires:  fdisk
BuildRequires:  jq
BuildRequires:  uuid-runtime
BuildRequires:  initramfs-tools
#BuildRequires:  debootstrap
BuildRequires:  bubblewrap
### image deps
BuildRequires:  --ignoreignore--
BuildRequires:  systemd
BuildRequires:  login
BuildRequires:  udev
BuildRequires:  libudev1
BuildRequires:  libsystemd0
BuildRequires:  zstd
BuildRequires:  libsolv-tools
BuildRequires:  dash
BuildRequires:  base-files
BuildRequires:  coreutils
BuildRequires:  util-linux
BuildRequires:  busybox-static
BuildRequires:  dbus-broker
BuildRequires:  lsof strace procps
BuildRequires:  apt
#BuildRequires:  usr-is-merged
BuildRequires:  tzdata
#
BuildRequires:  initrd linux-cip
%ifarch arm64 aarch64
BuildRequires:  u-boot-qemuarm64
%endif

%description

%prep
cat > packages.t <<EOF
system %_build_arch %{?debian:deb}%{!?debian:rpm}
repo repo 50 solv packages.solv
solverflags ignorerecommended
solverflags addalreadyrecommended
job install name systemd
job install name udev
job install name login
job install name dbus-broker
job install name coreutils
job install name util-linux
job install name dash
job install name base-files
job install name lsof
job install name strace
job install name procps
job install name busybox-static
job install name apt
#job install name usr-is-merged
job install name tzdata
#
job install name libpam-runtime
# libpam-runtime needs perl for pam-auth-update but does not
# depend on it
job install name perl
#
job install name initrd
job install name linux-cip
%ifarch arm64 aarch64
job install name u-boot-qemuarm64
%endif
EOF
cat packages.t

%build
for i in %{SOURCE0} %{SOURCE1} %{SOURCE2} %{SOURCE3} %{SOURCE4} %{SOURCE5} %{SOURCE6}; do
	cp "$i" .
done
chmod 755 solve_and_install

%if 0
dist=$(lsb_release --codename --short)
arch=$(dpkg-architecture -qDEB_BUILD_ARCH)
#pkgs="$(set -- $(./solve_and_install packages); IFS=, eval echo '"$*"')"

chmod 755 makerepo
./makerepo "$dist" "$arch"
# dunno, this shit doesn't work
/usr/sbin/debootstrap \
	--keep-debootstrap-dir \
	--verbose \
	--no-check-gpg \
	--variant=minbase \
	--arch="${arch}" \
	--merged-usr \
	"$dist" \
	"$PWD/rootfs" "file:///.build.binaries"

# only solve actually
./solve_and_install packages > rootfs/.pkglist

echo done
%else
./solve_and_install packages rootfs
env DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
	chroot rootfs /usr/sbin/pam-auth-update --force
%endif

#
# hardcoded to make reproducible
export ROOTFS_UUID=97c5aabe-54f4-4aef-8066-87782a478681
#
# create boot parition
#
for kernel in /lib/modules/*/Image; do
	kv="${kernel%%/*}"
	kv="${kv##*/}"
	ln -s "$kernel" kernel
	break
done
[ -n "$kv" ]
# xxx: need to add modules or maybe just build here
ln -s /usr/lib/initrd/30-systemd initrd
#
if [ -n "$KEYNAME" ]; then
	cat > empty.dts <<-EOF
	/dts-v1/;

	/ {
		signature {
		};
	};
	EOF
	dtc -o "$KEYNAME.dtb" empty.dts
	rm empty.dts
fi
mkdir -p efi/FIT
sed -e "s|@FIT@|FIT/Linux-$kv.fit|" < boot.env.in > boot.env
if [ -n "$ROOTFS_UUID" ]; then
	sed -i -e "/^setenv bootargs/s/\$/ root=UUID=$ROOTFS_UUID/" bootargs.env
fi
mkimage -f auto -A arm64 -T script -C none -n 'U-Boot script' -d boot.env efi/boot.scr
mkimage_args="-v -f kernel.its -r"
[ -z "$KEYNAME" ] || mkimage_args="$mkimage_args -K $KEYNAME.dtb -k keys -k $KEYNAME"
#mkimage_args+=(-g foo -k keys -o sha256,rsa2048)
mkimage $mkimage_args efi/FIT/Linux-"$kv".fit
du -sh efi/FIT

#
# configure rootfs
#
echo "uninitialized" > rootfs/etc/machine-id
# no root password
if [ -e "rootfs/etc/passwd" ]; then
  sed -e '/^root:x:/s/x//' rootfs/etc/passwd
else
  echo 'root::0:0:root:/root:/bin/sh' > rootfs/etc/passwd
fi
mkdir -p rootfs/etc/systemd/system/basic.target.wants
ln -s /usr/lib/systemd/system/debug-shell.service rootfs/etc/systemd/system/basic.target.wants/debug-shell.service
SYSTEMD_LOG_LEVEL=info systemd-sysusers --root="$PWD/rootfs"
SYSTEMD_LOG_LEVEL=info systemctl --root="$PWD/rootfs" preset-all
# XXX
chmod 400 rootfs/etc/shadow rootfs/etc/gshadow
mkdir -p rootfs/var/log/journal
ln -s /usr/share/zoneinfo/UTC rootfs/etc/localtime
#
#r() {
#  bwrap --clearenv --unshare-all --bind "$1" / --proc /proc \
#    --dev /dev --tmpfs /tmp --tmpfs /run --uid 0 --gid 0 "$@"
#}
#r $PWD/rootfs /usr/sbin/pam-auth-update --force

#
# assemble the bits to OBS
#
out=~/debbuild/OTHER/%{name}-%{version}-%{release}.raw
chmod 755 creatediskimage
./creatediskimage "$out" efi rootfs
echo "Compressing ..."
xz -0 "$out"
du -h "$out.xz"
#
cp %{_prefix}/lib/u-boot/u-boot-qemuarm64.bin "${out%%.raw}.u-boot"

%install

%files

%changelog

openSUSE Build Service is sponsored by