云服务器安装 Live 操作系统(AlpineLinux)

本文讲解如何在支持 grub 启动器的现代 Linux 系统上安装 AlpineLinux Live 系统。此功能用于在内存中启动一个完整的 Linux 系统,启动完成后不再依赖本地硬盘(懂的都懂,不懂多说无益)。
前置条件
支持且不限于阿里云、腾讯云、微软云、谷歌云,只要满足操作系统要求,均可完成安装。
​​操作系统要求​​

✅ 支持系统:
Debian 10/11/12
Ubuntu 22.04 / 24.04

❌ 不支持:
CentOS/RHEL(使用grub2除外)
FreeBSD/Windows

​​​​硬件要求​​

x86_64 架构
≥ 512MB 内存
支持 DHCP 网络协议

进入 Live 操作系统
执行下面的代码,将安装并立即重启进入 AlpineLinux Live 系统。登录账号:root(无需密码)。

mkdir -p /netboot && cd /netboot

rgeo=`wget -qO- https://ipip.rehi.org/country_code`

if [ "$rgeo" == "CN" ]; then
  repo=https://mirrors.tuna.tsinghua.edu.cn/alpine/edge
else
  repo=https://dl-cdn.alpinelinux.org/alpine/edge
fi

wget $repo/releases/x86_64/netboot/vmlinuz-virt
wget $repo/releases/x86_64/netboot/initramfs-virt
modl=$repo/releases/x86_64/netboot/modloop-virt

cat >> /etc/grub.d/40_custom <<EOF
menuentry "Alpine Linux Minimal" {
    search --set=root --file /netboot/vmlinuz-virt
    linux /netboot/vmlinuz-virt ip=dhcp modloop=$modl alpine_repo=$repo/main/
    initrd /netboot/initramfs-virt
}
EOF

sed -i 's/GRUB_DEFAULT=0/GRUB_DEFAULT="Alpine Linux Minimal"/' /etc/default/grub

update-grub
sync && reboot -f

切换至原系统
AlpineLinux 终端执行下面的代码,注意修改系统盘分区代码。

mount /dev/vda1 /mnt           # 挂载原系统根分区
chroot /mnt                    # 切换至原系统

sed -i 's/GRUB_DEFAULT=.*/GRUB_DEFAULT=0/' /etc/default/grub

update-grub
reboot