2

I want to connect to a Debian or CentOS device using SSH and modify its whole disk(just one 30 GB disk that has 5 partitions, 5th is Linux root) by writing a new raw disk image file and new partition table on it. My main goal is to make this remotely and automatically.

So I thought maybe I can somehow unmount the root partition and run Python while Linux runs on ram, is this possible?

Are there any alternative or better solution?

Added note: "They are not servers, they are like Raspberry Pi's that we have easy physical access, have SD card like disk that we manually remove, mount on our computers and write a new image then boot to Debian USB and install it on rest of the free space. I am trying to make this easier."

4
  • ...modify its whole disk ... writing a new raw disk image file and new partition table ... remotely and automatically I do hope you also have recovery processes ready when something unexpected happens and the remote server winds up thoroughly borked... Oct 10 '19 at 21:08
  • They are not servers, they are like Raspberry Pi's that we have easy physical access, have SD card like disk that we manually remove, mount on our computers and write a new image then boot to Debian USB and install it on rest of the free space. I am trying to make this easier.
    – Cagurtay
    Oct 11 '19 at 6:11
  • 1
    @GAD3R yes we can
    – Cagurtay
    Oct 11 '19 at 6:57
  • I guess I was confused about what I am trying to do, why do I need Python to run while I can do everything using bash and SSH. I will try davolfman's idea, thank you all.
    – Cagurtay
    Oct 14 '19 at 7:19
1

You can't do exactly as described.

However, many distributions use an intramfs image for very early boot. If you were to modify the init scripts in this image you could run your task before the filesystems are mounted. You would need to be extra clever to code a way for it to only happen once. All of this could be done remotely with sufficient access. Since CentOS and Debian use different systems for their initramfs's you'd need to do it twice. It would be a very good idea to work out the bugs on one system you have local access to before deploying on systems you don't. This kind of thing needs to work perfectly.

1

You can create a custom initramfs image for this purpose, look into Dracut modules (or initramfs-tools hooks, depending on which initramfs builder you prefer) for that. Providing a full Python environment will be challenging, I'd recommend sticking to shell tools like sfdisk if possible, although even your stock initramfs already has everything needed for writing out a full disk image. The easiest way to execute it would be kexec, but even if your disk image compresses very well (so make sure all unused regions are zeroed out!) the initramfs image containing it might still be too big to load on a running system. In this case you'll have to fiddle with your bootloader configuration, to effect the change on the next reboot. Some bootloaders provide "boot once" functionality; otherwise you'll have to revert the change after overwriting the disk. At least the Syslinux bootloader allows you to load multiple initramfs images, which are merged on boot, so you can make your normal image check for the presence of the replacement image file and write it out before it continues booting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.