Category Archives: Linux

How I Successfully Shrunk an AWS EC2 Root EBS Volume (Even Though AWS Says You Can’t)

AWS doesn’t natively support shrinking an EBS volume — especially not a root volume. But with a little low-level Linux work and some patience, I successfully reduced a 200GB root volume down to just 20GB on a live EC2 instance.

Here’s the complete step-by-step process I followed, in case you ever need to do the same.

The Situation

– OS: Ubuntu on EC2
– Root volume: 200GB (over-allocated)
– Goal: Shrink to 20GB without rebuilding the system from scratch
– Challenge: AWS doesn’t allow shrinking EBS volumes directly

The Solution: Copy and Boot from a New, Smaller Volume

Attach a New Blank Volume

Create a 20GB EBS volume and attach it to the instance (default naming will usually be /dev/nvme1n1).

Identify the New Device

Run:
lsblk

Confirm the new volume appears as /dev/nvme1n1 and has no partitions.

Partition and Format the New Volume

sudo parted /dev/nvme1n1 --script mklabel gpt
sudo parted /dev/nvme1n1 --script mkpart ESP fat32 1MiB 201MiB
sudo parted /dev/nvme1n1 --script set 1 boot on
sudo parted /dev/nvme1n1 --script mkpart primary ext4 201MiB 1225MiB
sudo parted /dev/nvme1n1 --script mkpart primary ext4 1225MiB 100%


sudo mkfs.vfat -F32 /dev/nvme1n1p1
sudo mkfs.ext4 /dev/nvme1n1p2
sudo mkfs.ext4 /dev/nvme1n1p3

Mount the New Volume

sudo mkdir -p /mnt/newroot/boot/efi
sudo mount /dev/nvme1n1p3 /mnt/newroot
sudo mkdir /mnt/newroot/boot
sudo mount /dev/nvme1n1p2 /mnt/newroot/boot
sudo mount /dev/nvme1n1p1 /mnt/newroot/boot/efi

Copy the Existing System

sudo rsync -aAXH --progress \
--exclude={"/mnt/*","/proc/*","/sys/*","/dev/*","/run/*","/tmp/*","/media/*","/lost+found"} \
/ /mnt/newroot/

Chroot and Rebuild Boot Environment

sudo mount --bind /dev /mnt/newroot/dev
sudo mount --bind /sys /mnt/newroot/sys
sudo mount --bind /proc /mnt/newroot/proc
sudo mount --bind /run /mnt/newroot/run
sudo chroot /mnt/newroot


Inside chroot:
blkid /dev/nvme1n1p3
nano /etc/default/grub


Set:
GRUB_CMDLINE_LINUX="root=UUID=your-root-uuid-here"

Then run:
update-grub
update-initramfs -c -k all
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=ubuntu --recheck --no-floppy

Exit and Unmount Everything

exit
for dir in run dev sys proc; do sudo umount /mnt/newroot/$dir; done
sudo umount /mnt/newroot/boot/efi
sudo umount /mnt/newroot/boot
sudo umount /mnt/newroot

Swap the Volumes

Stop the instance
Detach the old 200GB volume
Detach the 20GB volume
Reattach the 20GB volume as /dev/nvme0n1

Boot and Verify

After starting the instance:
lsblk
df -h /
cat /etc/fstab


Ensure / is mounted from nvme0n1p3 and fstab UUIDs match.

Clean Up and Celebrate

Delete the old volume to reduce cost
Optionally create an AMI of the new setup

How To Add Swap Space on Debian 11/12

Swap memory is a location on hard disk to be used as Memory by the operating system. When the operating systems detects that main memory is low and required more RAM to run applications properly it check for swap space and transfer files there. In general terms, swap is a part of the hard disk used as RAM on the system.

This tutorial will help you to Add Swap on Debian 11/12 Linux system.

How to Create Swap in Debian 11

Use the below steps to create and enable Swap memory on your Debian 11 system via command line.

  1. Check that no Swap memory is enabled on your system. You can see the swap memory details by running the following commands.
sudo swapon -s
free -m
Check Available Swap Memory
Check Available Swap Memory
  1. Now, create a file to use as swap in your system system. Make sure you have enough free disk available to create new file. Also keep the swap just up to 2x of the memory on your system. My Debian system have 2GB of main memory. So we will create a swapfile of 4GB in size.
sudo fallocate -l 4G /swapfilechmod 600 /swapfile
  1. Now use the mkswap command to convert file to use for swap memory.
sudo mkswap /swapfile
  1. Then activate the swap memory on your system.
sudo swapon /swapfile
  1. You have successfully added swap memory to your system. Execute one of the below commands to view current active swap memory on your system:
sudo swapon -s 
free -m 
Swap is Added to your System

Make Swap Permanent

After running above commands, Swap memory is added to your system and operating system can use when required. But after reboot of system swap will deactivate again.

You can make it permanent by appending the following entry in /etc/fstab file. Edit fstab file in editor:

vim /etc/fstab 

and add below entry to end of file:

/swapfile   none    swap    sw    0   0
Add swap in fatab

Save file and close. Now Swap memory will remain activate after system reboots.

Configure Swappiness

Now change the swappiness kernel parameter as per your requirement. It tells the system how often system utilize this swap area.

Edit /etc/sysctl.conf file:

sudo vim /etc/sysctl.conf

append following configuration to end of file

vm.swappiness=10

Now reload the sysctl configuration file

sudo sysctl -p 

Conclusion

Now the operating system can use swap memory in case of low physical memory. In this tutorial, you have learned to create and enable Swap memory on Debian 11/12 Linux system.

Thanks to Rahul for the original article found here: How To Add Swap Space on Debian 11 – TecAdmin

Create private network using proxmox on ovh

On your proxmox server edit /etc/interfaces and add a vmbr1 interface like so

load kernel module dummy. modprobe dummy

auto lo
iface lo inet loopback

auto vmbr0
iface vmbr0 inet static
        address  167.114.101.88
        netmask  255.255.255.0
        gateway  167.114.101.254
        broadcast  167.114.101.255
        network 167.114.101.0
        bridge_ports eth0
        bridge_stp off
        bridge_fd 0


# Internal Network
auto vmbr1
iface vmbr1 inet static
    address 10.0.1.1/23
    bridge_ports dummy0
    bridge_stp off
    bridge_fd 0
    post-up /sbin/iptables -t nat -A POSTROUTING -o vmbr0 -j MASQUERADE
    pre-down /sbin/iptables -t nat -D POSTROUTING -o vmbr0 -j MASQUERADE

Bring up the new vmbr1 interface

ifdown vmbr1 && ifup vmbr1

Then enable ip forwarding in /etc/sysctl.conf

net.ipv4.ip_forward=1

Load the new values

sysctl -p /etc/sysctl.conf

On your vm configure networking like so

auto eth0
iface eth0 inet static
	address 10.0.1.100
	netmask 255.255.254.0
	gateway 10.0.1.1