简易Ramdisk 镜像制作(基于Centos7+)

2020-08-06 09:50:25 浏览数 (1)

镜像完成后可用于PXE环境或U盘randisk等

安装系统

考虑到实际应用中Ramdisk需要保持小而精,故在安装系统时选择最小化安装,然后再安装所需要的包。

安装必要软体

tool

描述

vim

ifconfig

net-tools

lspci

pciutils

numactl

nfs

tmux

ftp

bc

smartctl

smartmontools

mailx

mktemp

mcelog

ipmi

ipmitool和OpenIPMI

编译环境

gcc make kernel-headers gcc-c kernel-devel

dos2unix

ethtool

lsusb

usbutils

zlib

zip

unzip

killall

psmisc

sg3_utils

numactl

locate

mlocate

rngd.service

rng-tools

strace

strace

expect

expect和tcl

openssh

openssh-server

nvme

mcelog

smartd

issdcm

一些OS设置

代码语言:javascript复制
systemctl set-default multi-user.target
timedatectl set-timezone Asia/Shanghai #Modify TIME zone
systemctl disable firewalld 
systemctl disable NetworkManager
sed -i "/^SELINUX=/cSELINUX=disabled" /etc/selinux/config #disable SELINUX
setenforce 0 #shutdown SELINUX
sed -i "s/^ExecStart=-/sbin/agetty --noclear %I $TERM/& --autologin root/g" /usr/lib/systemd/system/getty@.service #root autologin
sed -i "s/^ExecStart=-/sbin/agetty --keep-baud 115200,38400,9600 %I $TERM/& --autologin root/g" /usr/lib/systemd/system/serial-getty@.service #root Serial autologin
sed -i "/^#UseDNS/cUseDNS no" /etc/ssh/sshd_config #SSH setting
sed -i "/^#IgnoreUserKnownHosts no/cIgnoreUserKnownHosts yes" /etc/ssh/sshd_config #SSH setting
sed -i "/StrictHostKeyChecking/cStrictHostKeyChecking no" /etc/ssh/ssh_config #SSH client setting
echo -e "set -g mode-keys vinset -g prefix C-anunbind C-bnbind C-a send-prefix" > /root/.tmux.conf #tmux setting

打包系统

代码语言:javascript复制
[root@localhost ~]# cat make_img.sh
#!/bin/bash
###############################################
# Filename    :   make_img.sh
# Author      :   PedroQin
# Date        :   2020-01-17 15:54:12
# Description :   
# Version     :   1.0.0
###############################################

target_folder="/temp/create_os"
os_folder="$target_folder/os"
# show message in green
function green_message()
{
    tput bold
    echo -ne "33[32m$@33[0m"
    echo
}

# show message in red
function red_message()
{
    tput bold
    echo -ne "33[31m$@33[0m"
    echo
}

# print description and then run it
function print_run()
{
    if [ $# -eq 1 ];then
        green_message "$1"
        eval "$1"
    elif [ $# -ge 2 ];then
        green_message "$1"
        shift
        eval "$@"
    else
        return -1
    fi
}

whereami=`cd $(dirname $0);pwd`
change_log="$whereami/Changelog"
version=`awk '/Version/ {print $2}' $change_log | sed -n '1p'`
[ -z "$version" ] && red_message "please check $change_log" && exit 1
DATE=`date  "%Y%m%d%H%M%S"`
os_image="initrd_${version}_${DATE}.img"
green_message "Image Name : $os_image , press any key to package ..."
read -n 1
green_message "Copy Data..."
print_run "mkdir -p $os_folder"
print_run "cp -d /{bin,lib,lib64,sbin} $os_folder"
print_run "cp -arf /{boot,etc,opt,root,run,srv,usr,var} $os_folder"
print_run "mkdir $os_folder/{dev,home,media,mnt,proc,sys,log,tmp}"

green_message "Initialize Data..."
print_run "delete fstab"                "rm -f $os_folder/etc/fstab 2>/dev/null"
print_run "delete network config file"  "rm -rf $os_folder/etc/sysconfig/network-scripts/ifcfg-e*  2>/dev/null"
print_run "delete log"                  "rm $os_folder/var/log/messages* ; rm $os_folder/var/log/dmesg* ; rm $os_folder/var/log/mail* ; rm $os_folder/var/log/mcelog* ;rm $os_folder/var/log/boot.log* ;rm $os_folder/var/log/cron* ;rm $os_folder/var/log/secure* ;rm $os_folder/var/log/spooler* ;rm $os_folder/var/log/yum.log* ;rm $os_folder/var/log/btmp* ;touch $os_folder/var/log/mcelog "
print_run "clean log"                   "echo > $os_folder/root/.ssh/known_hosts ; echo > $os_folder/root/.bash_history ; echo > $os_folder/var/log/wtmp ; echo > $os_folder/var/log/btmp ; echo > $os_folder/root/.viminfo"
print_run "create init"                 "cp $os_folder/usr/sbin/init $os_folder"

green_message "Package image..."

print_run "cd $os_folder; find .|cpio -c -o|xz --format=lzma --best > ../$os_image"
echo
green_message "OS image: $target_folder/$os_image"

0 人点赞