文章摘要
这篇文章介绍了一种极简的Linux系统安装方法:通过curl命令直接将磁盘镜像下载并写入硬盘设备文件(如/dev/sda),无需先保存到临时文件。作者解释了这种方法的可行性源于Unix"一切皆文件"的设计理念,并指出该方法在大多数EFI机器上也适用,因为固件能自动识别新的EFI系统分区。这是关于Linux启动阶段进行非常规操作的四篇系列文章的开篇。
文章总结
标题:通过curl > /dev/sda直接安装Linux发行版
主要内容概述:
本文探讨了一种非常规的Linux系统安装方法:通过curl命令直接将磁盘镜像写入硬盘设备(如/dev/sda),无需传统安装流程或中间文件。作者以树莓派刷机为例,逐步简化步骤,最终实现“从网络直接写入磁盘”的极简操作,并深入探讨了其原理与潜在问题。
关键细节:
核心命令
bash curl https://example.com/os.img > /dev/sda- 利用Unix“一切皆文件”的特性,直接向磁盘设备文件写入数据。
- 适用于EFI系统,因固件会自动检测新EFI分区。
动机
- 避免支付额外存储费用(如Contabo VPS的存储服务)。
- 对
curl | sh争议的幽默回应,探索技术极限。
技术验证
- 风险尝试:在运行中的系统直接覆盖自身磁盘,导致77.8%进度时崩溃。
- 解决方案:通过救援镜像(如Arch/NixOS安装环境)启动,安全写入磁盘。
扩展应用
- 支持压缩镜像(如
gunzip管道传输)。 - 远程SSH写入示例:
bash gzip -vc disk.img | ssh user@host 'gunzip -vc | dd of=/dev/sda'
- 支持压缩镜像(如
自动化思考
- 提出“是否无需救援盘?”的设想:将工具加载到内存后卸载磁盘,实现原地替换(后续系列文章展开)。
精简部分:
- 移除网站导航栏、页脚版权声明等非技术内容。
- 省略部分幽默旁白(如“连Caligula用户都不校验SHA256”)及多协议实现的调侃。
- 合并重复的“逐步简化命令”示例,保留核心逻辑链。
保留的技术亮点:
- 树莓派刷机演进史:从图形界面下载到极简命令行的一步步优化。
- QEMU镜像制作:
truncate创建磁盘并安装系统的流程。 - NixOS自动化:通过
make-disk-image.nix生成镜像的实践。
后续方向:
作者预告本系列共四部分,后续将探讨更底层的问题(如“如何在启动前替换根文件系统”),暗示技术深度会进一步增加。
评论总结
以下是评论内容的总结:
关于直接写入挂载磁盘的风险
- 作者M95D指出,通过内核配置选项CONFIGBLKDEVWRITEMOUNTED可以允许写入挂载的磁盘。
- "There's a kernel config option that allows it. CONFIGBLKDEVWRITEMOUNTED"
- 作者rwmj警告,直接写入挂载磁盘可能导致文件系统损坏。
- "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."
- 作者M95D指出,通过内核配置选项CONFIGBLKDEVWRITEMOUNTED可以允许写入挂载的磁盘。
替代方案:kexec和initramfs
- 作者matja提出使用kexec进入新的内核和initramfs,避免文件系统访问。
- "My solution was to kexec into a new kernel+initramfs which has a DHCP client and cURL in it"
- 作者alexellisuk建议在initramfs中直接写入磁盘,无需挂载根文件系统。
- "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?"
- 作者matja提出使用kexec进入新的内核和initramfs,避免文件系统访问。
实际应用案例
- 作者irishcoffee分享了在嵌入式系统中使用dd更新分区的经验。
- "I've been dd-ing A/B partitions for embedded yocto distributions for years and years."
- 作者SamWhited回忆了使用rsync和ZFS快照的部署方法。
- "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."
- 作者irishcoffee分享了在嵌入式系统中使用dd更新分区的经验。
其他技术方案
- 作者PunchyHamster提到将OS完全迁移到RAM中的方法。
- "Boom, now your OS lives entirely in RAM"
- 作者ma2kx和alexellisuk推荐使用网络启动(netboot)作为替代方案。
- "Why not just use netboot?"
- "This reminds me of netbooting workflows from things like MaaS, Tinkerbell"
- 作者PunchyHamster提到将OS完全迁移到RAM中的方法。
技术挑战和注意事项
- 作者pzmarzly指出磁盘扇区大小不匹配可能导致的问题。
- "You will run into problems if destination drive has different sector size than your VM"
- 作者zoobab分享了通过网络使用netcat和dd克隆磁盘的经验。
- "I used netcat and dd via the network to clone machines that has the same HDD"
- 作者pzmarzly指出磁盘扇区大小不匹配可能导致的问题。
批评与幽默
- 作者megous批评了文章中提出的方法不够合理。
- "Instead of applying some sense to the problem, this ridiculous approach gets promoted to a front page, lol."
- 作者PunchyHamster幽默地回顾了磁盘安装的历史。
- "back in the day you installed os on diskettes like that!"
- 作者megous批评了文章中提出的方法不够合理。