From 2fd2a45c07fe3ff3d764ecd070ab7475aca8eb20 Mon Sep 17 00:00:00 2001 From: Lu Qiuwen Date: Tue, 27 Apr 2021 14:16:43 +0800 Subject: [PATCH] First commit. --- cook-bits.sh | 88 +++++++++++++ installer/distro-setup.sh | 74 +++++++++++ installer/install.sh | 252 ++++++++++++++++++++++++++++++++++++++ mkimage-yum.sh | 104 ++++++++++++++++ 4 files changed, 518 insertions(+) create mode 100755 cook-bits.sh create mode 100755 installer/distro-setup.sh create mode 100755 installer/install.sh create mode 100755 mkimage-yum.sh diff --git a/cook-bits.sh b/cook-bits.sh new file mode 100755 index 00000000..c4162b8d --- /dev/null +++ b/cook-bits.sh @@ -0,0 +1,88 @@ +#!/bin/bash + +# +# Copyright (C) 2017 Rajendra Dendukuri +# +# SPDX-License-Identifier: GPL-2.0 +# + +# Make an ONIE installer using CentOS 7 chroot environment +# +# inputs: cento7 chroot package +# output: ONIE compatible OS installer image +# +# Comments: This script expects that yumbootsstrap is installed on +# on the Linux host where it is executed. + +#!/bin/sh + +set -e + +IN=./input +OUT=./output +rm -rf $OUT +mkdir -p $OUT + +INSTALLER_TEMPLATE_DIR=./installer +WORKDIR=./work +EXTRACTDIR="$WORKDIR/extract" +INSTALLDIR="$WORKDIR/installer" + +# Create a centos-7 chroot package if not done already +DISTR0_VER="21.04.1" +MACHINE_ID="9140NPCP01R01" + +CHROOT_PKG="tsg-os-${DISTR0_VER}-${MACHINE_ID}-chroot.tar.bz2" +output_file="${OUT}/tsg-os-${DISTR0_VER}-${MACHINE_ID}-ONIE.bin" + +echo -n "Creating $output_file: ." + +# prepare workspace +[ -d $EXTRACTDIR ] && chmod +w -R $EXTRACTDIR +rm -rf $WORKDIR +mkdir -p $EXTRACTDIR +mkdir -p $INSTALLDIR + +# Copy distro package +cp -f ${IN}/${CHROOT_PKG} $INSTALLDIR + +# Create custom install.sh script +cp $INSTALLER_TEMPLATE_DIR/install.sh $INSTALLDIR/install.sh +chmod +x $INSTALLDIR/install.sh +sed -i -e "s/%%DISTR0_VER%%/$DISTR0_VER/" $INSTALLDIR/install.sh +sed -i -e "s/%%MACHINE_ID%%/$MACHINE_ID/" $INSTALLDIR/install.sh +sed -i -e "s/%%CHROOT_PKG%%/$CHROOT_PKG/" $INSTALLDIR/install.sh + +# Create o/s setup script +cp $INSTALLER_TEMPLATE_DIR/distro-setup.sh $INSTALLDIR/distro-setup.sh +chmod +x $INSTALLDIR/distro-setup.sh + +echo -n "." + +# Repackage $INSTALLDIR into a self-extracting installer image +sharch="$WORKDIR/sharch.tar" +tar -C $WORKDIR -cf $sharch installer || { + echo "Error: Problems creating $sharch archive" + exit 1 +} + +[ -f "$sharch" ] || { + echo "Error: $sharch not found" + exit 1 +} +echo -n "." + +sha1=$(cat $sharch | sha1sum | awk '{print $1}') +echo -n "." + +cp sharch_body.sh $output_file || { + echo "Error: Problems copying sharch_body.sh" + exit 1 +} + +# Replace variables in the sharch template +sed -i -e "s/%%IMAGE_SHA1%%/$sha1/" $output_file +echo -n "." +cat $sharch >> $output_file +rm -rf $tmp_dir +echo " Done." diff --git a/installer/distro-setup.sh b/installer/distro-setup.sh new file mode 100755 index 00000000..5ae978a5 --- /dev/null +++ b/installer/distro-setup.sh @@ -0,0 +1,74 @@ +#!/bin/sh -x + +# Create default user onie, with password onie +echo "Setting user onie password as onie" +useradd -s /bin/bash -m -k /dev/null onie +echo onie | passwd onie --stdin +echo "onie ALL=(ALL) ALL" >> /etc/sudoers +echo onie | passwd --stdin + +# Setup o/s mount points +(cat < /etc/fstab + +# Configure default hostname +echo "HOSTNAME=localhost" > /etc/sysconfig/network + +# Disable selinux +sed -ie "s/SELINUX=/SELINUX=disabled/g" /etc/selinux/config + +# Fix grub2 cannot read envcfg from grub2 +ln -sf /boot/grub2/grubenv /boot/grub/grubenv + +# restore /etc/passwd, /etc/group, and /etc/shadow +mkdir -p /config/sysroot/etc/ +mkdir -p /config/sysroot/etc/ssh + +if [ ! -f /config/sysroot/etc/passwd ]; then + cp /etc/passwd /config/sysroot/etc/passwd +fi + +if [ ! -f /config/sysroot/etc/group ]; then + cp /etc/group /config/sysroot/etc/group +fi + +if [ ! -f /config/sysroot/etc/shadow ]; then + cp /etc/shadow /config/sysroot/etc/shadow +fi + +ln -sf "/config/sysroot/etc/passwd" /etc/passwd +ln -sf "/config/sysroot/etc/group" /etc/group +ln -sf "/config/sysroot/etc/shadow" /etc/shadow + +# regenerate ssh host keys +if [ ! -f /config/sysroot/etc/ssh/ssh_host_dsa_key ]; then + ssh-keygen -q -N "" -t dsa -f /config/sysroot/etc/ssh/ssh_host_dsa_key +fi + +if [ ! -f /config/sysroot/etc/ssh/ssh_host_rsa_key ]; then + ssh-keygen -q -N "" -t rsa -b 4096 -f /config/sysroot/etc/ssh/ssh_host_rsa_key +fi + +if [ ! -f /config/sysroot/etc/ssh/ssh_host_ecdsa_key ]; then + ssh-keygen -q -N "" -t ecdsa -f /config/sysroot/etc/ssh/ssh_host_ecdsa_key +fi + +if [ ! -f /config/sysroot/etc/ssh/ssh_host_ed25519_key ]; then + ssh-keygen -q -N "" -t ed25519 -f /config/sysroot/etc/ssh/ssh_host_ed25519_key +fi + +ln -sf "/config/sysroot/etc/ssh/ssh_host_dsa_key" /etc/ssh/ssh_host_dsa_key +ln -sf "/config/sysroot/etc/ssh/ssh_host_rsa_key" /etc/ssh/ssh_host_rsa_key +ln -sf "/config/sysroot/etc/ssh/ssh_host_ecdsa_key" /etc/ssh/ssh_host_ecdsa_key +ln -sf "/config/sysroot/etc/ssh/ssh_host_ed25519_key" /etc/ssh/ssh_host_ed25519_key + +exit 0 diff --git a/installer/install.sh b/installer/install.sh new file mode 100755 index 00000000..60b0b4a8 --- /dev/null +++ b/installer/install.sh @@ -0,0 +1,252 @@ +#!/bin/sh + +blk_dev=/dev/sda +random_dev=/dev/urandom +root_disk=hd0 +distro_part=5 +distro_dev="/dev/sda${distro_part}" +distro_mnt=/mnt/distro +onie_root_dir=/mnt/onie-boot/onie +distro_config_mnt=/mnt/distro_config +kernel_args="" +grub_serial_command="" + +vol_label_tsg_os_sysroot_primary="TSG-OS-SYSROOT-1" +vol_label_tsg_os_sysroot_secondry="TSG-OS-SYSROOT-2" +vol_label_tsg_os_config="TSG-OS-CONFIG" +vol_label_tsg_os_data="TSG-OS-DATA" +exp_part_tsg_os_config=3 +exp_part_tsg_os_data=4 +exp_part_tsg_os_sysroot_primary=5 +exp_part_tsg_os_sysroot_secondry=6 +size_part_tsg_os_config="128M" +size_part_tsg_os_data="128M" +size_part_tsg_os_sysroot_primary="4096M" +size_part_tsg_os_sysroot_secondry="2048M" +dev_part_tsg_os_sysroot_primary="/dev/sda${exp_part_tsg_os_sysroot_primary}" +dev_part_tsg_os_sysroot_secondry="/dev/sda${exp_part_tsg_os_sysroot_secondry}" +dev_part_tsg_os_config="/dev/sda${exp_part_tsg_os_config}" +dev_part_tsg_os_data="/dev/sda${exp_part_tsg_os_data}" + +DISTR0_VER="%%DISTR0_VER%%" +MACHINE_ID="%%MACHINE_ID%%" +CHROOT_PKG="%%CHROOT_PKG%%" + +check_is_upgrade() { + part_tsg_os_data=$(sgdisk -p $blk_dev | grep -e "$vol_label_tsg_os_data" | awk '{print $1}') + part_tsg_os_config=$(sgdisk -p $blk_dev | grep -e "$vol_label_tsg_os_config" | awk '{print $1}') + part_tsg_os_sysroot_primary=$(sgdisk -p $blk_dev | grep -e "$vol_label_tsg_os_sysroot_primary" | awk '{print $1}') + part_tsg_os_sysroot_secondry=$(sgdisk -p $blk_dev | grep -e "$vol_label_tsg_os_sysroot_secondry" | awk '{print $1}') + + if [ "$part_tsg_os_data" != "$exp_part_tsg_os_data" ]; then return 1 + elif [ "$part_tsg_os_config" != "$exp_part_tsg_os_config" ]; then return 1 + elif [ "$part_tsg_os_sysroot_primary" != "$exp_part_tsg_os_sysroot_primary" ]; then return 1 + elif [ "$part_tsg_os_sysroot_secondry" != "$exp_part_tsg_os_sysroot_secondry" ]; then return 1 + fi + + return 0 +} + +format_sysroot_partitions() { + mkfs.ext4 -L "TGS-OS-SYSROOT-1" $dev_part_tsg_os_sysroot_primary || { + echo "ERROR: Unable to create file system on \$dev_part_tsg_os_sysroot_primary" + exit 1 + } + + mkfs.ext4 -L "TGS-OS-SYSROOT-2" $dev_part_tsg_os_sysroot_secondry || { + echo "ERROR: Unable to create file system on \$dev_part_tsg_os_sysroot_secondry" + exit 1 + } +} + +format_presistent_partitions() { + mkfs.ext4 -L "TSG-OS-CONFIG" $dev_part_tsg_os_config || { + echo "ERROR: Unable to create file system on \$dev_part_tsg_os_config" + exit 1 + } + + mkfs.ext4 -L "TSG-OS-DATA" $dev_part_tsg_os_data || { + echo "ERROR: Unable to create file system on \$dev_part_tsg_os_data" + exit 1 + } +} + +create_gpt_partitions() { + # remove old partitions + for p in $(seq 3 9) ; do + umount -f $blk_dev$p > /dev/null 2&>1 + sgdisk -d $p $blk_dev > /dev/null 2&>1 + done + + partprobe ${blk_dev} + + echo "Create partition TSG-OS-CONFIG." + begin=$(sgdisk -F $blk_dev) + sgdisk --new=${exp_part_tsg_os_config}:${begin}:+${size_part_tsg_os_config} \ + --change-name=${exp_part_tsg_os_config}:"TSG-OS-CONFIG" ${blk_dev} || { + echo "ERROR: Unable to create partition TSG-OS-CONFIG on $blk_dev" + exit 1 + } + + echo "Create partition TSG-OS-DATA." + begin=$(sgdisk -F $blk_dev) + sgdisk --new=${exp_part_tsg_os_data}:${begin}:+${size_part_tsg_os_data} \ + --change-name=${exp_part_tsg_os_data}:"TSG-OS-DATA" ${blk_dev} || { + echo "ERROR: Unable to create partition TSG-OS-DATA on $blk_dev" + exit 1 + } + + echo "Create partition TSG-OS-SYSROOT-1." + begin=$(sgdisk -F $blk_dev) + sgdisk --new=${exp_part_tsg_os_sysroot_primary}:${begin}:+${size_part_tsg_os_sysroot_primary} \ + --change-name=${exp_part_tsg_os_sysroot_primary}:"TSG-OS-SYSROOT-1" ${blk_dev} || { + echo "ERROR: Unable to create partition TSG-OS-SYSROOT-1 on $blk_dev" + exit 1 + } + + echo "Create partition TSG-OS-SYSROOT-2." + begin=$(sgdisk -F $blk_dev) + sgdisk --new=${exp_part_tsg_os_sysroot_secondry}:${begin}:+${size_part_tsg_os_sysroot_secondry} \ + --change-name=${exp_part_tsg_os_sysroot_secondry}:"TSG-OS-SYSROOT-2" ${blk_dev} || { + echo "ERROR: Unable to create partition TSG-OS-SYSROOT-2 on $blk_dev" + exit 1 + } + + partprobe ${blk_dev} +} + +cd $(dirname $0) +# bonk out on errors + +if check_is_upgrade; then + echo "Upgrading TSG-OS, the config and data partitions will be keeped in $blk_dev." + + format_sysroot_partitions || { + echo "ERROR: Unable to format sysroot partition on $blk_dev, TSG-OS install failed." + exit 1 + } + +else + echo "Installing TSG-OS, all partitions on $blk_dev will be erased." + + create_gpt_partitions || { + echo "ERROR: Unable to create partitions on $blk_dev, TSG-OS install failed." + exit 1 + } + + format_sysroot_partitions || { + echo "ERROR: Unable to format sysroot partition on $blk_dev, TSG-OS install failed." + exit 1 + } + + format_presistent_partitions || { + echo "ERROR: Unable to format presistent partitions on $blk_dev, TSG-OS install failed." + exit 1 + } +fi + +# Mount filesystem +mkdir -p $distro_mnt || { + echo "ERROR: Unable to create distro file system mount point: \$distro_mnt" + exit 1 +} + +mount -t ext4 -o defaults,rw $distro_dev $distro_mnt || { + echo "ERROR: Unable to mount $distro_dev on $distro_mnt" + exit 1 +} + +set -ex + +cp -f distro-setup.sh ${distro_mnt} +echo "Extract chroot environment ..." +tar -xf ${CHROOT_PKG} -C ${distro_mnt} + +[ -e ${distro_mnt}/dev/pts ] && { + mount -o bind /dev/pts \${distro_mnt}/dev/pts +} + +mount -t proc proc ${distro_mnt}/proc +mount -t sysfs sys ${distro_mnt}/sys +cp -a ${blk_dev} ${distro_mnt}/${blk_dev} +cp -a ${random_dev} ${distro_mnt}/${random_dev} + +mkdir -p ${distro_mnt}/config +mount -t ext4 /dev/sda${exp_part_tsg_os_config} ${distro_mnt}/config + +echo "Setting up TSG-OS sysroot .." +chroot ${distro_mnt} /distro-setup.sh ${distro_dev} + +[ -e ${distro_mnt}/dev/pts ] && { + umount ${distro_mnt}/dev/pts +} + +umount ${distro_mnt}/proc +umount ${distro_mnt}/sys +umount ${distro_mnt}/config + +# Install boot loader +echo "Install GRUB2 bootloader .." +grub-install --boot-directory="${distro_mnt}/boot" --recheck "${blk_dev}" || { + echo "ERROR: grub-install failed on: ${blk_dev}" + exit 1 +} + +# Prepare boot loader configuration file +grub_cfg=$(mktemp) + +# Add common configuration, like the timeout and serial console. +(cat <> ${grub_cfg} + + +# Add a menu entry for o/s distro +(cat <> ${grub_cfg} + + +# Add menu entries for ONIE -- use the grub fragment provided by the +# ONIE distribution. +$onie_root_dir/grub.d/50_onie_grub >> ${grub_cfg} + +# Copy boot loader config to appropriate location +cp -f ${grub_cfg} ${distro_mnt}/boot/grub/grub.cfg +cat ${grub_cfg} >> ${distro_mnt}/etc/grub.d/40_onie_grub + +umount ${distro_mnt} + +cd / + +echo "TSG-OS install successfully, need to reboot." +exit 0 diff --git a/mkimage-yum.sh b/mkimage-yum.sh new file mode 100755 index 00000000..2abcd90a --- /dev/null +++ b/mkimage-yum.sh @@ -0,0 +1,104 @@ +#!/usr/bin/env bash +# +# Create a base CentOS Docker image. +# +# This script is useful on systems with yum installed (e.g., building +# a CentOS image on CentOS). See contrib/mkimage-rinse.sh for a way +# to build CentOS images on other systems. + +usage() { + cat < +OPTIONS: + -y The path to the yum config to install packages from. The + default is /etc/yum.conf. +EOOPTS + exit 1 +} + +# option defaults +yum_config=/etc/yum.conf +while getopts ":y:h" opt; do + case $opt in + y) + yum_config=$OPTARG + ;; + h) + usage + ;; + \?) + echo "Invalid option: -$OPTARG" + usage + ;; + esac +done +shift $((OPTIND - 1)) +name=$1 + +if [[ -z $name ]]; then + usage +fi + +#-------------------- + +#target=$(mktemp -d --tmpdir $(basename $0).XXXXXX) +target=$PWD/centos_mkroot + +set -x + +for dev in console null zero urandom; do + /sbin/MAKEDEV -d "$target"/dev -x $dev +done + +package_to_install="@base @core @debugging @directory-client @guest-agents + @hardware-monitoring @network-file-system-client @performance @remote-system-management + grub2 kernel kernel-devel" + +setopt="group_package_types=mandatory,default,optional" + +yum -c "$yum_config" --installroot="$target" -y --setopt=$setopt install $package_to_install +yum -c "$yum_config" --installroot="$target" -y --setopt=$setopt clean all + +cat > "$target"/etc/sysconfig/network <&2 "warning: cannot autodetect OS version, using '$name' as tag" + version=$name +fi + +#tar --numeric-owner -c -C "$target" . | docker import - $name:$version +#docker run -i -t $name:$version echo success + +tar --numeric-owner -c -C centos_mkroot/ . | bzip2 > input/centos-7-chroot.tar.bz2 + +#rm -rf "$target"