使用Debian Preseeding在无VNC控制台的虚拟机上安装Debian

内容纲要

起源

找学长要了一台学校的高配置虚拟机方便运行自动调参内卷机,但由于不提供VNC控制台也不提供Debian镜像,只提供Ubuntu和CentOS,且安装虚拟机内系统还得找服务器厂家的人来进行,我也不知道是啥Hypervisor,因此非常难受,就想自己折腾一下安装。

后来经过Soha指点,可以采用Debian Preseeding来进行,现在汇报研究成果。

前置技能

由于涉及到修改bootloader的配置,需要对当前虚拟机内的Linux发行版使用的bootloader有一定的了解,本文将以grub2为例。

且为了防止遇到一些问题,建议在开始之前先进行切换至root用户。

准备文件

  • 下载当前Debian的安装内核与initrd
wget http://mirrors.cqu.edu.cn/debian/dists/stable/main/installer-amd64/current/images/netboot/debian-installer/amd64/initrd.gz
wget http://mirrors.cqu.edu.cn/debian/dists/stable/main/installer-amd64/current/images/netboot/debian-installer/amd64/linux

注:如果服务器并非amd64架构,例如arm64,换成对应的架构也是可以的,曾经在华为云的ARM机器上亲测成功

修改initrd

  1. 解压initrd
mkdir initrd
cd initrd
gzip -cd ../initrd.gz | cpio -idmv
  1. 绑定网卡

首先查询本机当前网卡的MAC地址:

root@ubuntu-test:~/initrd# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:5c:d1:12 brd ff:ff:ff:ff:ff:ff
    inet 10.237.55.101/24 brd 10.237.55.255 scope global dynamic eth0
       valid_lft 10658sec preferred_lft 10658sec
    inet6 fe80::215:5dff:fe5c:d112/64 scope link
       valid_lft forever preferred_lft forever

可以得到00:15:5d:5c:d1:12

然后,我们将它写入/etc/udev/rules.d/文件夹下的一个自定文件中:

root@ubuntu-test:~/initrd# echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"00:15:5d:5c:d1:12\", ATTR{type}==\"1\", KERNEL==\"eth*\", NAME=\"eth0\"" > etc/udev/rules.d/001_myeth.rules

注意:经过这次操作之后,安装完系统后请务必也将该udev的规则写入新系统的root中。

  1. 编写Preseed脚本
d-i netcfg/choose_interface select eth0
d-i netcfg/disable_autoconfig boolean true
d-i netcfg/dhcp_failed note
d-i netcfg/dhcp_options select Configure network manually
d-i netcfg/get_ipaddress string 10.237.55.101
d-i netcfg/get_netmask string 255.255.255.0
d-i netcfg/get_gateway string 10.237.55.1
d-i netcfg/get_nameservers string 202.202.0.33 202.202.0.34
d-i netcfg/confirm_static boolean true
d-i netcfg/get_hostname string Hello
d-i netcfg/get_domain string
d-i hw-detect/load_firmware boolean true

d-i anna/choose_modules string network-console
d-i preseed/early_command string anna-install network-console
d-i network-console/password password 123321qweewq
d-i network-console/password-again password 123321qweewq

d-i mirror/country string manual
d-i mirror/http/hostname string mirrors.cqu.edu.cn
d-i mirror/http/directory string /debian
d-i mirror/http/proxy string

然后保存到initrd目录下的preseed.cfg

注:

  1. IP地址和DNS请根据自己实际情况修改。
  2. 如果学校网络需要认证的话,如果你的学校有开源镜像站且不需要网络认证就可以访问,请将mirror/http/hostname改成自己学校的地址。如果校内没有镜像站,请在本地搭建一个http代理然后修改mirror/http/proxy

打包initrd以及复制内核

# 请先确保你在initrd根目录下
find | cpio -ovH newc | gzip -c9 > /boot/initrd-preseed.img
cd ..
mv linux /boot/debian-installer

修改grub

因为我们之后这个系统不再使用了,所以考虑直接修改/boot/grub/grub.cfg

我们找到grub的第一个menuentry,如图所示:

menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-bfa64965-fdf7-4e1f-bca0-e9f4cf649468' {
    recordfail
    load_video
    gfxmode $linux_gfx_mode
    insmod gzio
    if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
    insmod part_gpt
    insmod ext2
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root  bfa64965-fdf7-4e1f-bca0-e9f4cf649468
    else
      search --no-floppy --fs-uuid --set=root bfa64965-fdf7-4e1f-bca0-e9f4cf649468
    fi
    if [ "${initrdfail}" = 1 ]; then
      linux /boot/vmlinuz-5.4.0-40-generic root=PARTUUID=f1ea6d88-3da3-471a-9fd5-7f223b387786 ro  console=tty1 console=ttyS0
      initrd    /boot/initrd.img-5.4.0-40-generic
    else
      linux /boot/vmlinuz-5.4.0-40-generic root=PARTUUID=f1ea6d88-3da3-471a-9fd5-7f223b387786 ro  console=tty1 console=ttyS0 panic=-1
    fi
    initrdfail
}

我们需要去掉这个if,然后将linux和initrd的部分对应修改,并去掉上面的menuentry的所有class以及定义一个新的名称:

menuentry 'Debian installer' {
    recordfail
    load_video
    gfxmode $linux_gfx_mode
    insmod gzio
    if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
    insmod part_gpt
    insmod ext2
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root  bfa64965-fdf7-4e1f-bca0-e9f4cf649468
    else
      search --no-floppy --fs-uuid --set=root bfa64965-fdf7-4e1f-bca0-e9f4cf649468
    fi
    linux /boot/debian-installer
    initrd /boot/initrd-preseed.img
}

然后把它添加到第一个menuentry前面,成为第一个启动选项,如图所示:

# ...
export linux_gfx_mode
menuentry 'Debian installer' {
# ...
}
menuentry 'Ubuntu' {
# ...
}
# ...

重启进入安装

reboot
ssh installer@10.237.55.101

输入之前设置的密码123321qweewq即可

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Back to Top