Skip to content

Instantly share code, notes, and snippets.

@mtelvers
Created April 30, 2025 15:03
Show Gist options
  • Select an option

  • Save mtelvers/1fe3571830d982eb8adbcf5a513edb2c to your computer and use it in GitHub Desktop.

Select an option

Save mtelvers/1fe3571830d982eb8adbcf5a513edb2c to your computer and use it in GitHub Desktop.
Remove a /dev/md0 root from a running system
- hosts: all
vars:
root_device: "/dev/sda2"
tasks:
- name: Install apt packages
apt:
name:
- debootstrap
state: present
- name: Fail and remove {{ root_device }} from the RAID5 set
shell: |
mdadm --fail /dev/md0 {{ root_device }}
mdadm --remove /dev/md0 {{ root_device }}
- name: format new root
community.general.filesystem:
fstype: ext4
dev: "{{ root_device }}"
force: true
- name: Find UUID of {{ root_device }}
shell: blkid -s UUID -o value {{ root_device }}
register: blkid
- name: Set a fact based upon the output
set_fact:
blkid: "{{ blkid.stdout_lines[0] }}"
- name: Temporary mount of the new root drive
shell: mount {{ root_device }} /mnt
- name: Install the system
shell: debootstrap noble /mnt
- name: Ensure directory exists
ansible.builtin.file:
path: /mnt/boot/efi
state: directory
- name: Copy files from original root
shell: rsync -a {{ item }} /mnt{{ item }}
with_items:
- /etc/hostname
- /etc/hosts
- /etc/fstab
- /etc/passwd
- /etc/shadow
- /etc/group
- /etc/default/grub
- /etc/sudoers.d/
- /etc/ssh/
- /etc/netplan/
- /etc/apt/
- /root/
- /home/
- name: Update the new /etc/fstab
ansible.builtin.lineinfile:
path: /mnt/etc/fstab
regexp: '/dev/[^ ]* / ext4 defaults'
line: "/dev/disk/by-uuid/{{ blkid }} / ext4 defaults 0 1"
owner: root
group: root
mode: '0644'
- name: Temporary mount from the live environment
shell: |
mount --make-private --rbind /dev /mnt/dev
mount --make-private --rbind /proc /mnt/proc
mount --make-private --rbind /sys /mnt/sys
mount /dev/sda1 /mnt/boot/efi
- name: Run apt update
shell: chroot /mnt apt update
- name: Install grub
shell: chroot /mnt apt install -y grub-efi-amd64 grub-efi-amd64-signed \
linux-image-generic shim-signed
- name: Install SSHD
shell: chroot /mnt apt install -y openssh-server
- name: Update grub
shell: chroot /mnt update-grub
- name: Install GRUB
shell: |
chroot /mnt grub-install --target=x86_64-efi --efi-directory=/boot/efi \
--bootloader-id=ubuntu --recheck --no-floppy
- name: Reboot
shell: |
reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment