curl > /dev/sda: How I made a Linux distro that runs wget | dd

131 points - today at 10:06 AM

Source

Comments

rwmj today at 12:00 PM
Unfortunately it's not safe as the kernel can still write to (what it thinks is) the old filesystem on the device, which will introduce corruption to the new disk image.

However a fun fact is that you can (do not actually do this!) boot a qemu VM from /dev/sda. You have to use an overlay (eg. qemu -drive snapshot=on flag) so that qemu won't write through to /dev/sda. I use this trick in supernested, a script I wrote that runs nested within nested within nested VMs ad infinitum until your hypervisor crashes. http://git.annexia.org/?p=supernested.git;a=blob;f=run-super...

e12e today at 7:56 PM
Nice series! Really takes me back to the days of Linux 1.x kernel, Lilo and trying to fit a kernel and initrd on a single floppy disk.

So ending up at:

> From a 292MB initramfs, we now have a 6.1MB initramfs, smaller than almost every other distro's initramfs and made entirely to run busybox wget dd.

Is pretty great achievement today - but way bigger than something that can fit on a floppy.

matja today at 12:08 PM
> How do you unmount your OS’s disk while keeping the OS running to be able to overwrite itself?

I went down a similar rabbit-hole myself, with the goal of safely replacing the Linux installation on a disk that a machine is already running from (e.g. replace a VPS's setup image with one of your own) without needing a KVM-style remote access tool to the console.

The problem there is if you directly modify the disk when a filesystem is mounted on that disk then all bets are off in terms of corruption of the filesystem that's already on there and also the filesystem(s) you're writing over the top.

My solution was to kexec into a new kernel+initramfs which has a DHCP client and cURL in it - that effectively stops any filesystem access while the image is being written over the disk, then to just reboot.

SamWhited today at 3:40 PM
Reminds me of the first company I worked for out of school.

We had a big drive with the source of truth image used to boot all our machines on it, and we added rsync to the init image. When each machine booted init would rsync everything from the storage box to the local machine. We'd keep the storage machine up to date and when we wanted to update other machines in the fleet we'd just do a reboot and it would sync up the latest files (provisioning for whatever each machine was supposed to do happened later, can't remember how that was handled now). The storage machine was running ZFS so we also took a snapshot before doing any rolling reboots, so if anything did go wrong you could just revert to the previous snapshot and reboot again as long as you didn't break the init image.

Sounds jank saying it out loud, but I don't remember it ever causing us any problems.

pzmarzly today at 1:03 PM
You will run into problems if destination drive has different sector size than your VM, as GPT header won't be aligned.

QEMU defaults to 512B sectors, which isn't true for many NVMe drives. There are some flags to change that. https://unix.stackexchange.com/a/722450

I think it should be possible to make an image with many headers at different locations, so that it works on all types of disks at once, but I don't think any tools do it for you by default.

alexellisuk today at 2:39 PM
This reminds me of netbooting workflows from things like MaaS, Tinkerbell, and Dan's old Plunder tool.

They'd netboot.. not mount the disks, then download an ISO/IMG and write it directly to the primary boot disk.

If netbooting is a heavy lift, why not boot into a custom initramfs you built, with i.e. dd/curl installed, and flash the disk that way, without mounting / at all? Then kexec/chroot into it?

I'd much prefer this as a way to provision Raspberry Pis.

M95D today at 11:52 AM
From the article:

> The OS may stop you from unmounting /dev/sda1, but it won’t stop you from writing to /dev/sda1 or /dev/sda even if there’s something mounted!

Not always true. There's a kernel config option that allows it. CONFIG_BLK_DEV_WRITE_MOUNTED

tosti today at 5:19 PM
If you have a swap partition, swapoff it and install there. Or at least a minimal kernel and initramfs. Set as default in grub and there you go.

Also, I once burned an iso straight from ftp using a fifo. I was low on disk space and really needed that CD. Worked fine because the Internet was already faster than the CDR.

PunchyHamster today at 12:48 PM
> Well, what can we try instead? > write to the mounted disk anyways. fuck you

Stupid penguin trick I learned: Add a file inside ramdisk (i use /dev/shm) as LVM PV.

pvmove off the hard drive

Boom, now your OS lives entirely in RAM

You can now even replace the hard disk, put a new one and migrate back.

Or migrate to network storage (nbd,iSCSI etc.), re-sequence disks into whatever RAID you need, and migrate back

Need to fix /boot after that tho, and probably make sure to not have power failure in meantime

klinch today at 3:10 PM
Sounds cursed. But I'm not judging, given that I use nixos-anywhere[0] on an almost weekly basis.

[0] https://github.com/nix-community/nixos-anywhere

dizhn today at 12:09 PM
Reminded me of how to install Alpine linux (which isn't available) on Oracle cloud over an ubuntu install. It uses dd and has the advantage of having a console.

I had found it in a github gist when I used it but here's a similar blog post.

https://alextsang.net/articles/20191006-063049/index.html

zoobab today at 3:49 PM
I used netcat and dd via the network to clone machines that has the same HDD:

https://support.tools/dd-over-netcat-clone-drive-remote-back...

But I like the curl approach very much!

em3rgent0rdr today at 5:28 PM
> "download a pre-prepared disk image directly to your disk"

Well not quite direct; the bits go through your RAM in between.

PunchyHamster today at 12:45 PM
and we've gone full circle, back in the day you installed os on diskettes like that!
indigodaddy today at 5:21 PM
NOC techs have been doing these tricks for tens of years
creantum today at 4:51 PM
Just because you can doesn’t mean you should.
anshulbasia27 today at 5:22 PM
Happened with me as well
ma2kx today at 1:48 PM
Why not just use netboot?
megous today at 4:46 PM
Instead of applying some sense to the problem, and using a solution that actually allows you to kill all running processes of the original distro at runtime, incl. getting rid of the original init process, to be able to pivot_root somewhere else amd umount the original system's filesystems and free the block device for re-installation, this ridiculous approach gets promoted to a front page, lol.
igtztorrero today at 3:33 PM
Can I run a Windows qcow2 disk imagen on a Contabo Vps ?
poppafuze today at 3:17 PM
Looking forward to seeing a device with a short image that has the string "404" on it.
irishcoffee today at 12:34 PM
I've been dd-ing A/B partitions for embedded yocto distributions for years and years. read-only-rootfs (/var/log is its own writable partition), dd the "other partition", sed fstab, reboot.

The neat part was the whole process kicked off when you scp'd the rootfs and inotifywait kicked off the whole process.

nkondratyk today at 4:11 PM
[dead]
Nahid890 today at 10:14 AM
[dead]