You know that feeling when everything goes well for a while … until it doesn’t ? been using Fedora Workstation 25 and quite satisfied with it, util one late evening when I decided to clean up old kernels and removed every last kernel available. Here’s how to restore a Fedora Workstation system after you’ve removed every kernel available 🙂 this applies to a Fedora 25 on an EFI system and GRUB 2 – so 2017!

Normally, at this stage you’d boot the borked system with an USB stick and simply reinstall the kernel packages. This works fine on a Debian/Ubuntu system, but Fedora seems to be a bit more annoying – after chrooting I’d get:

[root@localhost-live ~]# yum -y --disablerepo='*' install kernel-*
Redirecting to '/usr/bin/dnf -y --disablerepo=* install kernel-4.10.8-200.fc25.x86_64.rpm kernel-core-4.10.8-200.fc25.x86_64.rpm kernel-headers-4.10.8-200.fc25.x86_64.rpm kernel-modules-4.10.8-200.fc25.x86_64.rpm' (see 'man yum2dnf')

error: Failed to initialize NSS library
Traceback (most recent call last):
  File "/usr/bin/dnf", line 57, in <module>
    from dnf.cli import main
  File "/usr/lib/python3.5/site-packages/dnf/__init__.py", line 31, in <module>
    import dnf.base
  File "/usr/lib/python3.5/site-packages/dnf/base.py", line 30, in <module>
    from dnf.yum import history
  File "/usr/lib/python3.5/site-packages/dnf/yum/history.py", line 31, in <module>
    import dnf.rpm.miscutils
  File "/usr/lib/python3.5/site-packages/dnf/rpm/__init__.py", line 22, in <module>
    from . import transaction
  File "/usr/lib/python3.5/site-packages/dnf/rpm/transaction.py", line 14, in <module>
    import rpm
  File "/usr/lib64/python3.5/site-packages/rpm/__init__.py", line 39, in <module>
    from rpm.transaction import *
  File "/usr/lib64/python3.5/site-packages/rpm/transaction.py", line 5, in <module>
    from rpm._rpm import ts as TransactionSetCore
ImportError: cannot import name 'ts'

Well, f*** you, I don’t remember my package manager needing a ton of Python dependencies to work ! a few more straces and copious amounts of cursing later, here’s the step by step solution:

1. Boot from a Fedora Workstation 25 Live USB stick. Make sure you click the “Try” option to get your live USB stick mounted read-write, otherwise you won’t be able to install packages on the live USB stick. Make sure the system has network connectivity.

2. Install the nscd package and its dependencies:

# sudo su
# dnf -y install nscd

3. Start nscd (this is done in order to have the nscd socket available in chroot, rpm/dnf won’t work without this – STUPID STUPID STUPID):

# systemctl start nscd

After this, a socket in /var/run/named/socket should exist.

4. Prepare the chroot mount point and get inside the chroot:

# mkdir /mnt/sysimage
# mkdir /mnt/sysimage/{boot,proc,run,sys,dev}
# mount [your root partition] /mnt/sysimage
# mount [your boot partition] /mnt/sysimage/boot
# mount --bind /dev /mnt/sysimage/dev/
# mount --bind /sys /mnt/sysimage/sys/
# mount --bind /proc /mnt/sysimage/proc/
# mount --bind /var/run /mnt/sysimage/var/run
# cp /etc/resolv.conf /mnt/sysimage/etc/resolv.conf

5. Reinstall the packages by downloading the kernel-* RPMs with wget and installing them manually OR with a “dnf -y reinstall kernel-*” (didn’t tried this one).

6. Your Grub is now broken too, so we need to rescue it as well. At the GRUB console find your boot and root partitions with “ls” and then:

> set prefix=(hdX,Y) 
> set root=(hdX,Z)
> linuxefi /boot/vmlinuz-4.xxxxxx root=(hdX,Z)
> initramfs /boot/initramfs-4.xxxxxx
> boot

7. After booting successfully, update GRUB with:

# grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg
Tagged with:
 

3 Responses to how to rescue a fedora workstation after removing all available kernels

  1. DVA says:

    dnf ftw. yum is so outdated :).

    Multumesc frumos de sugestie, am folosit materialul sa repar o masina cu Fedora Server pe care am pus in /etc/dnf/dnf.conf linia installonly_limit=0.

Leave a Reply

Your email address will not be published. Required fields are marked *


Anunţă-mă când sunt comentarii noi. Te poţi abona fără a comenta.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Set your Twitter account name in your settings to use the TwitterBar Section.