Skip to content

Instantly share code, notes, and snippets.

@m-bers
Last active July 6, 2024 19:05
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 Arch Linux
#!/bin/bash
# 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://geo.mirror.pkgbuild.com/images/latest/Arch-Linux-x86_64-cloudimg.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 Arch Linux conversion
cat << 'EOF' > /etc/systemd/system/arch_conversion.service
[Unit]
Description=Arch Linux Conversion
After=network.target
[Service]
Type=oneshot
ExecStart=/bin/bash /tmp/arch_conversion.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
# Create the conversion script
cat << 'EOF' > /tmp/arch_conversion.sh
#!/bin/bash
# Arch Linux conversion script
fuser -vkm /oldroot
umount -l /oldroot/
sleep 5
qemu-img convert -f qcow2 -O raw /Arch-Linux-x86_64-cloudimg.qcow2 /dev/nvme0n1
sleep 5
reboot
EOF
# Make the conversion script executable
chmod +x /tmp/arch_conversion.sh
# Enable and start the systemd service
systemctl enable arch_conversion.service
systemctl start arch_conversion.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment