%bcond_with systemd

%define dracutlibdir %{_prefix}/lib/dracut
%bcond_without doc

# We ship a .pc file but don't want to have a dep on pkg-config. We
# strip the automatically generated dep here and instead co-own the
# directory.
%global __requires_exclude pkg-config

# Variables must be defined
%define with_nbd                0

%define dist_free_release 1

Name: dracut
Summary: Initramfs generator using udev
Version: 059
Release: %{dist_free_release}%{?_dist_release}%{?with_systemd:.systemd}
Group: system,admin-tools
Vendor: Project Vine
Distribution: Vine Linux

# The entire source code is GPLv2+
# except install/* which is LGPLv2+
License: GPLv2+ and LGPLv2+
URL: https://dracut.wiki.kernel.org/
# Source can be generated by
# http://git.kernel.org/?p=boot/dracut/dracut.git;a=snapshot;h=%%{version};sf=tgz
#Source0: http://www.kernel.org/pub/linux/utils/boot/dracut/dracut-%{version}.tar.xz
Source0: https://github.com/dracutdevs/dracut/archive/refs/tags/%{version}.tar.gz#/dracut-%{version}.tar.gz
Source1: https://www.gnu.org/licenses/lgpl-2.1.txt
Source2000: vine.conf
Source2001: vine-module-setup.sh
Source2002: vine-usb-pre-udev.sh
Source2003: ChangeLog.fedora

Patch1000: dracut-050-resolve-symlink.patch

BuildRequires: bash git
BuildRequires: kmod-devel >= 15

%if 0%{?fedora} || 0%{?rhel}
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
BuildRequires: pkgconfig
%endif
%if 0%{?fedora}
BuildRequires: bash-completion
%endif

%if 0%{?suse_version}
BuildRoot: %{_tmppath}/%{name}-%{version}-build
%endif

%if %{with doc}
BuildRequires: docbook-style-xsl docbook-dtds libxslt
BuildRequires: asciidoc
%endif

# no "provides", because dracut does not offer
# all functionality of the obsoleted packages
Obsoletes: mkinitrd < 6.0.94
Obsoletes: mkinitrd-devel < 6.0.94
Obsoletes: nash < 6.0.94

%if 0%{?fedora} > 16 || 0%{?rhel} > 6
BuildRequires: systemd-units
%endif

Obsoletes: dracut-kernel < 005
Provides:  dracut-kernel = %{version}-%{release}

Obsoletes: dracut < 030
Obsoletes: dracut-norescue < 030
Provides:  dracut-norescue = %{version}-%{release}

Requires: bash >= 4
Requires: coreutils
Requires: cpio
Requires: filesystem >= 2.1.0
Requires: findutils
Requires: grep
Requires: kmod
Requires: sed
Requires: xz
Requires: xz-lzma-compat
Requires: zstd
Requires: gzip

Requires: procps
Requires: udev > 166
Requires: util-linux-ng >= 2.21

Conflicts: mdadm < 3.2.6-14

%description
dracut contains tools to create a bootable initramfs for 2.6 Linux kernels.
Unlike existing implementations, dracut does hard-code as little as possible
into the initramfs. dracut contains various modules which are driven by the
event-based udev. Having root on MD, DM, LVM2, LUKS is supported as well as
NFS, iSCSI, NBD, FCoE with the dracut-network package.


%package network
Summary: dracut modules to build a dracut initramfs with network support
Group: system
%if 0%{?_module_build}
# In the module-build-service, we have pieces of dracut provided by different
# modules ("base-runtime" provides most functionality, but we need
# dracut-network in "installer". Since these two modules build with separate
# dist-tags, we need to reduce this strict requirement to ignore the dist-tag.
Requires: %{name} >= %{version}-%{dist_free_release}
%else
Requires: %{name} = %{version}-%{release}
%endif
Requires: iputils
Requires: iproute
Requires: dhclient
Obsoletes: dracut-generic < 008
Provides:  dracut-generic = %{version}-%{release}

%description network
This package requires everything which is needed to build a generic
all purpose initramfs with network support with dracut.


%if 0%{?fedora} || 0%{?rhel} >= 6 || 0%{?suse_version}
%package fips
Summary: dracut modules to build a dracut initramfs with an integrity check
Group: system,security
Requires: %{name} = %{version}-%{release}
Requires: hmaccalc
%if 0%{?rhel} > 5
# For Alpha 3, we want nss instead of nss-softokn
Requires: nss
%else
Requires: nss-softokn
%endif
Requires: nss-softokn-freebl

%description fips
This package requires everything which is needed to build an
initramfs with dracut, which does an integrity check.
%endif

%package fips-aesni
Summary: dracut modules to build a dracut initramfs with an integrity check with aesni-intel
Group: system,security
Requires: %{name}-fips = %{version}-%{release}

%description fips-aesni
This package requires everything which is needed to build an
initramfs with dracut, which does an integrity check and adds the aesni-intel kernel module.


%package caps
Summary: dracut modules to build a dracut initramfs which drops capabilities
Group: system
Requires: %{name} = %{version}-%{release}
Requires: libcap

%description caps
This package requires everything which is needed to build an
initramfs with dracut, which drops capabilities.


%package live
Summary: dracut modules to build a dracut initramfs with live image capabilities
Group: system
%if 0%{?_module_build}
# See the network subpackage comment.
Requires: %{name} >= %{version}-%{dist_free_release}
%else
Requires: %{name} = %{version}-%{release}
%endif
Requires: %{name}-network = %{version}-%{release}
Requires: tar gzip coreutils bash device-mapper curl

%description live
This package requires everything which is needed to build an
initramfs with dracut, with live image capabilities, like Live CDs.


%package config-generic
Summary: dracut configuration to turn off hostonly image generation
Group: system
Requires: %{name} = %{version}-%{release}
Obsoletes: dracut-nohostonly < 030
Provides:  dracut-nohostonly = %{version}-%{release}

%description config-generic
This package provides the configuration to turn off the host specific initramfs
generation with dracut and generates a generic image by default.


%package config-rescue
Summary: dracut configuration to turn on rescue image generation
Group: system
Requires: %{name} = %{version}-%{release}
Obsoletes: dracut < 030

%description config-rescue
This package provides the configuration to turn on the rescue initramfs
generation with dracut.


%package tools
Summary: dracut tools to build the local initramfs
Group: admin-tools
Requires: %{name} = %{version}-%{release}

%description tools
This package contains tools to assemble the local initrd and host configuration.


%debug_package


%prep
%autosetup -n %{name}-%{version} -S git_am

cp %{SOURCE1} .
cp %{SOURCE2003} .


%build
%configure \
%if %{defined _unitdir}
            --systemdsystemunitdir=%{_unitdir} \
%endif
            --bashcompletiondir=$(pkg-config \
            --variable=completionsdir bash-completion) \
            --libdir=%{_prefix}/lib \
%if %{without doc}
            --disable-documentation \
%endif
            ${NULL}

make %{?_smp_mflags}


%install
rm -rf -- $RPM_BUILD_ROOT

make %{?_smp_mflags} install \
     DESTDIR=$RPM_BUILD_ROOT \
     libdir=%{_prefix}/lib

echo "DRACUT_VERSION=%{version}-%{release}" > $RPM_BUILD_ROOT/%{dracutlibdir}/dracut-version.sh

%if 0%{?fedora} == 0 && 0%{?rhel} == 0 && 0%{?suse_version} == 0
rm -fr -- $RPM_BUILD_ROOT/%{dracutlibdir}/modules.d/01fips
rm -fr -- $RPM_BUILD_ROOT/%{dracutlibdir}/modules.d/02fips-aesni
%endif

# we do not support dash in the initramfs
rm -fr -- $RPM_BUILD_ROOT/%{dracutlibdir}/modules.d/00dash

# we do not support mksh in the initramfs
rm -fr -- $RPM_BUILD_ROOT/%{dracutlibdir}/modules.d/00mksh

# remove gentoo specific modules
rm -fr -- $RPM_BUILD_ROOT/%{dracutlibdir}/modules.d/50gensplash

%if %{defined _unitdir}
# with systemd IMA and selinux modules do not make sense
rm -fr -- $RPM_BUILD_ROOT/%{dracutlibdir}/modules.d/96securityfs
rm -fr -- $RPM_BUILD_ROOT/%{dracutlibdir}/modules.d/97masterkey
rm -fr -- $RPM_BUILD_ROOT/%{dracutlibdir}/modules.d/98integrity
%endif

mkdir -p $RPM_BUILD_ROOT/boot/dracut
mkdir -p $RPM_BUILD_ROOT/var/lib/dracut/overlay
mkdir -p $RPM_BUILD_ROOT%{_localstatedir}/log
touch $RPM_BUILD_ROOT%{_localstatedir}/log/dracut.log
mkdir -p $RPM_BUILD_ROOT%{_sharedstatedir}/initramfs

install -m 0644 %{SOURCE2000} %{buildroot}%{dracutlibdir}/dracut.conf.d/01-dist.conf

%if 0%{?fedora} || 0%{?rhel} || 0%{?suse_version}
install -m 0644 dracut.conf.d/fips.conf.example $RPM_BUILD_ROOT%{dracutlibdir}/dracut.conf.d/40-fips.conf
%endif

echo 'hostonly="no"' > $RPM_BUILD_ROOT%{dracutlibdir}/dracut.conf.d/02-generic-image.conf
echo 'dracut_rescue_image="yes"' > $RPM_BUILD_ROOT%{dracutlibdir}/dracut.conf.d/02-rescue.conf

%if 0%{?fedora} || 0%{?rhel} || 0%{?suse_version}
> $RPM_BUILD_ROOT/etc/system-fips
%endif

# make to load EHCI driver at first
mkdir -p %{buildroot}%{dracutlibdir}/modules.d/70ehci_first
install -m755 %{SOURCE2001} %{buildroot}%{dracutlibdir}/modules.d/70ehci_first/module-setup.sh
install -m755 %{SOURCE2002} %{buildroot}%{dracutlibdir}/modules.d/70ehci_first/usb-pre-udev.sh

# fix path to rngd
sed -i -e 's|/usr/sbin/|/sbin/|' \
	%{buildroot}%{dracutlibdir}/modules.d/06rngd/rngd.service

# drop a unshipped file
rm -f %{buildroot}%{_mandir}/man8/mkinitrd-suse.8*


%clean
rm -rf -- $RPM_BUILD_ROOT


%files
%defattr(-,root,root,0755)
%if %{with doc}
%doc README* AUTHORS dracut.html
%endif
%{!?_licensedir:%global license %%doc}
%license COPYING lgpl-2.1.txt
%doc ChangeLog.fedora
%{_bindir}/dracut
%dir %{_datadir}/bash-completion
%dir %{_datadir}/bash-completion/completions
%{_datadir}/bash-completion/completions/dracut
%{_datadir}/bash-completion/completions/lsinitrd
%{_bindir}/lsinitrd
%dir %{dracutlibdir}
%dir %{dracutlibdir}/modules.d
%{dracutlibdir}/dracut-functions.sh
%{dracutlibdir}/dracut-init.sh
%{dracutlibdir}/dracut-functions
%{dracutlibdir}/dracut-version.sh
%{dracutlibdir}/dracut-logger.sh
%{dracutlibdir}/dracut-initramfs-restore
%{dracutlibdir}/dracut-install
%{dracutlibdir}/dracut-util
%{dracutlibdir}/skipcpio
%config(noreplace) %{_sysconfdir}/dracut.conf
%{dracutlibdir}/dracut.conf.d/01-dist.conf
%dir %{_sysconfdir}/dracut.conf.d
%dir %{dracutlibdir}/dracut.conf.d
%dir %{_datadir}/pkgconfig
%{_datadir}/pkgconfig/dracut.pc

%if %{with doc}
%{_mandir}/man8/dracut.8*
%{_mandir}/man8/*service.8*
%{_mandir}/man1/lsinitrd.1*
%{_mandir}/man7/dracut.kernel.7*
%{_mandir}/man7/dracut.cmdline.7*
%{_mandir}/man7/dracut.modules.7*
%{_mandir}/man7/dracut.bootup.7*
%{_mandir}/man5/dracut.conf.5*
%endif

%{dracutlibdir}/modules.d/00bash
%{dracutlibdir}/modules.d/00systemd
%{dracutlibdir}/modules.d/00systemd-network-management
%{dracutlibdir}/modules.d/00warpclock
%{dracutlibdir}/modules.d/01systemd-ac-power
%{dracutlibdir}/modules.d/01systemd-ask-password
%{dracutlibdir}/modules.d/01systemd-coredump
%{dracutlibdir}/modules.d/01systemd-hostnamed
%{dracutlibdir}/modules.d/01systemd-initrd
%{dracutlibdir}/modules.d/01systemd-integritysetup
%{dracutlibdir}/modules.d/01systemd-journald
%{dracutlibdir}/modules.d/01systemd-ldconfig
%{dracutlibdir}/modules.d/01systemd-modules-load
%{dracutlibdir}/modules.d/01systemd-pcrphase
%{dracutlibdir}/modules.d/01systemd-portabled
%{dracutlibdir}/modules.d/01systemd-pstore
%{dracutlibdir}/modules.d/01systemd-repart
%{dracutlibdir}/modules.d/01systemd-resolved
%{dracutlibdir}/modules.d/01systemd-rfkill
%{dracutlibdir}/modules.d/01systemd-sysext
%{dracutlibdir}/modules.d/01systemd-sysctl
%{dracutlibdir}/modules.d/01systemd-sysusers
%{dracutlibdir}/modules.d/01systemd-timedated
%{dracutlibdir}/modules.d/01systemd-timesyncd
%{dracutlibdir}/modules.d/01systemd-tmpfiles
%{dracutlibdir}/modules.d/01systemd-udevd
%{dracutlibdir}/modules.d/01systemd-veritysetup
%{dracutlibdir}/modules.d/03modsign
%{dracutlibdir}/modules.d/03rescue
%{dracutlibdir}/modules.d/04watchdog
%{dracutlibdir}/modules.d/04watchdog-modules
%{dracutlibdir}/modules.d/05busybox
%{dracutlibdir}/modules.d/06dbus-broker
%{dracutlibdir}/modules.d/06dbus-daemon
%{dracutlibdir}/modules.d/06rngd
%{dracutlibdir}/modules.d/09dbus
%{dracutlibdir}/modules.d/10i18n
%{dracutlibdir}/modules.d/30convertfs
%{dracutlibdir}/modules.d/45url-lib
%{dracutlibdir}/modules.d/50drm
%{dracutlibdir}/modules.d/50plymouth
%{dracutlibdir}/modules.d/62bluetooth
# vine
%{dracutlibdir}/modules.d/70ehci_first

%{dracutlibdir}/modules.d/80lvmmerge
%{dracutlibdir}/modules.d/80lvmthinpool-monitor
%{dracutlibdir}/modules.d/80test
%{dracutlibdir}/modules.d/80test-makeroot
%{dracutlibdir}/modules.d/80test-root
%{dracutlibdir}/modules.d/90btrfs
%{dracutlibdir}/modules.d/90crypt
%{dracutlibdir}/modules.d/90dm
%{dracutlibdir}/modules.d/90dmraid
%{dracutlibdir}/modules.d/90kernel-modules
%{dracutlibdir}/modules.d/90kernel-modules-extra
%{dracutlibdir}/modules.d/90lvm
%{dracutlibdir}/modules.d/90mdraid
%{dracutlibdir}/modules.d/90multipath
%{dracutlibdir}/modules.d/90nvdimm
%{dracutlibdir}/modules.d/90overlayfs
%{dracutlibdir}/modules.d/90ppcmac
%{dracutlibdir}/modules.d/90qemu
%{dracutlibdir}/modules.d/91crypt-gpg
%{dracutlibdir}/modules.d/91crypt-loop
	
%{dracutlibdir}/modules.d/91fido2
%{dracutlibdir}/modules.d/91pcsc
%{dracutlibdir}/modules.d/91pkcs11
%{dracutlibdir}/modules.d/91tpm2-tss
%{dracutlibdir}/modules.d/95debug
%{dracutlibdir}/modules.d/95fstab-sys
%{dracutlibdir}/modules.d/95lunmask
%{dracutlibdir}/modules.d/95nvmf
%{dracutlibdir}/modules.d/95resume
%{dracutlibdir}/modules.d/95rootfs-block
%{dracutlibdir}/modules.d/95terminfo
%{dracutlibdir}/modules.d/95udev-rules
%{dracutlibdir}/modules.d/95virtfs
%{dracutlibdir}/modules.d/95virtiofs
%ifarch s390 s390x
%{dracutlibdir}/modules.d/80cms
%{dracutlibdir}/modules.d/81cio_ignore
%{dracutlibdir}/modules.d/91zipl
%{dracutlibdir}/modules.d/95dasd
%{dracutlibdir}/modules.d/95dasd_mod
%{dracutlibdir}/modules.d/95dasd_rules
%{dracutlibdir}/modules.d/95dcssblk
%{dracutlibdir}/modules.d/95qeth_rules
%{dracutlibdir}/modules.d/95zfcp
%{dracutlibdir}/modules.d/95zfcp_rules
%else
%exclude %{dracutlibdir}/modules.d/80cms
%exclude %{dracutlibdir}/modules.d/81cio_ignore
%exclude %{dracutlibdir}/modules.d/91zipl
%exclude %{dracutlibdir}/modules.d/95dasd
%exclude %{dracutlibdir}/modules.d/95dasd_mod
%exclude %{dracutlibdir}/modules.d/95dasd_rules
%exclude %{dracutlibdir}/modules.d/95dcssblk
%exclude %{dracutlibdir}/modules.d/95qeth_rules
%exclude %{dracutlibdir}/modules.d/95zfcp
%exclude %{dracutlibdir}/modules.d/95zfcp_rules
%endif
%if %{undefined _unitdir}
%{dracutlibdir}/modules.d/96securityfs
%{dracutlibdir}/modules.d/97masterkey
%{dracutlibdir}/modules.d/98integrity
%endif
%{dracutlibdir}/modules.d/97biosdevname
%{dracutlibdir}/modules.d/98ecryptfs
%{dracutlibdir}/modules.d/98pollcdrom
%{dracutlibdir}/modules.d/98selinux
%{dracutlibdir}/modules.d/98syslog
%{dracutlibdir}/modules.d/98dracut-systemd
%{dracutlibdir}/modules.d/98usrmount
%{dracutlibdir}/modules.d/99base
%{dracutlibdir}/modules.d/99memstrack
%{dracutlibdir}/modules.d/99fs-lib
%{dracutlibdir}/modules.d/99shutdown
%{dracutlibdir}/modules.d/99squash

%attr(0644,root,root) %ghost %config(missingok,noreplace) %{_localstatedir}/log/dracut.log
%dir %{_sharedstatedir}/initramfs
%if %{defined _unitdir}
%{_unitdir}/dracut-shutdown.service
%{_unitdir}/dracut-shutdown-onfailure.service
%{_unitdir}/sysinit.target.wants/dracut-shutdown.service
%{_unitdir}/dracut-cmdline.service
%{_unitdir}/dracut-initqueue.service
%{_unitdir}/dracut-mount.service
%{_unitdir}/dracut-pre-mount.service
%{_unitdir}/dracut-pre-pivot.service
%{_unitdir}/dracut-pre-trigger.service
%{_unitdir}/dracut-pre-udev.service
%{_unitdir}/initrd.target.wants/dracut-cmdline.service
%{_unitdir}/initrd.target.wants/dracut-initqueue.service
%{_unitdir}/initrd.target.wants/dracut-mount.service
%{_unitdir}/initrd.target.wants/dracut-pre-mount.service
%{_unitdir}/initrd.target.wants/dracut-pre-pivot.service
%{_unitdir}/initrd.target.wants/dracut-pre-trigger.service
%{_unitdir}/initrd.target.wants/dracut-pre-udev.service
%endif
%{_prefix}/lib/kernel/install.d/50-dracut.install

%files network
%defattr(-,root,root,0755)
%{dracutlibdir}/modules.d/01systemd-networkd
%{dracutlibdir}/modules.d/35connman
%{dracutlibdir}/modules.d/35network-legacy
%{dracutlibdir}/modules.d/35network-manager
%{dracutlibdir}/modules.d/35network-wicked
%{dracutlibdir}/modules.d/40network
%{dracutlibdir}/modules.d/90kernel-network-modules
%{dracutlibdir}/modules.d/95fcoe
%{dracutlibdir}/modules.d/95iscsi
%{dracutlibdir}/modules.d/90qemu-net
%{dracutlibdir}/modules.d/95cifs
%{dracutlibdir}/modules.d/95nbd
%{dracutlibdir}/modules.d/95nfs
%{dracutlibdir}/modules.d/95ssh-client
%{dracutlibdir}/modules.d/45ifcfg
%ifarch s390 s390x
%{dracutlibdir}/modules.d/95znet
%else
%exclude %{dracutlibdir}/modules.d/95znet
%endif
%{dracutlibdir}/modules.d/95fcoe-uefi
%{dracutlibdir}/modules.d/99uefi-lib

%if 0%{?fedora} || 0%{?rhel} || 0%{?suse_version}
%files fips
%defattr(-,root,root,0755)
%{dracutlibdir}/modules.d/01fips
%{dracutlibdir}/dracut.conf.d/40-fips.conf
%config(missingok) /etc/system-fips

%files fips-aesni
%defattr(-,root,root,0755)
%{dracutlibdir}/modules.d/02fips-aesni
%endif

%files caps
%defattr(-,root,root,0755)
%{dracutlibdir}/modules.d/02caps

%files live
%defattr(-,root,root,0755)
%{dracutlibdir}/modules.d/99img-lib
%{dracutlibdir}/modules.d/90dmsquash-live
%{dracutlibdir}/modules.d/90dmsquash-live-autooverlay
%{dracutlibdir}/modules.d/90dmsquash-live-ntfs
%{dracutlibdir}/modules.d/90livenet

%files tools
%defattr(-,root,root,0755)

%if %{with doc}
%doc %{_mandir}/man8/dracut-catimages.8*
%endif

%{_bindir}/dracut-catimages
%dir /boot/dracut
%dir /var/lib/dracut
%dir /var/lib/dracut/overlay

%files config-generic
%defattr(-,root,root,0755)
%{dracutlibdir}/dracut.conf.d/02-generic-image.conf

%files config-rescue
%defattr(-,root,root,0755)
%{dracutlibdir}/dracut.conf.d/02-rescue.conf
%{_prefix}/lib/kernel/install.d/51-dracut-rescue.install


%changelog
* Sun May 14 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 059-1
- new upstream release.

* Tue Apr 06 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 053-1
- new upstream release.
- added O:mkinitrd, nash.

* Fri Apr 03 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 050-3
- added support for recursive symlink of /sbin/init.

* Thu Apr 02 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 050-2
- fixed path to rngd.
- truncated %%changelog: old logs are moved to ChangeLog.fedora.

* Wed Apr 01 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 050-1
- new upstream release.

* Fri Sep 13 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 049-2
- imported Patch100 and 101 from rawhide.
- added R:xz-lzma-compat.

* Tue Jan 01 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 049-1
- new upstream release.

* Fri Sep 15 2017 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 046-10
- fixed release number.

* Thu Sep 14 2017 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 046-9
- imported warpclock scripts from openSUSE.

* Tue Sep 12 2017 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 046-8
- initial build for Vine Linux.
