Skip to content

Instantly share code, notes, and snippets.

@ClashTheBunny
Forked from dotsh/reinstall_VPS_from_inside.sh
Last active February 19, 2023 18:34
Show Gist options
  • Save ClashTheBunny/8da6f422fc955bfd63ba1b03dd8254c8 to your computer and use it in GitHub Desktop.
Save ClashTheBunny/8da6f422fc955bfd63ba1b03dd8254c8 to your computer and use it in GitHub Desktop.
DIY install debian on Oracle Cloud Infrastructure ( Free Tier )
# should works on any cloud-init enabled hypervisor (openstack.. )
# start from a ubuntu minimal install
# we need to shrink down the used space to move it in a tmpfs of 700MB
while ! ssh -o ConnectTimeout=2 -p 22 ubuntu@$instanceIP; do sleep 1; done
# make sure we are on the highest kernel, so we can delete all the others ...
sudo apt update && sudo apt dist-upgrade -y && sudo apt install lsof && sudo reboot
sudo snap remove --purge oracle-cloud-agent && sudo snap remove --purge core18 && sudo snap remove --purge snapd && sudo sudo apt -y purge snapd && sudo rm -rf ~/snap /snap /var/cache/snapd
sudo apt purge -y $(dpkg-query -Wf '${Package}\n' | grep header) $(apt list --installed | grep -oP "^linux.*\d\d\d\d-oracle" | grep -v "$(uname -r)") linux-modules-extra-$(uname -r) lxc* lxd* vim* && sudo apt -y autoremove && sudo apt -y autoclean && sudo apt -y clean
sudo rm -rf /var/log/* /var/lib/apt/lists/*
# ok, the disk usage of / should be less than 600MB now
cd /
mount -t tmpfs -o size=700m tmpfs mnt
tar --one-file-system -c . | tar -C /mnt -x
mount --make-private -o remount,rw /
mount --move dev mnt/dev
mount --move proc mnt/proc
mount --move run mnt/run
mount --move sys mnt/sys
sed -i '/^[^#]/d;' mnt/etc/fstab
echo 'tmpfs / tmpfs defaults 0 0' >> mnt/etc/fstab
cd mnt
mkdir old_root
mount --make-private /
unshare -m
pivot_root . old_root
/usr/sbin/sshd -D -p 1022 &
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 1022 -j ACCEPT
# reconnect on port 1022:
while ! ssh -o ConnectTimeout=2 -p 1022 ubuntu@$instanceIP; do sleep 1; done
# now the root storage is the RAM
pkill agetty
pkill dbus-daemon
pkill atd
pkill iscsid
pkill rpcbind
pkill unattended-upgrades
kill 1
# check with "lsof /old_root" that there is no remaining process
umount -l /dev/sda1
# check :
df -h
lsblk
# the disk should be unmount
# now, just copy the debian cloud image on the disk.
# curl https://cdimage.debian.org/cdimage/openstack/current/debian-10-openstack-amd64.raw | dd of=/dev/sda bs=1M
# update :
# curl -L https://cdimage.debian.org/cdimage/openstack/testing/debian-testing-openstack-amd64.raw | dd of=/dev/sda bs=1M
# curl -L https://cdimage.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-amd64.raw | dd of=/dev/sda bs=1M
curl -L https://cloud.debian.org/cdimage/cloud/bullseye/latest/debian-11-generic-amd64.tar.xz | tar -OJxvf - disk.raw | dd of=/dev/sda bs=1M
sync
reboot
while ! ssh -o ConnectTimeout=2 -p 22 debian@$instanceIP; do sleep 1; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment