!1 提交riscv自动构建镜像脚本

Merge pull request !1 from dingchenguang/master
This commit is contained in:
dingchenguang 2023-03-27 09:22:07 +00:00 committed by Gitee
commit fa49acaf3e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 206 additions and 0 deletions

84
autoBuildImgStep1.sh Executable file
View File

@ -0,0 +1,84 @@
#!/bin/bash
echo "脚本执行过程中需要输入sudo密码"
echo "推荐本地机使用优麒麟系统。如果需要构建openKylin系统需要在本地机中安装openKylin版本的debootstrap和openkylin-keyring"
echo "请输入您想构建的系统类型、适用硬件代表的序号如果输入错误可以按ctrl+c之后重新执行该脚本"
read -p "请输入你想构建的系统类型1.优麒麟2.openKylin :" img_type
if [ $img_type == 1 ]
then
echo "您想构建的系统类型是:优麒麟"
img_type_name="ubuntukylin"
elif [ $img_type == 2 ]
then
echo "您想构建的系统类型是openKylin"
img_type_name="openKylin"
else
echo "系统类型输入错误,请重新执行该脚本"
exit
fi
read -p "请输入你想适用的硬件1.hifive-unmatched2.visionfive1 :" img_hardware
if [ $img_hardware == 1 ]
then
echo "您想适用的硬件是hifive-unmatched"
img_hardware_name="hifive-unmatched"
elif [ $img_hardware == 2 ]
then
echo "您想适用的硬件是visionfive1"
img_hardware_name="visionfive1"
else
echo "适用硬件输入错误,请重新执行该脚本"
exit
fi
touch log1-img_type_name
echo $img_type_name > log1-img_type_name
touch log2-img_hardware_name
echo $img_hardware_name > log2-img_hardware_name
img_name=$img_type_name-$img_hardware_name.img
echo "镜像文件名为:$img_name"
# 创建空镜像文件
dd if=/dev/zero of=$img_name bs=1M count=8192
# 对镜像文件进行分区
sudo sgdisk -g --clear --set-alignment=1 \
--new=1:34:+1M: --change-name=1:'u-boot-spl' --typecode=1:5b193300-fc78-40cd-8002-e86c45580b47 \
--new=2:2082:+6M: --change-name=2:'opensbi-uboot' --typecode=2:2e54b353-1271-4842-806f-e436d6af6985 \
--new=3:16384:+500M: --change-name=3:'boot' --typecode=3:0x0700 --attributes=3:set:2 \
--new=4:0:-0 --change-name=4:'rootfs' --typecode=4:0x8300 \
$img_name
# 将镜像文件挂在为循环设备
logsave log3-loopnum bash -c "sudo losetup --partscan --find --show $img_name"
loop_num=$(head -n 4 log3-loopnum | tail -n 1)
p3='p3'
p4='p4'
loop_nump3=$loop_num$p3
loop_nump4=$loop_num$p4
p1='p1'
p2='p2'
loop_nump1=$loop_num$p1
loop_nump2=$loop_num$p2
echo $loop_nump3
echo $loop_nump4
# 格式化镜像并设置镜像分区格式
sudo mkfs.vfat $loop_nump3
sudo mkfs.ext4 $loop_nump4
sudo dosfslabel $loop_nump3 boot
sudo e2label $loop_nump4 rootfs
# 挂载循环设备的p4分区到/mnt/目录
sudo mount $loop_nump4 /mnt
# debootstrap在镜像中下载根文件系统
sudo apt-get install debootstrap qemu-user-static binfmt-support
if [ $img_type == 1 ]
then
sudo debootstrap --arch=riscv64 focal /mnt http://ports.ubuntu.com/ubuntu-ports
elif [ $img_type == 2 ]
then
echo "如果报错需要在本地机中安装openKylin版本的debootstrap和openkylin-keyring并重新执行脚本"
sudo debootstrap --arch=riscv64 --keyring=/usr/share/keyrings/openkylin-archive-keyring.gpg yangtze /mnt http://archive.build.openkylin.top/openkylin/
else
echo "系统类型输入错误,请重新执行该脚本"
exit
fi
# 挂载循环设备的p3分区到/mnt/boot/目录
sudo mount $loop_nump3 /mnt/boot/
sudo cp autoBuildImgStep2.sh log1-img_type_name log2-img_hardware_name /mnt/home/
echo "第二段脚本autoBuildImgStep2.sh在chroot后的/home/目录中"
#chroot进入镜像进行后续操作
sudo chroot /mnt/

94
autoBuildImgStep2.sh Executable file
View File

@ -0,0 +1,94 @@
#!/bin/bash
img_type_name=$(head -n 1 log1-img_type_name)
img_hardware_name=$(head -n 1 log2-img_hardware_name)
# 配置软件源
if [ $img_type_name == "ubuntukylin" ]
then
echo "deb http://ports.ubuntu.com/ubuntu-ports focal main universe multiverse restricted" > /etc/apt/sources.list
echo "deb http://ports.ubuntu.com/ubuntu-ports focal-updates main universe multiverse restricted" >> /etc/apt/sources.list
echo "deb http://ports.ubuntu.com/ubuntu-ports focal-backports main universe multiverse restricted" >> /etc/apt/sources.list
echo "deb http://ports.ubuntu.com/ubuntu-ports focal-security main universe multiverse restricted" >> /etc/apt/sources.list
touch /etc/apt/sources.list.d/test.list
rm /etc/apt/sources.list.d/*
elif [ $img_type_name == "openKylin" ]
then
echo "deb http://archive.build.openkylin.top/openkylin/ yangtze main cross pty" > /etc/apt/sources.list
echo "deb http://archive.build.openkylin.top/openkylin/ yangtze-security main cross pty" >> /etc/apt/sources.list
echo "deb http://archive.build.openkylin.top/openkylin/ yangtze-updates main cross pty" >> /etc/apt/sources.list
echo "deb http://archive.build.openkylin.top/openkylin/ yangtze-proposed main cross pty" >> /etc/apt/sources.list
touch /etc/apt/sources.list.d/test.list
rm /etc/apt/sources.list.d/*
else
echo "系统类型错误请检查log1-img_type_name文件中内容,并重新执行该脚本"
exit
fi
apt-get update
# 配置网卡设置
touch /etc/network/interfaces
echo " auto lo iface lo inet loopback" > /etc/network/interfaces
echo " auto eth0 iface eth0 inet dhcp" >> /etc/network/interfaces
# 配置root密码
echo "请输入要设置的root密码建议$img_type_name"
passwd
# 设置主机名
echo $img_type_name > /etc/hostname
echo "# <file system> <mount point> <type> <options> <dump> <pass>" > /etc/fstab
echo "LABEL=rootfs / ext4 errors=remount-ro 0 1" >> /etc/fstab
echo "LABEL=boot /boot vfat nodev,noexec,ro 0 2" >> /etc/fstab
# 配置内核软链接
touch /etc/kernel-img.conf
echo "do_symlinks = No" > /etc/kernel-img.conf
echo "no_symlinks = Yes" >> /etc/kernel-img.conf
# 安装内核
if [ $img_hardware_name == "hifive-unmatched" ]
then
apt-get install linux-image-5.11.0-1017-generic u-boot-menu u-boot-sifive initramfs-tools linux-modules-5.11.0-1017-generic linux-modules-extra-5.11.0-1017-generic linux-headers-5.11.0-1017-generic linux-firmware linux-base
apt-get clean
echo mmc_spi >>/etc/initramfs-tools/modules
rm /boot/initrd*
update-initramfs -c -k all
cp /lib/firmware/5.11.0-1017-generic/device-tree/sifive/hifive-unmatched-a00.dtb /boot/
echo U_BOOT_FDT=\"hifive-unmatched-a00.dtb\" >> /etc/default/u-boot
echo U_BOOT_PARAMETERS=\"rw rootwait console=ttySIF0,115200 earlycon\" >> /etc/default/u-boot
u-boot-update
echo "脚本执行结束后请在/boot/extlinux/extlinux.conf中的两个空白行都添加 fdt /hifive-unmatched-a00.dtb"
elif [ $img_hardware_name == "visionfive1" ]
then
apt install linux-firmware linux-base
echo "请在执行完脚本之后从赛昉官网下载visionfive1的内核deb包并拷贝进入镜像dpkg -i安装并执行以下命令"
echo "echo mmc_spi >>/etc/initramfs-tools/modules"
echo "rm /boot/initrd*"
echo "update-initramfs -c -k all"
echo "cp /lib/firmware/5.11.0-1017-generic/device-tree/sifive/hifive-unmatched-a00.dtb /boot/ #不同版本内核的dtb文件目录可能不同"
echo "visionfive1安装内核部分命令结束"
else
echo "适用的硬件类型错误请检查log2-img_hardware_name文件中内容,并重新执行该脚本"
exit
fi
# 设置镜像时间
apt-get install openssh-server openntpd ntpdate
apt-get clean
sed -i 's/^DAEMON_OPTS="/DAEMON_OPTS="-s /' /etc/default/openntpd
apt-get install language-pack-zh-hans language-pack-gnome-zh-hans
# 设置镜像默认语言
echo "这里配置系统默认语言为中文,请稍后选择 zh_CN.UTF-8"
locale-gen zh_CN.UTF-8
echo "选择 zh_CN.UTF-8"
dpkg-reconfigure locales
echo "LANG="zh_CN.UTF-8"" > /etc/default/locale
echo "LANGUAGE="zh_CN:zh"" >> /etc/default/locale
echo "LC_ALL="zh_CN.UTF-8"" >> /etc/default/locale
apt-get install language-selector-common language-selector-gnome
# 镜像中添加用户
echo "请输入要设置的密码,推荐$img_type_name"
adduser $img_type_name
usermod -G sudo -a $img_type_name
# 安装显示相关组件
apt install udisks2 xorg lightdm
# 安装ukui桌面组件
apt install ukui-desktop-environment ukui-desktop-environment-core ubuntukylin-theme
apt -f install
echo "请手动解决可能出现的apt安装错误之后执行exit退出chroot并执行第三段脚本autoBuildImgStep3.sh"

28
autoBuildImgStep3.sh Executable file
View File

@ -0,0 +1,28 @@
#!/bin/bash
echo "脚本执行过程中需要输入sudo密码"
img_type_name=$(head -n 1 log1-img_type_name)
img_hardware_name=$(head -n 1 log2-img_hardware_name)
loop_num=$(head -n 4 log3-loopnum | tail -n 1)
echo $loop_num
# 拷贝u-boot文件到镜像的p1、p2两个分区
if [ $img_hardware_name == "hifive-unmatched" ]
then
sudo dd if=/mnt/usr/lib/u-boot/sifive_hifive_unmatched_fu740/u-boot-spl.bin of=/dev/loop0p1 bs=4k
sudo dd if=/mnt/usr/lib/u-boot/sifive_hifive_unmatched_fu740/u-boot.itb of=/dev/loop0p2 bs=4k
elif [ $img_hardware_name == "visionfive1" ]
then
echo " "
else
echo "适用的硬件类型错误请检查log2-img_hardware_name文件中内容,并重新执行该脚本"
exit
fi
p3='p3'
p4='p4'
loop_nump3=$loop_num$p3
loop_nump4=$loop_num$p4
# 删除镜像中命令历史
sudo rm /mnt/root/.bash_history
sudo umount $loop_nump3
sudo umount $loop_nump4
# 取消设置循环设备
sudo losetup -d $loop_num