Skip to content

Instantly share code, notes, and snippets.

@lidgnulinux
Last active May 10, 2024 00:33
Show Gist options
  • Save lidgnulinux/071d7536c26a84c41eee847365ac2fdb to your computer and use it in GitHub Desktop.
Save lidgnulinux/071d7536c26a84c41eee847365ac2fdb to your computer and use it in GitHub Desktop.

Using dracut as default on archlinux instead of mkinitcpio ? Here a simple guide.

PS : you can check it on archwiki actually.

Installation.

Assumming you still don't have dracut and mkinitcpio, you can install dracut using pacman.

# pacman -S dracut (other packages blablabla)

Scripts for removing and installing vmlinuz and initramfs.

We need to make 2 scripts for removing and installing vmlinuz and initramfs when kernel upgrade is available. We can place them under /usr/local/bin and call them as dracut-install.sh and dracut-remove.sh.

  • dracut install.sh (for installing vmlinuz and generate initramfs).

    #!/usr/bin/env bash
    
    args=('--force' '--no-hostonly-cmdline')
    
    while read -r line; do
    	if [[ "$line" == 'usr/lib/modules/'+([^/])'/pkgbase' ]]; then
    		read -r pkgbase < "/${line}"
    		kver="${line#'usr/lib/modules/'}"
    		kver="${kver%'/pkgbase'}"
    
    		install -Dm0644 "/${line%'/pkgbase'}/vmlinuz" "/boot/vmlinuz-${pkgbase}"
    		dracut "${args[@]}" --hostonly "/boot/initramfs-${pkgbase}.img" --kver "$kver"
    		dracut "${args[@]}" --no-hostonly "/boot/initramfs-${pkgbase}-fallback.img" --kver "$kver"
    	fi
    done
    
  • dracut-remove.sh (for removing vmlinuz and initramfs).

    #!/usr/bin/env bash
    
    while read -r line; do
    	if [[ "$line" == 'usr/lib/modules/'+([^/])'/pkgbase' ]]; then
    		read -r pkgbase < "/${line}"
    		rm -f "/boot/vmlinuz-${pkgbase}" "/boot/initramfs-${pkgbase}.img" "/boot/initramfs-${pkgbase}-fallback.img"
    	fi
    done
    

We should not forget to set their permission to rwxr-xr-x. We can do it by using chmod command :

# chmod 755 /usr/local/bin/dracut-install.sh
# chmod 755 /usr/local/bin/dracut-remove.sh

Pacman hooks for dracut.

We need to make two hooks so pacman will automatically run the previous scripts (dracut-install.sh and dracut-remove.sh) when kernel upgrade is available. We can call them as 90-dracut-install.hook and 60-dracut-remove.hook. We can place them under /etc/pacman.d/hooks/.

  • 90-dracut-install.hook

    [Trigger]
    Type = Path
    Operation = Install
    Operation = Upgrade
    Target = usr/lib/modules/*/pkgbase
    
    [Action]
    Description = Updating linux initcpios (with dracut!)...
    When = PostTransaction
    Exec = /usr/local/bin/dracut-install.sh
    Depends = dracut
    NeedsTargets
    
  • 60-dracut-remove.hook

    [Trigger]
    Type = Path
    Operation = Remove
    Target = usr/lib/modules/*/pkgbase
    
    [Action]
    Description = Removing linux initcpios...
    When = PreTransaction
    Exec = /usr/local/bin/dracut-remove.sh
    NeedsTargets
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment