3

Ok so let me try to explain it as thoroughly as possible. So I'm using chameleon cloud to reserve nodes on bare metal (~distant computers) and access them via ssh. These nodes run Ubuntu 20.04, and the ones I have access to only have 1 SSD, this is important as the project I am in requires only SSD's.

Root is on sda1 on the ssd, and sda1 occupies the whole ssd.

Basically I want to shrink sda1 (which contains root), to create another partition on the SSD and mount another filesystem for my project on it(this filesystem will change throughout the course of the project, btrfs, xfs, etc). So, the original EXT4 filesystem will stay on sda1, I need an extra partition with another filesystem. But since this is not a machine I have physical access to, I cannot use a usb stick or a CD to reinstall, etc.

I understand this might be confusing, maybe incorrect or incomplete. This is also probably not a new question, but I can't find satisfying answers that can adapt to my case. My project lead thinks that this is a solvable issue. Feel free to ask for more details (screenshots of commands, or whatever), correct errors, help me, anything is welcome.

I thank you very much for your time and help.

  • You can use resize2fs to shrink the filesystem. It requires a reboot to make that effective as far as I know. After the reboot, use fdisk or parted to modify the partition table on sda. If this server contains anything of value, take a backup before you manipulate partition sizes. – berndbausch Feb 26 '21 at 03:47
  • The version of parted on Ubuntu 20 may have a resizepart command, which can (I think) resize both the filesystem and the partition. This would make the task easier and less dangerous. – berndbausch Feb 26 '21 at 03:48
  • 2
    I don't think you can resize2fs to shrink a mounted partition, only grow it. – Philip Couling Feb 26 '21 at 08:08
  • @berndbausch, nope... resizepart only resizes the partition, not the filesystem. Also you can't use resize2fs to shrink a filesystem while it is mounted. – psusi Feb 26 '21 at 18:44
  • That's correct, my suggestion doesn't work at all on a remote server. – berndbausch Feb 26 '21 at 20:55
  • Hey guy who asked the question here, for some reason I had to create an acocunt so I can't comment yet. First of all thank you for your answers! Now the thing is, this isn't AWS. It's actually ChameleonCloud. It is very similar, but in such a smaller scale that I can't add an SSD at the moment. I'm stuck with the 1-SSD... – aveillon Feb 26 '21 at 15:13

2 Answers2

1

I did this in my Amazon Lightsail vps in order to convert it over to using LVM. First, you have to disable the cloud-init package from automatically growing the partition on each boot. IIRC, you need to comment out or delete the lines in /etc/cloud/cloud.cfg that reference growpart and resizefs. Then you can add these two scripts:

#!/bin/sh

PREREQ=""

prereqs() { echo "$PREREQ" }

case "${1:-}" in prereqs) prereqs exit 0 ;; esac

. /usr/share/initramfs-tools/hook-functions

copy the binary as early as possible

copy_exec /sbin/resize2fs /sbin copy_exec /sbin/e2fsck /sbin

Put that in /etc/initramfs-tools/hooks/resize-hook

#!/bin/sh

PREREQ=""

prereqs() { echo "$PREREQ" }

case $1 in

get pre-requisites

prereqs) prereqs exit 0 ;; esac

e2fsck -fy /dev/sda1 resize2fs /dev/sda1 9g

Change the 9g to whatever size you want the fs shrunk to and put that in /etc/initramfs-tools/scripts/local-premount/resize. Don't forget to chmod the two scripts +x, and rebuild your initramfs with update-initramfs.

After you reboot, verify that the filesystem did get shrunk (df), use the resizepart command in parted to shrink the partition. It' a good idea to leave an extra gb or so in the partition to make sure you don't shrink the partition smaller than the filesystem, then run resize2fs on it later to expand it to fill the whole new partition size.

Also after you verify that the filesystem was shrunk, remove the two above scripts and rebuild your initramfs again.

psusi
  • 17,303
0

Psusi's answer will work if performed correctly, but a small mistake could leave you with an unresponsive system. If that happens I believe you would lose anything you already have on your system. So if you chose that option you should take a snapshot first!


Reading into Chameleon cloud, it's built around the concept of images. You provision a server for some time, deploy an image to it and be careful to snapshot your server at the end of your lease or risk losing the lot. There are some hints on chameleon that most images are single partition inferring that some might not be.

If it were me I wouldn't edit the server in place. Instead I would download download the latest snapshot to my local machine (any linux or mac you have access to), edit it, upload it and then use it.

According to the documentation these images are QCOW2 images. Once downloaded these can be converted to a raw image with qemu-image convert. Modified with resize2fs / fdisk, repacked with qemu-image and uploaded back:

# Unpack
qemu-img convert -p -O raw base_image.qcow2 raw_image

Repack

qemu-img convert -p -O qcow2 raw_image new_image.qcow_2

  • Do they let you upload your own images? Lightsail only let you choose from a number of prebuilt images to create a new instance from, which is why I had to do it the cheeky way. They charge extra for creating external snapshots, so I got the feeling that they don't really want you using LVM inside the instance where you can make your own snapshots. – psusi Mar 01 '21 at 14:17
  • @psusi as mentioned before, this question has nothing to do with amazon. It's better to not use AWS as an analogy as it is profoundly different. There's documentation which discusses using images as a way to "save your work". It seems that the whole design of chameleon is based on the principle that you should be creating your own images. I can't speak for costs. Academic systems are frequently charged very differently compared to cloud providers. – Philip Couling Mar 01 '21 at 14:41
  • 1
    I mention the snapshots and cost only to explain one reason for me wanting to switch to LVM. The fact that you can't upload your own image to lightsail is why I had to do it in place. Whatever your host, if you have to do it in place, my answer is how. If you can upload your own image, then that is probably preferable, but the original question was how can you shrink the existing filesystem in place. – psusi Mar 05 '21 at 14:28