Skip to content

Instantly share code, notes, and snippets.

@m-bers
Last active October 29, 2024 20:16
Show Gist options
  • Save m-bers/bd8e0791e36d335293582faa131f2b9a to your computer and use it in GitHub Desktop.
Save m-bers/bd8e0791e36d335293582faa131f2b9a to your computer and use it in GitHub Desktop.
Convert running Ubuntu system to Debian 12
#!/bin/bash
set -x
# Install prereqs - Ubuntu
apt-get update && apt-get -y install qemu-utils
# Stop services
df -TH > mounted_fs
systemctl list-units --type=service --state=running --no-pager --no-legend | awk '!/ssh/ {print $1}' | xargs systemctl stop
# Stop DNS from breaking
echo "nameserver 8.8.8.8" > /etc/resolv.conf
# Copy old root to tmpfs
umount -a
mkdir /tmp/tmproot
mount none /tmp/tmproot -t tmpfs
mkdir /tmp/tmproot/{proc,sys,usr,var,run,dev,tmp,home,oldroot}
cp -ax /{bin,etc,mnt,sbin,lib,lib64} /tmp/tmproot/
cp -ax /usr/{bin,sbin,lib,lib64} /tmp/tmproot/usr/
cp -ax /var/{lib,local,lock,opt,run,spool,tmp} /tmp/tmproot/var/
cp -Rax /home /tmp/tmproot/
# Ensure sudo and its dependencies are copied
mkdir -p /tmp/tmproot/usr/libexec/sudo
cp -ax /usr/libexec/sudo/* /tmp/tmproot/usr/libexec/sudo/
# Copy new image to tmpfs
wget https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2 -P /tmp/tmproot
# Switch root to tmpfs
mount --make-rprivate /
pivot_root /tmp/tmproot /tmp/tmproot/oldroot
# Move system mounts to tmpfs
for i in dev proc sys run; do mount --move /oldroot/$i /$i; done
# Restart services within the ramroot
systemctl restart sshd
systemctl list-units --type=service --state=running --no-pager --no-legend | awk '!/ssh/ {print $1}' | xargs systemctl restart
systemctl daemon-reexec
# Create and enable systemd service for Debian Linux conversion
cat << 'EOF' > /etc/systemd/system/debian_conversion.service
[Unit]
Description=Debian Linux Conversion
After=network.target
[Service]
Type=oneshot
ExecStart=/bin/bash /tmp/debian_conversion.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
# Create the conversion script
cat << 'EOF' > /tmp/debian_conversion.sh
#!/bin/bash
# Arch Linux conversion script
fuser -vkm /oldroot
umount -l /oldroot/
sleep 5
qemu-img convert -f qcow2 -O raw /debian-12-genericcloud-amd64.qcow2 /dev/nvme0n1
sleep 5
reboot
EOF
# Make the conversion script executable
chmod +x /tmp/debian_conversion.sh
# Enable and start the systemd service
systemctl enable debian_conversion.service
systemctl start debian_conversion.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment