Last active
April 22, 2023 22:11
-
-
Save m-bers/bd8e0791e36d335293582faa131f2b9a to your computer and use it in GitHub Desktop.
Convert running Ubuntu system to Arch Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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/ | |
# 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 | |
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 | |
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/vda | |
sleep 5 | |
reboot | |
EOF | |
chmod +x /tmp/arch_conversion.sh | |
systemctl daemon-reload | |
systemctl enable arch_conversion.service | |
# Start the service | |
systemctl start arch_conversion.service |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment