27-2017-12-27-练习-自定linux和编译内核

练习1-定制linux

1、定制一个linux

准备磁盘并分区格式化

[root@centos66 Bash]$ fdisk /dev/sdc

WARNING: GPT (GUID Partition Table) detected on '/dev/sdc'! The util fdisk doesn't support GPT. Use GNU Parted.


WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
                switch off the mode (command 'c') and change display units to
                sectors (command 'u').

Command (m for help): p

Disk /dev/sdc: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa55d77b3

Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
p
Partition number (1-4):
Value out of range.
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1G

Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (133-2610, default 133):
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-2610, default 2610):
Using default value 2610

Command (m for help): p

Disk /dev/sdc: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa55d77b3

Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1         132     1060258+  83  Linux
/dev/sdc2             133        2610    19904535   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos66 Bash]$ partprobe
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.
Warning: /dev/sdc contains GPT signatures, indicating that it has a GPT table.  However, it does not have a valid fake msdos partition table, as it should.  Perhaps it was corrupted -- possibly by a program that doesn't understand GPT partition tables.  Or perhaps you deleted the GPT table, and are now using an msdos partition table.  Is this a GPT partition table?
Warning: Unable to open /dev/sr1 read-write (Read-only file system).  /dev/sr1 has been opened read-only.
[root@centos66 Bash]$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1 1024M  0 rom
sr1     11:1    1  5.8G  0 rom
sr2     11:2    1 1024M  0 rom
sr3     11:3    1 1024M  0 rom
sda      8:0    0  200G  0 disk
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0 48.8G  0 part /
├─sda3   8:3    0 19.5G  0 part /app
├─sda4   8:4    0    1K  0 part
├─sda5   8:5    0    2G  0 part [SWAP]
└─sda6   8:6    0    1G  0 part
sdb      8:16   0  100G  0 disk
├─sdb1   8:17   0    1G  0 part
├─sdb2   8:18   0   20G  0 part
├─sdb3   8:19   0    1G  0 part
├─sdb4   8:20   0    1K  0 part
├─sdb5   8:21   0   20G  0 part
├─sdb6   8:22   0    1G  0 part
└─sdb7   8:23   0    1G  0 part
sdc      8:32   0   20G  0 disk
├─sdc1   8:33   0    1G  0 part
└─sdc2   8:34   0   19G  0 part
[root@centos66 Bash]$ mkfs
mkfs          mkfs.cramfs   mkfs.ext2     mkfs.ext3     mkfs.ext4     mkfs.ext4dev  mkfs.msdos    mkfs.vfat
[root@centos66 Bash]$ mkfs
mkfs          mkfs.cramfs   mkfs.ext2     mkfs.ext3     mkfs.ext4     mkfs.ext4dev  mkfs.msdos    mkfs.vfat
[root@centos66 Bash]$ mkfs.ext4 /dev/sdc1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
66384 inodes, 265064 blocks
13253 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=272629760
9 block groups
32768 blocks per group, 32768 fragments per group
7376 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@centos66 Bash]$ mkfs.ext4 /dev/sdc2
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1245184 inodes, 4976133 blocks
248806 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
152 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@centos66 Bash]$ cls

安装grub,复制文件,grub配置文件启用

[root@centos66 Bash]$ mkdir /mnt/boot
[root@centos66 Bash]$ mount /dev/sdc1 /mnt/boot
[root@centos66 Bash]$ grub-install  --root-directory=/mnt /dev/sdc
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /mnt/boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script 'grub-install'.

(fd0)   /dev/fd0
(hd0)   /dev/sda
(hd1)   /dev/sdb
(hd2)   /dev/sdc

[root@centos66 mnt]$ cd /mnt/boot
[root@centos66 boot]$ ls
grub  lost+found
[root@centos66 boot]$ cp /boot/vmlinuz-2.6.32-696.el6.x86_64  .
[root@centos66 boot]$ cp /boot/initramfs-2.6.32-696.el6.x86_64.img .
[root@centos66 boot]$ cp /boot/grub/grub.conf grub/
[root@centos66 ~]$ vim /mnt/boot/grub/grub.conf
[root@centos66 ~]$ cat /mnt/boot/grub/grub.conf
default=0
timeout=5
title centos6.9
kernel /vmlinuz-2.6.32-696.el6.x86_64 root=/dev/sda2  selinux=0 init=/bin/bash
initrd /initramfs-2.6.32-696.el6.x86_64.img

[root@centos66 boot]$ tree
.
├── grub
│   ├── device.map
│   ├── e2fs_stage1_5
│   ├── fat_stage1_5
│   ├── ffs_stage1_5
│   ├── grub.conf
│   ├── iso9660_stage1_5
│   ├── jfs_stage1_5
│   ├── minix_stage1_5
│   ├── reiserfs_stage1_5
│   ├── stage1
│   ├── stage2
│   ├── ufs2_stage1_5
│   ├── vstafs_stage1_5
│   └── xfs_stage1_5
├── initramfs-2.6.32-696.el6.x86_64.img
├── lost+found
└── vmlinuz-2.6.32-696.el6.x86_64

2 directories, 16 files

创建根目录

[root@centos66 ~]$ mkdir /mnt/sysroot
[root@centos66 ~]$ mount /dev/sdc2 /mnt/sysroot/
[root@centos66 ~]$ ll /
total 168
drwxr-xr-x.   7 root root  4096 Dec 15 02:35 app
dr-xr-xr-x.   2 root root  4096 Nov 26 12:58 bin
dr-xr-xr-x.   3 root root  4096 Dec 16  2017 boot
drwxr-xr-x.   2 root root  4096 Nov 26 12:58 build
drwxr-xr-x.   2 root root  4096 Nov 26 12:44 cgi-bin
drwxr-xr-x.   4 root root  4096 Nov 26 12:44 conf
drwxr-xr-x   20 root root  4560 Dec 16 06:51 dev
drwxr-xr-x.   3 root root  4096 Nov 26 12:44 error
drwxr-xr-x. 127 root root 12288 Dec 16 06:58 etc
drwxr-xr-x.   3 root root  4096 Dec 14 07:21 git
drwxr-xr-x.  27 root root  4096 Dec 10 21:40 home
drwxr-xr-x.   2 root root  4096 Jul  7 03:19 htdocs
drwxr-xr-x.   3 root root  4096 Nov 26 12:44 icons
drwxr-xr-x.   2 root root  4096 Nov 26 12:58 include
dr-xr-xr-x.  11 root root  4096 Nov 26 12:25 lib
dr-xr-xr-x.   9 root root 12288 Nov 26 12:54 lib64
drwxr-xr-x.   2 root root  4096 Nov 26 12:44 logs
drwx------.   2 root root 16384 Nov  7 15:26 lost+found
drwxr-xr-x.   4 root root  4096 Nov 26 12:44 man
drwxr-xr-x.  14 root root 12288 Jul  7 03:20 manual
drwxr-xr-x.   6 root root  4096 Dec 10 04:32 media
drwxr-xr-x    2 root root     0 Dec 16 06:42 misc
drwxr-xr-x.   9 root root  4096 Dec 16 06:58 mnt
drwxr-xr-x.   2 root root  4096 Nov 26 12:44 modules
drwxr-xr-x    2 root root     0 Dec 16 06:42 net
drwxr-xr-x.   3 root root  4096 Nov  7 15:38 opt
dr-xr-xr-x  151 root root     0 Dec 16  2017 proc
dr-xr-x---.  38 root root  4096 Dec 16 06:48 root
dr-xr-xr-x.   2 root root 12288 Nov 26 12:55 sbin
drwxr-xr-x.   2 root root  4096 Nov  7 15:31 selinux
drwxr-xr-x.   2 root root  4096 Sep 23  2011 srv
drwxr-xr-x   13 root root     0 Dec 16  2017 sys
drwxr-xr-x.   3 root root  4096 Nov 13 01:43 testdir
drwxrwxrwt.  17 root root  4096 Dec 16 06:54 tmp
drwxr-xr-x.  13 root root  4096 Nov  7 15:32 usr
drwxr-xr-x.  22 root root  4096 Nov 26 12:40 var
[root@centos66 ~]$ mkdir -pv /mnt/sysroot/{etc,lib,lib64,bin,sbin,tmp,var,usr,opt,home,root,boot,dev,mnt,media}
mkdir: created directory '/mnt/sysroot/etc'
mkdir: created directory '/mnt/sysroot/lib'
mkdir: created directory '/mnt/sysroot/lib64'
mkdir: created directory '/mnt/sysroot/bin'
mkdir: created directory '/mnt/sysroot/sbin'
mkdir: created directory '/mnt/sysroot/tmp'
mkdir: created directory '/mnt/sysroot/var'
mkdir: created directory '/mnt/sysroot/usr'
mkdir: created directory '/mnt/sysroot/opt'
mkdir: created directory '/mnt/sysroot/home'
mkdir: created directory '/mnt/sysroot/root'
mkdir: created directory '/mnt/sysroot/boot'
mkdir: created directory '/mnt/sysroot/dev'
mkdir: created directory '/mnt/sysroot/mnt'
mkdir: created directory '/mnt/sysroot/media'

复制命令并测试

[root@centos66 ~]$ vim /root/copy_cmd.sh
[root@centos66 ~]$ cat /root/copy_cmd.sh
#!/bin/bash
#================================================
#FileName   :copycmd.sh
#Author     :zhaojiedi
#Description:
#DateTime   :2017-12-22 15:57:39
#Version    :V1.0
#Other      :
#================================================
dst=/mnt/sysroot
overwrite=yes

if [ ! -e "$dst" ] ; then
        mkdir -pv $dst
fi
my_cp() {
        local src=$1
        local dst=$2
        local dir=`dirname $dst`
        if [ ! -e $dir ] ; then
                mkdir -pv $dir
        fi

        if [ ! -f "$dst"  -o "$overwrite" == "yes" ] ; then
                cp  $src $dst
        fi
}


while true; do
        read -p "enter you cmd :  " short_cmd
        if [ "$short_cmd" == "quit" ] ; then
                exit 0
        fi
        type $short_cmd 2>/dev/null |grep -q "builtin"
        if [ "$?" -eq 0 ] ; then
                echo "this is a buildin cmd, nothing todo,  input 'bash' for copy buildin cmd"
                continue
        fi
        short_cmd=`basename $short_cmd` &>/dev/null
        long_cmd=`which --skip-alias --skip-functions --skip-dot --skip-tilde $short_cmd 2>/dev/null`
        if [ "$?" -ne 0 ] ; then
                echo "not a valid command, please check and try again"
                continue
        fi
        # cp bin
        my_cp $long_cmd $dst$long_cmd
        # cp lib
        for lib in `ldd $long_cmd |egrep -o  "/[^[:space:]]+" ` ; do
                #echo -e "\n$lib\n"
                my_cp $lib $dst$lib
        done
        # cp man
        #for man in `man -aw $short_cmd` ; do
        #       my_cp $man ${dst}${man}
        #done
done
[root@centos66 ~]$ /root/copy_cmd.sh
enter you cmd :  bash
enter you cmd :  ifconfig
enter you cmd :  insmod
enter you cmd :  ping
enter you cmd :  mount
enter you cmd :  ls
enter you cmd :  cat
enter you cmd :  stat
mkdir: created directory '/mnt/sysroot/usr/bin'
enter you cmd :  ping
enter you cmd :  nmcli
mkdir: created directory '/mnt/sysroot/usr/lib64'
enter you cmd :  ip
enter you cmd :  stat
enter you cmd :  pwd
this is a buildin cmd, nothing todo,  input 'bash' for copy buildin cmd
enter you cmd :  bash
enter you cmd :  modprobe
enter you cmd :  rmmod
enter you cmd :  mount
enter you cmd :  quit

切根测试

[root@centos66 ~]$ chroot /mnt/sysroot/
bash-4.1# ls
bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  root  sbin  tmp  usr  var
bash-4.1# cd
bash-4.1# ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.454 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.030 ms
^C
--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1712ms
rtt min/avg/max/mdev = 0.030/0.242/0.454/0.212 ms
bash-4.1# nmcli
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }

OPTIONS
-t[erse]                                   terse output
-p[retty]                                  pretty output
-m[ode] tabular|multiline                  output mode
-f[ields] <field1,field2,...>|all|common   specify fields to output
-e[scape] yes|no                           escape columns separators in values
-v[ersion]                                 show program version
-h[elp]                                    print this help

OBJECT
nm          NetworkManager status
con         NetworkManager connections
dev         devices managed by NetworkManager

bash-4.1# exit
exit

[root@centos66 ~]$ modinfo  -n e1000
/lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000/e1000.ko
[root@centos66 ~]$ mkdir /mnt/sysroot/lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net
mkdir: cannot create directory '/mnt/sysroot/lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net': No such file or directory
[root@centos66 ~]$ mkdir /mnt/sysroot/lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net -pv
mkdir: created directory '/mnt/sysroot/lib/modules'
mkdir: created directory '/mnt/sysroot/lib/modules/2.6.32-696.el6.x86_64'
mkdir: created directory '/mnt/sysroot/lib/modules/2.6.32-696.el6.x86_64/kernel'
mkdir: created directory '/mnt/sysroot/lib/modules/2.6.32-696.el6.x86_64/kernel/drivers'
mkdir: created directory '/mnt/sysroot/lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net'
cp /lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000/e1000.ko

最后取出磁盘,作为新系统的第一个硬盘即可

练习2-

1、修改内核参数

[root@centos66 rules.d]$ echo 1 > /proc/sys/net/ipv4/ip_forward
[root@centos66 rules.d]$ sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1
[root@centos66 rules.d]$ sysctl -p

2、给制定硬盘添加软连接

[root@centos66 rules.d]$ cat  /etc/udev/rules.d/80-disk.rules
SUBSYSTEM=="block", KERNEL=="sdb" ,SYMLINK="disk2"