2018年9月3日 星期一

Rebuild kernel with Ubuntu kernel source

##  Ubuntu kernel source
    **  網站支援KERNEL
    **  https://github.com/torvalds/linux
   **  首先看一下自己的内核版本
    **  apt-cache search linux-source
    **  自動抓取相同KERNEL
    **  sudo apt-get install linux-source
    **  sudo apt-get install linux-source-4.4.0


##  Ubuntu 16.04 server rebuild kernel
    **  安裝必要套件
    **  sudo apt-get update
    **  sudo apt-get install libncurses-dev
    **  sudo apt-get install fakeroot build-essential kernel-package ncurses-dev libssl-dev xz-utils
    **  sudo apt-get install fakeroot build-essential kernel-package libncurses5 libncurses5-dev
    **  sudo apt-get install fakeroot build-essential kernel-package libncurses5 libncurses5-dev  bin86 linux-kernel-devel  debhelper modutils
    sudo apt-get install fakeroot build-essential kernel-package libncurses5 libncurses5-dev  linux-kernel-devel  debhelper modutils


##  Complier kernel and update
    **  使用mak3-kpkg安裝
        1.  make-kpkg clean
        2.  fakeroot make-kpkg --initrd kernel_image kernel_headers (complier 相關 .deb)
        3.  sudo dpkg -i linux-image-3.7.2_3.7.2-10.00.Custom_i386.deb
        4.  sudo dpkg -i linux-headers-3.7.2_3.7.2-10.00.Custom_i386.deb
     
    **  標準安裝

##  移除舊核心
    **  dpkg --get-selections | grep linux-image 顯示現有核心
    **  sudo apt-get purge linux-image-2.6.38-10-generic     移除特地核心
    **  sudo update-grub2 

A. 安裝Kernel:
Step1. 搜尋(檢查)想要安裝的kernel版本是否存在
        $ sudo apt-cache search linux-image-*

       或者填入版本號搜尋,將X.X.XX填入版本號
        $ sudo apt-cache search linux-image-X.X.XX-generic

Step2. 開始安裝kernel
        $ sudo apt-get install linux-image-X.X.XX-generic
        $ sudo apt-get install linux-image-4.15.0-33-generic

        如果有其他需求,像是編譯模組,可以連headers都裝上
        $ sudo apt-get install linux-headers-X.X.XX-generic
     
Step3. 安裝後,更新initramfs image,詳細man update-initramfs
        $ sudo update-initramfs -u -k all

       或者使用
        $ sudo update-initramfs -u -k `uname -r`
Step4. 接著更新grub清單
        $ sudo update-grub

Step5. 重開機
        $ sudo reboot

        按ESC鍵進入GRUB選單,選擇安裝的Kernel開機


B.移除Kernel:
Step1. 檢查目前正在用哪個kernel
        $ uname -a
Step2. 或者列出目前系統中已安裝哪些kernel
        $ dpkg --get-selections | grep linux-image
Step3. 移除不要的Kernel,會自動將相關版本的 linux-image-extra-* 也一併移除
        $ sudo apt-get purge linux-image-X.X.XX-XX-generic

    wget https://github.com/torvalds/linux/archive/v4.5.tar.gz
    tar -xvf v4.5.tar.gz
    cd linux-4.5/
 
    sudo apt-get install build-essential ncurses-dev libssl-dev build-essential ncurses-dev xz-utils kernel-package
    sudo make -j 4 clean
    sudo make -j 4
    sudo make modules -j 4
    sudo make modules_install
    sudo make install
   sudo mkinitramfs -o /boot/initrd.img-4.5
   sudo update-initramfs -c -k 4.5
 
    sudo apt update 更新系統的所有存儲庫。
    sudo apt upgrade -y 升級安裝的所有軟件包。
    sudo reboot 重新啟動服務器以確保應用可能安裝的內核更新。
    sudo apt list --upgradeable 現在再次檢查可升級包,並使用以下命令。
 
    wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.11.2/linux-headers-4.11.2-041102_4.11.2-041102.201705201036_all.deb
    wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.11.2/linux-headers-4.11.2-041102-generic_4.11.2-041102.201705201036_amd64.deb
    wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.11.2/linux-image-4.11.2-041102-generic_4.11.2-041102.201705201036_amd64.deb
    dpkg -i *.deb 使用dpkg命令安裝軟件包。
    sudo update-grub 內核程序包安裝完成後,更新grub並重新啟動系統。
    sudo reboot

如果想解壓*.src.rpm格式的包,可以執行rpm2cpio命令。

##  如果想解壓*.src.rpm格式的包,可以執行rpm2cpio命令。

    如下所示,有個*.src.rpm文件:
    [ root@localhost liusu]# ll
    total 1164
    -rw-r--r-- 1 root root 1188824 Nov 15 09:42 openstack-keystone-9.0.0-1.el7.src.rpm
   
    執行rpm2cpio命令來解壓:
    [ root@localhost liusu]# rpm2cpio openstack-keystone-9.0.0-1.el7.src.rpm |cpio -iv
    keystone-9.0.0.tar.gz
    keystone-dist.conf
    openstack-keystone-sample-data
    openstack-keystone.logrotate
    openstack-keystone.service
    openstack-keystone.spec
    openstack-keystone.sysctl
    2331 blocks
   
    如下所示,加壓後得文件列表:
    [ root@localhost liusu]# ll
    total 2352
    -rw-rw-r-- 1 root root 1181342 Nov 15 09:43 keystone-9.0.0.tar.gz
    -rw-rw-r-- 1 root root 263 Nov 15 09:43 keystone-dist.conf
    -rw-r--r-- 1 root root 1188824 Nov 15 09:42 openstack-keystone-9.0.0-1.el7.src.rpm
    -rw-rw-r-- 1 root root 96 Nov 15 09:43 openstack-keystone.logrotate
    -rwxrwxr-x 1 root root 53 Nov 15 09:43 openstack-keystone-sample-data
    -rw-rw-r-- 1 root root 242 Nov 15 09:43 openstack-keystone.service
    -rw-rw-r-- 1 root root 10122 Nov 15 09:43 openstack-keystone.spec
    -rw-rw-r-- 1 root root 201 Nov 15 09:43 openstack-keystone.sysctl
   
##  .tar.bz2
    套件名稱:bzip2。
    壓縮:
    [ jonny@linux ~ ]
    $ tar jcvf FileName.tar.bz2 DirName
    解壓縮:
    [ jonny@linux ~ ]
    $ tar jxvf FileName.tar.bz2
   

embededed system with CentOS boot setting terminal.


##  CentOS boot setting.

    ** CentOS 6.9 boot setting.
    serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
    terminal --timeout=0 serial console
    default=1
    timeout=5
    #splashimage=(hd0,0)/grub/splash.xpm.gz
    #hiddenmenu
   
    title Accusys 1U Switch Core (3nd Generation V2)
            root (hd0,0)
            kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=/dev/sda3  crashkernel=auto  console=ttyS0,115200n8 acscfg=1
            initrd /initramfs-2.6.32-696.el6.x86_64.img
   
    title Accusys 1U Switch Core (3nd Generation V2) Dev
            root (hd0,0)
            kernel /bzImage ro root=/dev/sda3  crashkernel=auto  console=ttyS0,115200n8 acscfg=1
            initrd /initramfs-2.6.32-696.el6.x86_64.img
   
    title CentOS 6 (2.6.32-696.el6.x86_64) Default boot setting
            root (hd0,0)
            kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=4df0d800-6b42-47f8-99da-5b50987f3824 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
            initrd /initramfs-2.6.32-696.el6.x86_64.img


    ** CentOS 5.8 boot setting.
    serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
    terminal --timeout=0 serial console
    default=2
    timeout=5
    title Accusys 1U Switch Core (3nd Generation)
            root (hd0,0)
            kernel /boot/vmlinuz-2.6.18-308.el5 ro root=/dev/sda1 crashkernel=128M@16M console=ttyS0,115200n8 acscfg=1
            initrd /boot/initrd-2.6.18-308.el5.img
   
    title Accusys 1U Switch Core (3nd Generation safe mode)
            root (hd0,0)
            kernel /boot/bzImage ro root=/dev/sda1 crashkernel=128M@16M console=ttyS0,115200n8 acscfg=1
            initrd /boot/initrd-2.6.18-308.el5.img
   
   
    title Accusys 1U Switch Core (3nd Generation Sam Debug mode)
            root (hd0,0)
            kernel /root/bzImage ro root=/dev/sda1 crashkernel=128M@16M console=ttyS0,115200n8 acscfg=1
            initrd /boot/initrd-2.6.18-308.el5.img


##  BIOS/GRUB/Kernel/XEN輸出重定向到串口的配置:

    將BIOS輸出重定向到串口
    這一功能需要主板支援,在BIOS中設置,比如我的設置為:
    Server ManagementConsole RedirectionConsole Redirection設置為”Serial Port A”
   
    將GRUB輸出重定向到串口(Edit /boot/grub/grub.conf)
    serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
    terminal --timeout=5 console serial
   
    grub引導過程中,會將輸出同時發送到終端螢幕和串口. grub引導過程中將在終端和連接到串口的超級終端上提示Press any key to continue,
    每秒鐘提示一次,共5次,可修改menu.lst檔terminal行中的--timeout=10改變提示次數,在這一段時間內,可以在終端的鍵盤,
    或者連接到串口的超級終端中按任意鍵進入grub選擇功能表.如果5秒內沒有在終端和連接串口的超級終端上按任意鍵,
    則grub的選擇功能表將出現在console上,如果希望默認情況下, grub選擇功能表出現在串口上,則可修改grub.conf將console serial修改為serial console.
   
    將kernel輸出資訊輸出到串口(編輯/boot/grub/grub.conf有的為menu.lst)
    增加kernel引導參數如下:
   
    kernel  /boot/vmlinuz-2.6.32-29 root=/dev/hda1 ro console=ttyS0,115200n8 console=tty0
   
    則kernel會將輸出資訊同時輸出到串口(ttyS0即COM1)和終端tty0。
    其中,“ttyS0”表示串口使用COM1;“115200”表示串列傳輸速率為115200bps;“n”表示無校驗位;“8”表示有8位元資料位元
   
##  kernel login message 修正為 Accusys login message
    "cat /etc/issue"
    Accusys PCIE SAN 1U Switch
    Kernel \r on an \m
   
##  To auto login to linux console on boot
    edit /etc/init/tty.conf (但是Switch 是ttys0設定方式不同)

    修正 /etc/init/tty.conf
    "exec /sbin/mingetty $TTY"
    改為
    "exec /sbin/mingetty --autologin root $TTY"
   
    [root@localhost ~]# cat /etc/init/tty.conf
    # tty - getty
    #
    # This service maintains a getty on the specified device.
    #
    # Do not edit this file directly. If you want to change the behaviour,
    # please create a file tty.override and put your changes there.
   
    stop on runlevel [S016]
   
    respawn
    instance $TTY
    #exec /sbin/mingetty $TTY
    exec /sbin/mingetty --autologin root $TTY
    usage 'tty TTY=/dev/ttyX  - where X is console id'
    [root@localhost ~]#

##  To auto login to linux serial console on boot
    edit /etc/init/serial.conf

    修正 /etc/init/serial.conf
    "exec /sbin/agetty /dev/$DEV $SPEED vt100-nav"
    改為
    "exec /sbin/agetty -n -l /usr/sbin/autologin /dev/$DEV $SPEED vt100-nav"
    但是 /usr/sbin 找不到 autologin
    我們要自己準備 autologin.c,  code 如下,

    #include "unistd.h"
    int main()
    {
        execlp( "login", "login", "-f", "root", NULL);
    }
    產生執行檔
    gcc autologin.c -o autologin, 將檔案放在你要存放的位子
   
    [root@localhost ~]# cat /etc/init/serial.conf
    # Automatically start a configured serial console
    #
    # How this works:
    #
    # On boot, a udev helper examines /dev/console. If a serial console is the
    # primary console (last console on the commandline in grub),  the event
    # 'fedora.serial-console-available <port name> <speed>' is emitted, which
    # triggers this script. It waits for the runlevel to finish, ensures
    # the proper port is in /etc/securetty, and starts the getty.
    #
    # If your serial console is not the primary console, or you want a getty
    # on serial even if it's not the console, create your own event e.g.
    # /etc/init/ttyS0.conf with content:
    # # ttyS0 - agetty
    # #
    # # This service maintains a agetty on ttyS0.
    #
    # stop on runlevel [S016]
    # start on runlevel [23]
    #
    # respawn
    # exec agetty -h -L -w /dev/ttyS0 115200 vt102
    #
    # Do not edit this file directly. If you want to change the behaviour,
    # please create a file serial.override and put your changes there.
   
    start on fedora.serial-console-available DEV=* and stopped rc RUNLEVEL=[2345]
    stop on runlevel [S016]
   
    instance $DEV
    respawn
    pre-start exec /sbin/securetty $DEV
    #exec /sbin/agetty /dev/$DEV $SPEED vt100-nav
    exec /sbin/agetty -n -l /root/autologin /dev/$DEV $SPEED vt100-nav
    post-stop exec /sbin/initctl emit --no-wait fedora.serial-console-available DEV=$DEV SPEED=$SPEED
    usage 'DEV=ttySX SPEED=Y  - where X is console id and Y is baud rate'


   

Rebuild kernel with CentOS 6.9 Source code

##  Get CentOS 6.9 Source code

    "yum -y install kernel-devel"  安裝complier tool
    "mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}" 創建一個Build source folder
    "echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros" 指定創建位址
 
    要在 CentOS-6 安裝源代碼套件及工具:

    以 root 的身份安裝 rpm-build、redhat-rpm-config、asciidoc、bison、hmaccalc、patchutils、perl-ExtUtils-Embed、
    xmlto、audit-libs-devel、binutils-devel、elfutils-devel、elfutils-libelf-devel、newt-devel、python-devel 及 zlib-devel 套件:
    "yum -y install rpm-build redhat-rpm-config asciidoc bison hmaccalc patchutils perl-ExtUtils-Embed xmlto"
    "yum -y install audit-libs-devel binutils-devel elfutils-devel elfutils-libelf-devel"
    "yum -y install ncurses-devel newt-devel python-devel zlib-devel"
    "yum -y install openssl-devel"
 
    "yum install rpm-build redhat-rpm-config asciidoc hmaccalc perl-ExtUtils-Embed pesign xmlto"
    "yum install audit-libs-devel binutils-devel elfutils-devel elfutils-libelf-devel"
    "yum install ncurses-devel newt-devel numactl-devel pciutils-devel python-devel zlib-devel"
 
    從這些地方找尋內核的源代碼套件:
    http://vault.centos.org/6.N/os/Source/SPackages/
    http://vault.centos.org/6.N/updates/Source/SPackages/
    or 直接抓取
    "rpm -i http://vault.centos.org/6.9/updates/Source/SPackages/kernel-2.6.32-696.28.1.el6.src.rpm 2>&1 | grep -v exist"
 
    Build 出 CentOS source code,
    "cd ~/rpmbuild/SPECS"
    "rpmbuild -bp --target=$(uname -m) kernel.spec"
 
    Centos source code in /root/rpmbuild/BUILD/kernel-2.6.32-696.28.1.el6/linux-2.6.32-696.28.1.el6.x86_64
 
    PS. 若發現GCC太舊,請update gcc "yum update gcc"

 
##  安裝網路設定命令工具
    #系統設定套件 - setup 工具包
    yum -y install setuptool
   
    #系統設定套件 - 服務套件
    yum -y install ntsysv
   
    #系統設定套件 - 網路管理
    yum -y install system-config-network-tui
   
    #系統設定套件 - 防火牆設定
    yum -y install system-config-firewall-tui
   
    #系統設定套件 - 安全性設定
    yum -y install system-config-securitylevel-tui

How To Install lm_sensors on a CentOS / RHEL / Scientific Linux

##  How To Install lm_sensors on a CentOS / RHEL / Scientific Linux
   
    **  To install lm-sensors on a CentOS, RHEL, Scientific and older version of Fedora Linux
          (fedora v21 or older) to monitor fan speed and cpu temp info, type:
          $ sudo yum install lm_sensors
       
    **  Once lm_sensors is installed you need to type:
          $ sudo sensors-detect
       
    **  Next hit ENTER key for everything it suggests. At the end it will ask you whether to add
          what it finds to /etc/modules. If you are happy with the output i.e. findings type “yes”.
         To see the values for the hardware sensors detected on your motherboard / server, type:
         $ sensors
       
    **  Sample outputs:
         coretemp-isa-0000
         Adapter: ISA adapter
         Physical id 0:  +37.0?C  (high = +80.0?C, crit = +90.0?C)
         Core 0:         +32.0?C  (high = +80.0?C, crit = +90.0?C)
         Core 1:         +29.0?C  (high = +80.0?C, crit = +90.0?C)
   
    **  Or use the following command:
         $ sensors | less
         $ sensors | egrep -i --color 'fan|cpu|temp' 
       
##  讀取CPU溫度
        cat /sys/class/thermal/thermal_zone1/temp

CentOS 6.9 eth setting

##  CentOS 6.9 eth setting

    **  "cat /etc/sysconfig/network-scripts/ifcfg-eth0"
   
    DEVICE=eth0
    HWADDR=00:30:18:00:e1:65
    TYPE=Ethernet
    UUID=87093b7d-de84-4418-bbdd-a49f21a0be9d
    ONBOOT=yes
    NM_CONTROLLED=yes
    BOOTPROTO=none
    USERCTL=no
    PEERDNS=yes
    IPV6INIT=no
    IPADDR=10.10.8.190
    GATEWAY=10.10.8.254
    NETMASK=255.255.255.0
    DNS1=10.10.0.9
    DNS2=10.10.0.10
 
##  DNS 設定
   
    /etc/hosts :這個是最早的 hostname 對應 IP 的檔案;
    /etc/resolv.conf :這個重要!就是 ISP 的 DNS 伺服器 IP 記錄處;
    /etc/nsswitch.conf:這個檔案則是在『決定』先要使用 /etc/hosts 還是 /etc/resolv.conf 的設定!
   
    "vim /etc/resolv.conf"
    nameserver 168.95.1.1
    nameserver 139.175.10.20
   
##  CentOS 新增網卡
    **找出目前所有網卡
    dmesg | grep -in eth
    387:[    2.074571] e1000 0000:00:03.0 eth0: (PCI:33MHz:32-bit) 08:00:27:37:d5:5a
    388:[    2.074587] e1000 0000:00:03.0 eth0: Intel(R) PRO/1000 Network Connection
    412:[    2.571363] e1000 0000:00:08.0 eth1: (PCI:33MHz:32-bit) 08:00:27:03:28:44
    413:[    2.571371] e1000 0000:00:08.0 eth1: Intel(R) PRO/1000 Network Connection
    417:[    2.588359] systemd-udevd[244]: renamed network interface eth1 to enp0s8
    420:[    2.597290] systemd-udevd[241]: renamed network interface eth0 to enp0s3
     
    **找出新網卡 mac address
    ifconfig
    enp0s3: flags=4163  mtu 1500
            inet 192.168.56.101  netmask 255.255.255.0  broadcast 192.168.56.255
            inet6 fe80::a00:27ff:fe37:d55a  prefixlen 64  scopeid 0x20
            ether 08:00:27:37:d5:5a  txqueuelen 1000  (Ethernet)
            RX packets 6911  bytes 3960745 (3.7 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 6048  bytes 1263062 (1.2 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
       
    **產生新網卡的 UUID
    # uuidgen enp0s8
    297daa1e-a83d-471e-b70d-83eafb2f2470


    **產生新網卡的 eth-scripts
    cd /etc/sysconfig/network-scripts/
    # cp ifcfg-enp0s3 ifcfg-enp0s8

    AME、UUID、DEVICE、HWADDR修改為新網卡資料
    # vi ifcfg-enp0s8
    TYPE=Ethernet
    BOOTPROTO=dhcp
    DEFROUTE=yes
    IPV4_FAILURE_FATAL=no
    IPV6INIT=yes
    IPV6_AUTOCONF=yes
    IPV6_DEFROUTE=yes
    IPV6_FAILURE_FATAL=no
    NAME=enp0s8
    UUID=297daa1e-a83d-471e-b70d-83eafb2f2470
    DEVICE=enp0s8
    HWADDR=08:00:27:03:28:44
    ONBOOT=yes
    PEERDNS=yes
    PEERROUTES=yes
    IPV6_PEERDNS=yes
    IPV6_PEERROUTES=yes