爱收集资源网

Ubuntu20.04服务器安装新操作模式

网络 2023-06-29 09:02

一、介绍

Ubuntu20.04的服务器安装程序支持新的操作模式:手动安装(automatedinstallation)。手动安装可以通过手动安装配置提早回答所有这种配置问题,并使安装过程无需任何交互即可运行。

在Ubuntu18.04中,用的应答文件是preseeds(预配置文件),它基于debian-installer(akadi)来实现手动安装的。须要注意的是,假如你使用的是cobbler,那你应当使用ubuntu-20.04-legacy-server-amd64.iso,而不是live-server(应当是缺乏netboot),Ubuntu20.04已没有server。

Ubuntu20.04手动安装在以下主要方面与之前的版本有所不同:

•应答文件格式完全不同。如今是:cloud-initconfig,一般为yaml。而之前是:debconf-set-selections格式。

•当前提条件中不存在问题的答案时,di会停止并要求用户输入。而手动安装不是这样的:默认情况下,假如根本没有任何手动安装配置,则安装程序将使用任何未回答问题的默认设置(假如没有默认问题,安装程序将失败)。在手动安装中可以将配置中的特定部份指定为“交互式”,这意味着安装程序仍将停止并寻问这种部份。

二、环境

在ubuntu18.04.5上布署autoinstall来批量安装ubuntu20.04.2。还是典型的PXE+TFTP+HTTP+DHCP+Subiquity(ubuntu服务器安装程序)。镜像为:ubuntu-20.04.2-live-server-amd64.iso。注意,引导方法为UEFI。

主机系统IP地址

server

ubuntu18.04.5

10.0.0.4

node1

修复ubuntu引导项_ubuntu安装后没有引导_win8引导ubuntu

未安装操作系统

node2

未安装操作系统

三、部署1、安装相关软件

isc-dhcp-server:拿来给顾客端主机分配可用的IP地址。

tftpd-hpa:拿来给顾客端主机提供引导及驱动文件。

apache2:拿来给顾客端主机提供镜像、应答文件以及一些自定义的文件脚本之类的。

root@server:~# apt-get -y install tftpd-hpa apache2 isc-dhcp-server whoisCOPY

2、配置tftp和apache

root@server:~# cat > /etc/apache2/conf-available/tftp.conf <
        Options +FollowSymLinks +Indexes
        Require all granted

Alias /tftp /var/lib/tftpboot
EOF
root@server:~# a2enconf tftp
root@server:~# systemctl restart apache2COPY

打算镜像,上传镜像到/var/lib/tftpboot/。

打算引导文件:vmlinuz(可引导的、压缩的内核),initrd(系统引导过程中挂载的一个临时根文件系统),pxelinux.0(网路引导程序)。

root@server:~# mount /var/lib/tftpboot/ubuntu-20.04.2-live-server-amd64.iso /mnt/
root@server:~# cp /mnt/casper/vmlinuz /var/lib/tftpboot/
root@server:~# cp /mnt/casper/initrd /var/lib/tftpboot/
root@server:~# umount  /mnt
root@server:~# wget http://archive.ubuntu.com/ubuntu/dists/focal/main/uefi/grub2-amd64/current/grubnetx64.efi.signed -O /var/lib/tftpboot/pxelinux.0COPY

打算grub

root@server:~# mkdir -p /var/lib/tftpboot/grub
root@server:~# cat > /var/lib/tftpboot/grub/grub.cfg <COPY

3、配置DHCP

root@server:~# cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.bak
root@server:~# cat > /etc/dhcp/dhcpd.conf <COPY

4、准备cloud.initconfig

root@server:~# cat > /var/lib/tftpboot/meta-data <COPY

在打算cloud.initconfig前。建议先手动安装一次ubuntu20.04.2,在/var/log/installer/目录下会生成一个autoinstall-user-data,这是基于当前的系统的应答文件,我们可以以它作为基础,按照实际情况进行更改。

root@server:/var/lib/tftpboot# cat > user-data <<'EOF'
#cloud-config
autoinstall:
  version: 1
  apt:
    primary:
    - arches: [default]
      uri: http://mirrors.aliyun.com/ubuntu
  # The passwords are all 000000
  user-data:
    timezone: Asia/Shanghai
    disable_root: false
    chpasswd:
      list: |
        root:$6$UenIfx4J$MXuFvAbjNjwotUl6CtEtwC.1SnlPqMkBd7oHg02XZ1iNk97eMglUrRO1hQUVvOZEf3M/aEhgyrQ/gTDx4fizz/
  identity:
    hostname: node
    password: $6$m/xrHiECoB3upm$qVLuNKyH67prn/uOKlM9soMSIugK.Bzy8jU.TpYDQhLRDvTQtn1ga6Hv0musEMbIUZNV1AmIwM6r/59ZfRA8X0
    username: test
  keyboard: {layout: us, variant: ''}
  locale: en_US.UTF-8
  ssh:
    install-server: true
  storage:
    grub:
      reorder_uefi: False
    config:
    - {ptable: gpt, path: /dev/sda, wipe: superblock-recursive, preserve: false, name: '',
      grub_device: false, type: disk, id: disk-sda}
    - {device: disk-sda, size: 536870912, wipe: superblock, flag: boot, number: 1,
      preserve: false, grub_device: true, type: partition, id: partition-0}
    - {fstype: fat32, volume: partition-0, preserve: false, type: format, id: format-0}
    - {device: disk-sda, size: -1, wipe: superblock, flag: '', number: 2,
      preserve: false, type: partition, id: partition-1}
    - {fstype: ext4, volume: partition-1, preserve: false, type: format, id: format-1}
    - {device: format-1, path: /, type: mount, id: mount-1}
    - {device: format-0, path: /boot/efi, type: mount, id: mount-0}
  packages:
  - linux-generic-hwe-20.04-edge
  late-commands:
  - curtin in-target --target=/target -- wget -P /root/ http://10.0.0.4/tftp/bash/init.sh
  - curtin in-target --target=/target -- wget -P /root/ http://10.0.0.4/tftp/bash/network.sh
  - curtin in-target --target=/target -- bash /root/init.sh
EOFCOPY

以上意思我就不注解了,详尽请查阅官方文档:AutomatedServerinstallerconfigfilereference|Ubuntu

win8引导ubuntu_ubuntu安装后没有引导_修复ubuntu引导项

5、脚本打算

我这儿打算了两个脚本(按照实际更改):

•init.sh:用于在系统成功重启之前,在安装成功并安装了所有更新和软件包然后运行的初始化脚本。

•network.sh:用于快速更改网路配置。在装好的系统上执行此脚本,输入IP,即可将动态地址换成静态地址。

root@server:~# mkdir /var/lib/tftpboot/bash
root@server:~# cd /var/lib/tftpboot/bash/
root@server:/var/lib/tftpboot/bash# vim init.sh
root@server:/var/lib/tftpboot/bash# vim network.shCOPY

init.sh:

#!/bin/bash
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
systemctl restart sshd
systemctl stop ufw.service
systemctl disable ufw.service
echo -e "NTP=ntp1.aliyun.com\nFallbackNTP=ntp.ubuntu.com" >> /etc/systemd/timesyncd.conf
systemctl restart systemd-timesyncd
cat >> /etc/security/limits.conf << EOF
*       soft        nofile  655350
*       hard        nofile  655350
*       soft        nproc   655350
*       hard        nproc   655350
root        soft        nofile  655350
root        hard        nofile  655350
root        soft        nproc   655350
root        hard        nproc   655350
EOF
rm -rf /root/init.shCOPY

network.sh:

#!/bin/bash
cd /etc/netplan
cp *.yaml network.yaml.bak
read -p "please ip address: " IP
Gateway=`echo $IP | awk -F '.' '{print $1"."$2"."$3".2"}'`
sed -i 's#critical: true#addresses: ['${IP}'/24]#' *.yaml
sed -i '/dhcp-identifier: mac/d' *.yaml
sed -i '0,/dhcp4: true/{s/dhcp4: true/gateway4: '${Gateway}'/}' *.yaml 
netplan apply
cdCOPY

四、自动布署

以上的布署步骤,我都写成了脚本。并将镜像、脚本和一些文件,打包成了一个压缩包。压缩包下载地址:autoinstall-virtual-无镜像

用法:

将下载出来的压缩包上传到服务器,解压缩,步入解压下来的autoinstall目录并把镜像放在其中。

root@server:~# tar -xf autoinstall.tar.gz
root@server:~# cd autoinstall/
root@server:~/autoinstall# ll
total 1188120
-rw-r--r-- 1 root root       5506 Apr 17 15:51 autoinstall.sh
-rw-r--r-- 1 root root       2104 Apr 17 15:43 init.sh
-rw-r--r-- 1 root root        334 Apr 17 16:04 network.sh
-rw-r--r-- 1 root root    1435512 Apr  4 11:30 pxelinux.0
-rw-r--r-- 1 root root 1215168512 Apr  4 11:30 ubuntu-20.04.2-live-server-amd64.isoCOPY

依据实际情况更改autoinstall.sh,主要更改以下几个地方:

# pxe服务端地址,root和普通用户test的密码
pxe_default_server='10.0.0.4'
root_passwd=`mkpasswd -m sha-512 '000000'`
test_passwd=`mkpasswd -m sha-512 '000000'`
# dhcp信息,根据实际网络来。注意实际网络必须要通外网,因为需要从网络下载内核更新,如果不通会报错。
subnet 10.0.0.0 netmask 255.255.255.0 {
     option routers             10.0.0.2;
     option domain-name-servers 114.114.114.114;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        10.0.0.200 10.0.0.220;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                ${pxe_default_server};
     filename "pxelinux.0";COPY

假如想在无网环境下安装,请在user-data中去除packages部份。

更改好autoinstall.sh后,注意要在autoinstall目录下执行脚本。

root@server:~/autoinstall# bash autoinstall.shCOPY

假如发觉autoinstall.sh更改错误且早已布署了。请删掉/va/lib/tftpboot下的所有东西。再到autoinstall目录下重新执行脚本。

放上脚本内容,结构逻辑比较简单,autoinstall.sh:

#!/bin/bash
apt-get -y install tftpd-hpa apache2 isc-dhcp-server whois
pxe_default_server='10.0.0.4'
root_passwd=`mkpasswd -m sha-512 '000000'`
hb_passwd=`mkpasswd -m sha-512 '000000'`
cat > /etc/apache2/conf-available/tftp.conf <
        Options +FollowSymLinks +Indexes
        Require all granted

Alias /tftp /var/lib/tftpboot
EOF
a2enconf tftp
systemctl restart apache2
cp ubuntu-20.04.2-live-server-amd64.iso /var/lib/tftpboot/
mount /var/lib/tftpboot/ubuntu-20.04.2-live-server-amd64.iso /mnt/
cp /mnt/casper/vmlinuz /var/lib/tftpboot/
cp /mnt/casper/initrd /var/lib/tftpboot/
umount  /mnt
#wget http://archive.ubuntu.com/ubuntu/dists/focal/main/uefi/grub2-amd64/current/grubnetx64.efi.signed -O /var/lib/tftpboot/pxelinux.0
cp pxelinux.0 /var/lib/tftpboot/pxelinux.0
mkdir /var/lib/tftpboot/bash
cp init.sh /var/lib/tftpboot/bash
cp network.sh /var/lib/tftpboot/bash
mkdir -p /var/lib/tftpboot/grub
cat > /var/lib/tftpboot/grub/grub.cfg <<'EOF'
default=autoinstall
timeout=0
timeout_style=menu
menuentry "Focal Live Installer - automated" --id=autoinstall {
    echo "Loading Kernel..."
    linux /vmlinuz ip=dhcp url=http://${pxe_default_server}/tftp/ubuntu-20.04.2-live-server-amd64.iso autoinstall ds=nocloud-net\;s=http://${pxe_default_server}/tftp/
    echo "Loading Ram Disk..."
    initrd /initrd
}
menuentry "Focal Live Installer" --id=install {
    echo "Loading Kernel..."
    linux /vmlinuz ip=dhcp url=http://${pxe_default_server}/tftp/ubuntu-20.04.2-live-server-amd64.iso
    echo "Loading Ram Disk..."
    initrd /initrd
}
EOF
sed -i 's#${pxe_default_server}#'${pxe_default_server}'#g' /var/lib/tftpboot/grub/grub.cfg
cat > /var/lib/tftpboot/meta-data < /var/lib/tftpboot/user-data <<'EOF'
#cloud-config
autoinstall:
  version: 1
  apt:
    primary:
    - arches: [default]
      uri: http://mirrors.aliyun.com/ubuntu
  user-data:
    timezone: Asia/Shanghai
    disable_root: false
    chpasswd:
      list: |
        root:${root_passwd}
  identity:
    hostname: hb
    password: ${hb_passwd}
    username: hb
  keyboard: {layout: us, variant: ''}
  locale: en_US.UTF-8
  ssh:
    install-server: true
  storage:
    grub:
      reorder_uefi: False
    config:
    - {ptable: gpt, path: /dev/sda, wipe: superblock-recursive, preserve: false, name: '',
      grub_device: false, type: disk, id: disk-sda}
    - {device: disk-sda, size: 536870912, wipe: superblock, flag: boot, number: 1,
      preserve: false, grub_device: true, type: partition, id: partition-0}
    - {fstype: fat32, volume: partition-0, preserve: false, type: format, id: format-0}
    - {device: disk-sda, size: -1, wipe: superblock, flag: '', number: 2,
      preserve: false, type: partition, id: partition-1}
    - {fstype: ext4, volume: partition-1, preserve: false, type: format, id: format-1}
    - {device: format-1, path: /, type: mount, id: mount-1}
    - {device: format-0, path: /boot/efi, type: mount, id: mount-0}
  packages:
  - linux-generic-hwe-20.04-edge
  late-commands:
  - curtin in-target --target=/target -- wget -P /root/ http://${pxe_default_server}/tftp/bash/init.sh
  - curtin in-target --target=/target -- wget -P /root/ http://${pxe_default_server}/tftp/bash/network.sh
  - curtin in-target --target=/target -- bash /root/init.sh
EOF
sed -i 's#${root_passwd}#'${root_passwd}'#' /var/lib/tftpboot/user-data
sed -i 's#${hb_passwd}#'${hb_passwd}'#' /var/lib/tftpboot/user-data
sed -i 's#${pxe_default_server}#'${pxe_default_server}'#g' /var/lib/tftpboot/user-data
cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.bak
cat > /etc/dhcp/dhcpd.conf <COPY

五、注意事项1、引导方法

假如使用VMware测试,请更改虚拟机的引导方法为UEFI(默认为BIOS)。打开虚拟机设置,选择选项,点击中级,找到固件类型,选择UEFI。

假如使用化学机,请将BIOS引导方法更改为UEFI。

2、内存

使用VMware测试,建议分配给虚拟机显存大点(必须小于镜像文件,像20.04,建议3G)。由于在安装过程中,是须要将镜像文件拷贝到显存的,假如虚拟机的显存不足会报错。

3、无网环境,应答文件假如配置

后面有提及实际网路必需要通内网,这是由于在应答文件中我定义packages,即要安装到目标系统的软件包列表,倘若不通网会报错,安装程序会仍然卡在错误提示那里,只能重启。其实重启是可以正常步入系统,系统也是没问题的。

前面说的报错并不会造成系统安装失败,只是安装软件失败,系统在这之前早已安装完毕了。只是后续的应答未能正常进行,像packages部份旁边是late-commands,那这一部份就不会执行了。应当是有跳过错误继续进行的指令,自行查阅吧。

假如想在无网环境下安装,最简单的办法就是在user-data中去除packages部份。

4、日志查看

假如在手动安装过程年报错卡住。请使用Ctrl+Alt+F2步入命令行,查看/var/log/installer/installer-journal.txt日志或则其它相关日志。

使用Ctrl+Alt+F1可回到安装界面。

参考文章:

AutomatedServerinstallation|Ubuntu

AutomatedServerinstallerconfigfilereference|Ubuntu

Automated20.04ServerInstallationusingPXEandliveserverimage-AskUbuntu

ubuntu安装后没有引
上一篇:画江湖杯莫停手游:最佳操作模式 下一篇:没有了