-
-
Save 4abhinavjain/893ec13c651bee08088c8f4661998952 to your computer and use it in GitHub Desktop.
# should works on any cloud-init enabled hypervisor (openstack.. ) | |
# start from a normal ubuntu 20.04 install as minimal was not available for ARM64 | |
# Since ARM64 machines has higher RAM, Shrinking is desired but not necessary. Instead we will increase tmpfs to 1700MB | |
# Getting root (if sudo -i doesn't work then set a root password beforehand using 'sudo passwd root' | |
sudo -i | |
# 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 | |
# snap removal didn't work so proceed without it. | |
sudo snap remove --purge oracle-cloud-agent && sudo snap remove --purge core18 | |
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/* | |
# use df to check the size. It should be now ~1100MB. If it is higher than 1700 MB then increase the size below | |
# make sure you have root here | |
cd / | |
mount -t tmpfs -o size=1700m 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 | |
# commands below open 1022 port a ssh port. Please ensure the port you select is open under Oracle's VNIC | |
/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: I kept the old putty session (on port 22) open and it didn't disconnect | |
# 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 ; for me lsblk showed a /dev/sda1 partiion but it went well. | |
# now, just copy the debian cloud image on the disk. | |
# I wanted an ARM64 image and Debian 10 , therefore I used this one https://cloud.debian.org/cdimage/cloud/buster/latest/debian-10-generic-arm64.tar.xz | |
curl -L https://cloud.debian.org/cdimage/cloud/buster/latest/debian-10-generic-arm64.tar.xz | tar -OJxvf - disk.raw | dd of=/dev/sda bs=1M | |
sync | |
reboot | |
# reboot command didn't work and returned an error "running in chroot, ignoring request" ; I tried exiting as they told here (https://bbs.archlinux.org/viewtopic.php?id=184076) but it was of no use | |
# I forced rebooted it from the Oracle instances' GUI and it seemed stuck on "STOPPING." But after an hour I noticed that the VM was rebooted and running. | |
# I was able to login with the same public key as original ubuntu, but the username that worked was debian | |
# SUCCESS!! I checked with lsb_release -a and it showed Debian 10 (earlier it was showing ubuntu) | |
# Additional things: You may want to reinstall the Oracle cloud agent that was removed in the beginning. |
Still working in 2025. I used an Intel VM (VM.Standard3.Flex) because there were no ARM ones left (could not provision one due to capacity limits), and started with a Ubuntu-22.04-Minimal, then switched to debian-12-generic-amd64. Seems to be working great.
but its not free tier right ? i though free tier only support arm ?
Yes, you’re correct, Intel is not currently free.
VM.Standard.E2.1.Micro
is free.
Available Shapes
Micro instances (AMD processor): All tenancies get up to two Always Free VM instances using the VM.Standard.E2.1.Micro [shape](https://docs.oracle.com/iaas/Content/Compute/References/computeshapes.htm), which has an AMD processor.
OCI Ampere A1 Compute instances (Arm processor): All tenancies get the first 3,000 OCPU hours and 18,000 GB hours per month for free for VM instances using the VM.Standard.A1.Flex [shape](https://docs.oracle.com/iaas/Content/Compute/References/computeshapes.htm), which has an [Arm processor](https://docs.oracle.com/iaas/Content/Compute/References/arm.htm). For Always Free tenancies, this is equivalent to 4 OCPUs and 24 GB of memory.
Any tricks for getting around timeouts? When doing tar --one-file-system -c . | tar -C /mnt -x
the ssh
session closes after while and I have to reboot the system.
I have tried multiple things on a VM.Standard.E2.1.Micro machine and after the tar
command it just dies down and I have to force reboot. The sda1 size is around ~1.1GB when starting the tar operation so tempfs is large enough.
just a tip by my trial with Oracle AMD free instance VM.Standard.E2.1.Micro, which only has 1GB RAM, you can actually use swap for the temporary chrooted os. tmpfs needs 1,7G in the guide above, which we don't have.
After the removal of the logs, I did the following (I was in Ubuntu 22 minimal)
mkdir /mnt2
fallocate -l 2G /mnt2/swapfile (mine was nearly 1,8GB in size)
chmod 600 /mnt2/swapfile
mkswap /mnt2/swapfile
swapon /mnt2/swapfile
tar --one-file-system --exclude=./mnt2/swapfile -c . | tar -C /mnt -x
full process at the end :)
I didn't change anything else in the process, also the fstab, which should be invalid at this point.
The only thing what I noticed, that after the remounts in /mnt most of the commands didn't work anymore, so I did a chroot /mnt after the pivot_root to continue. worked. I used the bookworm version for amd64, because I used the VPS for openmptcp, which requires max Debian 12. Don't forget to use the "debian" user, when login with ssh-key.
https://cloud.debian.org/cdimage/cloud/bookworm/20250814-2204/debian-12-generic-amd64-20250814-2204.tar.xz
Yes the process is still finishing, even the swapfile is overwritten by the dd :-) ignore the error, that the umount of sda didn't work. It is a surgery on the heart, but it works.
I did an additional server now with Debian trixie.
su -
snap remove --purge oracle-cloud-agent && snap remove --purge core18
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* && apt -y autoremove && apt -y autoclean && apt -y clean
...
rm -rf /var/log/* /var/lib/apt/lists/*
mkdir /mnt2
fallocate -l 2G /mnt2/swapfile
chmod 600 /mnt2/swapfile
mkswap /mnt2/swapfile
swapon /mnt2/swapfile
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=a5d43c04-ade7-47be-b211-575eea636ad5
cd /
tar --one-file-system --exclude=./mnt2/swapfile -c . | tar -C /mnt -x
tar: ./mnt: file changed as we read it
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
chroot /mnt
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 1022 -j ACCEPT
/usr/sbin/sshd -D -p 1022 &
pkill agetty
pkill dbus-daemon
pkill atd
pkill iscsid
pkill rpcbind
pkill unattended-upgrades
kill 1
[1] 1976091
The following commands can be skipped, it's just for checking the disk/partitions
umount -l /dev/sda1
> umount: /dev/sda1: not mounted.
df -h
> Filesystem Size Used Avail Use% Mounted on
> tmpfs 480M 0 480M 0% /dev/shm
> tmpfs 96M 2.3M 94M 3% /run
> tmpfs 5.0M 0 5.0M 0% /run/lock
> tmpfs 96M 4.0K 96M 1% /run/user/1001
> efivarfs 256K 21K 231K 9% /sys/firmware/efi/efivars
lsblk
> NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
> loop0 7:0 0 49.3M 1 loop
> loop1 7:1 0 50.9M 1 loop
> sda 8:0 0 46.6G 0 disk
> ├─sda1 8:1 0 46.5G 0 part
> ├─sda14 8:14 0 4M 0 part
> └─sda15 8:15 0 106M 0 part
write Debian to the disk.
curl -L https://cloud.debian.org/cdimage/cloud/trixie/20250814-2204/debian-13-generic-amd64-20250814-2204.tar.xz | tar -OJxvf - disk.raw | dd of=/dev/sda bs=1M
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 374 100 374 0 0 1357 0 --:--:-- --:--:-- --:--:-- 1360
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0disk.raw
100 303M 100 303M 0 0 2019k 0 0:02:33 0:02:33 --:--:-- 1824k
0+68353 records in
0+68353 records out
3221225472 bytes (3.2 GB, 3.0 GiB) copied, 186.029 s, 17.3 MB/s
sync
=> force reboot via Oracle website.
Connect to your VPS again, now in debian, with the same SSH key:
ssh-keygen -f '/home/mylocaluser/.ssh/known_hosts' -R '<my oracle vps ip'
ssh -i mykeyfile debian@myvpsip
Checkin the VPS, also don't forget to change the password of debian/root:
root@ubuntu-x86:~# uname -ar
Linux ubuntu-x86 6.12.41+deb13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.41-1 (2025-08-12) x86_64 GNU/Linux
root@ubuntu-x86:~# lsb_release -r
Release: 13
(yes I know, uname showed already deb13, just additional)
Still working in 2025. I used an Intel VM (VM.Standard3.Flex) because there were no ARM ones left (could not provision one due to capacity limits), and started with a Ubuntu-22.04-Minimal, then switched to debian-12-generic-amd64. Seems to be working great.