zoukankan      html  css  js  c++  java
  • 自制Linux操作系统

                 自制Linux操作系统

                                              作者:尹正杰

    版权声明:原创作品,谢绝转载!否则将追究法律责任。

    一.添加一块新的磁盘设备

    1>.将虚拟机关机,点击"编辑虚拟机设置"

    2>.点击"添加"按钮

    3>.选择添加虚拟硬件的类型为"硬盘"并点击下一步

    4>.点击下一步

    5>.点击下一步

    6>.点击"将虚拟磁盘存储为单个文件"并点击下一步

    7>.自定义新硬盘的名称

    8>.点击保存按钮

    9>.启动虚拟机

    二.分区并创建文件系统

    1>.查看硬盘资源

    [root@yinzhengjie ~]# lsblk 
    NAME                          MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    sr0                            11:0    1   3.7G  0 rom  
    sda                             8:0    0   500G  0 disk 
    ├─sda1                          8:1    0   500M  0 part /boot
    └─sda2                          8:2    0 499.5G  0 part 
      ├─vg_node200-lv_root (dm-0) 253:0    0    50G  0 lvm  /
      ├─vg_node200-lv_swap (dm-1) 253:1    0   3.9G  0 lvm  [SWAP]
      └─vg_node200-lv_home (dm-2) 253:2    0 445.7G  0 lvm  /home
    sdb                             8:16   0    20G  0 disk 
    [root@yinzhengjie ~]# 

    2>.使用fdisk命令对sdb硬盘进行分区操作

    [root@yinzhengjie ~]# lsblk 
    NAME                          MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    sr0                            11:0    1   3.7G  0 rom  
    sda                             8:0    0   500G  0 disk 
    ├─sda1                          8:1    0   500M  0 part /boot
    └─sda2                          8:2    0 499.5G  0 part 
      ├─vg_node200-lv_root (dm-0) 253:0    0    50G  0 lvm  /
      ├─vg_node200-lv_swap (dm-1) 253:1    0   3.9G  0 lvm  [SWAP]
      └─vg_node200-lv_home (dm-2) 253:2    0 445.7G  0 lvm  /home
    sdb                             8:16   0    20G  0 disk 
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# fdisk /dev/sdb 
    Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
    Building a new DOS disklabel with disk identifier 0xbebd0e26.
    Changes will remain in memory only, until you decide to write them.
    After that, of course, the previous content won't be recoverable.
    
    Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
    
    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): n
    Command action
       e   extended
       p   primary partition (1-4)
    p
    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): p
    
    Disk /dev/sdb: 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: 0xbebd0e26
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1         132     1060258+  83  Linux
    
    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): +10G
    
    Command (m for help): p
    
    Disk /dev/sdb: 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: 0xbebd0e26
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1         132     1060258+  83  Linux
    /dev/sdb2             133        1438    10490445   83  Linux
    
    Command (m for help): n
    Command action
       e   extended
       p   primary partition (1-4)
    p
    Partition number (1-4): 3
    First cylinder (1439-2610, default 1439): 
    Using default value 1439
    Last cylinder, +cylinders or +size{K,M,G} (1439-2610, default 2610): +2G
    
    Command (m for help): t
    Partition number (1-4): 3
    Hex code (type L to list codes): 82
    Changed system type of partition 3 to 82 (Linux swap / Solaris)
    
    Command (m for help): p
    
    Disk /dev/sdb: 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: 0xbebd0e26
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1               1         132     1060258+  83  Linux
    /dev/sdb2             133        1438    10490445   83  Linux
    /dev/sdb3            1439        1700     2104515   82  Linux swap / Solaris
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# lsblk 
    NAME                          MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    sr0                            11:0    1   3.7G  0 rom  
    sda                             8:0    0   500G  0 disk 
    ├─sda1                          8:1    0   500M  0 part /boot
    └─sda2                          8:2    0 499.5G  0 part 
      ├─vg_node200-lv_root (dm-0) 253:0    0    50G  0 lvm  /
      ├─vg_node200-lv_swap (dm-1) 253:1    0   3.9G  0 lvm  [SWAP]
      └─vg_node200-lv_home (dm-2) 253:2    0 445.7G  0 lvm  /home
    sdb                             8:16   0    20G  0 disk 
    ├─sdb1                          8:17   0     1G  0 part 
    ├─sdb2                          8:18   0    10G  0 part 
    └─sdb3                          8:19   0     2G  0 part 
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# fdisk /dev/sdb

    3>.对分区的磁盘进行格式化操作

    [root@yinzhengjie ~]# lsblk 
    NAME                          MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    sr0                            11:0    1   3.7G  0 rom  
    sda                             8:0    0   500G  0 disk 
    ├─sda1                          8:1    0   500M  0 part /boot
    └─sda2                          8:2    0 499.5G  0 part 
      ├─vg_node200-lv_root (dm-0) 253:0    0    50G  0 lvm  /
      ├─vg_node200-lv_swap (dm-1) 253:1    0   3.9G  0 lvm  [SWAP]
      └─vg_node200-lv_home (dm-2) 253:2    0 445.7G  0 lvm  /home
    sdb                             8:16   0    20G  0 disk 
    ├─sdb1                          8:17   0     1G  0 part 
    ├─sdb2                          8:18   0    10G  0 part 
    └─sdb3                          8:19   0     2G  0 part 
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# mkfs.ext4 /dev/sdb1 
    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 29 mounts or
    180 days, whichever comes first.  Use tune2fs -c or -i to override.
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# mkfs.ext4 /dev/sdb1
    [root@yinzhengjie ~]# mkfs.ext4 /dev/sdb2
    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
    655776 inodes, 2622611 blocks
    131130 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=2688548864
    81 block groups
    32768 blocks per group, 32768 fragments per group
    8096 inodes per group
    Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
    
    Writing inode tables: done                            
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done
    
    This filesystem will be automatically checked every 27 mounts or
    180 days, whichever comes first.  Use tune2fs -c or -i to override.
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# mkfs.ext4 /dev/sdb2
    [root@yinzhengjie ~]# mkswap /dev/sdb3 
    Setting up swapspace version 1, size = 2104508 KiB
    no label, UUID=7cfded20-b58a-4131-9055-23c4842c7dca
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# mkswap /dev/sdb3
    [root@yinzhengjie ~]# blkid 
    /dev/mapper/vg_node200-lv_root: UUID="fb2cc473-bcf1-4e0d-9fff-71f7aa018cc6" TYPE="ext4" 
    /dev/sda1: UUID="6148dc57-76a2-4214-b4d5-5af9c5b40576" TYPE="ext4" 
    /dev/sda2: UUID="MS8oog-4gRp-iB4D-bpwh-G9R0-BQ2R-U2wseb" TYPE="LVM2_member" 
    /dev/sdb1: UUID="fa9f112f-de3c-49e0-a14f-f33213047bb2" TYPE="ext4" 
    /dev/sdb2: UUID="6c60d0e6-5cca-443a-8123-c163f3523ce0" TYPE="ext4" 
    /dev/sdb3: UUID="7cfded20-b58a-4131-9055-23c4842c7dca" TYPE="swap" 
    /dev/mapper/vg_node200-lv_swap: UUID="a0fd4399-0a45-48ac-817a-3b6c830f63b4" TYPE="swap" 
    /dev/mapper/vg_node200-lv_home: UUID="12c57023-efb5-4f61-8c90-c672a3a24dcc" TYPE="ext4" 
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# 

     

    三.挂载boot

    1>.创建挂载点(注意, 子目录必须为boot哟~)

    [root@yinzhengjie ~]# mkdir /mylinux/boot -pv        
    mkdir: created directory `/mylinux'
    mkdir: created directory `/mylinux/boot'
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# 

    2>.将规划的boot分区挂载到咱们创建的挂载点上

    [root@yinzhengjie ~]# mount /dev/sdb1 /mylinux/boot/
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# ll /mylinux/boot/
    total 16
    drwx------ 2 root root 16384 Dec  2 21:32 lost+found
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]#  

    3>.安装grub

    [root@yinzhengjie ~]# ll /mylinux/boot/
    total 16
    drwx------ 2 root root 16384 Dec  2 21:32 lost+found
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# grub-install --root-directory=/mylinux/ /dev/sdb
    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 /mylinux//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
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# ll /mylinux/boot/
    total 20
    drwxr-xr-x 2 root root  4096 Dec  2 22:04 grub
    drwx------ 2 root root 16384 Dec  2 21:32 lost+found
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# ls /mylinux/boot/grub/
    device.map     fat_stage1_5  iso9660_stage1_5  minix_stage1_5     stage1  ufs2_stage1_5    xfs_stage1_5
    e2fs_stage1_5  ffs_stage1_5  jfs_stage1_5      reiserfs_stage1_5  stage2  vstafs_stage1_5
    [root@yinzhengjie ~]# 

    4>.恢复内核和initramfs文件(即安装内核文件,我这里直接从系统拷贝就懒得挂载光盘安装啦)

    [root@yinzhengjie ~]# cp /boot/vmlinuz-2.6.32-754.el6.x86_64 /mylinux/boot/vmlinuz
    [root@yinzhengjie ~]# cp /boot/initramfs-2.6.32-754.el6.x86_64.img /mylinux/boot/initramfs.img
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# ll /mylinux/boot/
    total 29396
    drwxr-xr-x 2 root root     4096 Dec  2 22:04 grub
    -rw------- 1 root root 25761655 Dec  2 22:07 initramfs.img
    drwx------ 2 root root    16384 Dec  2 21:32 lost+found
    -rwxr-xr-x 1 root root  4315504 Dec  2 22:06 vmlinuz
    [root@yinzhengjie ~]#
    [root@yinzhengjie ~]# vim /mylinux/boot/grub/grub.conf
    [root@yinzhengjie ~]#
    [root@yinzhengjie ~]# cat /mylinux/boot/grub/grub.conf      #自定义grub.conf配置文件
    default=0
    timeout=3
    title mylinux
    kernel /vmlinuz root=/dev/sda2 selinux=0 init=/bin/bash
    initrd /initramfs.img
    [root@yinzhengjie ~]#

    四.复制bash,常用命令和相关库文件

    1>.创建一级目录

    [root@yinzhengjie ~]# mkdir /mylinux/sysroot
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# mount /dev/sdb2 /mylinux/sysroot/
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# mkdir -pv /mylinux/sysroot/{etc,lib,lib64,bin,sbin,tmp,var,usr,sys,proc,opt,home,root,boot,dev,mnt,media}
    mkdir: created directory `/mylinux/sysroot/etc'
    mkdir: created directory `/mylinux/sysroot/lib'
    mkdir: created directory `/mylinux/sysroot/lib64'
    mkdir: created directory `/mylinux/sysroot/bin'
    mkdir: created directory `/mylinux/sysroot/sbin'
    mkdir: created directory `/mylinux/sysroot/tmp'
    mkdir: created directory `/mylinux/sysroot/var'
    mkdir: created directory `/mylinux/sysroot/usr'
    mkdir: created directory `/mylinux/sysroot/sys'
    mkdir: created directory `/mylinux/sysroot/proc'
    mkdir: created directory `/mylinux/sysroot/opt'
    mkdir: created directory `/mylinux/sysroot/home'
    mkdir: created directory `/mylinux/sysroot/root'
    mkdir: created directory `/mylinux/sysroot/boot'
    mkdir: created directory `/mylinux/sysroot/dev'
    mkdir: created directory `/mylinux/sysroot/mnt'
    mkdir: created directory `/mylinux/sysroot/media'
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# ll /mylinux/sysroot/
    total 84
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 bin
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 boot
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 dev
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 etc
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 home
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 lib
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 lib64
    drwx------ 2 root root 16384 Dec  2 21:32 lost+found
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 media
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 mnt
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 opt
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 proc
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 root
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 sbin
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 sys
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 tmp
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 usr
    drwxr-xr-x 2 root root  4096 Dec  2 22:15 var
    [root@yinzhengjie ~]# 

    2>.复制bash,常用命令和相关库文件

    [root@yinzhengjie ~]# cat copycmd.sh 
    #!/bin/bash
    #********************************************************************
    #Author:              yinzhengjie
    #QQ:                  1053419035
    #Date:                2019-11-23
    #FileName:           copycmd
    #URL:                 http://www.cnblogs.com/yinzhengjie
    #Description:        Copy command and script of dependent Library
    #Copyright notice:    original works, no reprint! Otherwise, legal liability will be investigated.
    #********************************************************************
    
    
    read -t 30 -p "Plesea input sysroot:>>> " ch_root
    
    [ ! -d $ch_root ] && mkdir $ch_root
     
    bincopy() {
        if which $1 &>/dev/null; then
    
            local cmd_path=`which --skip-alias $1`
            local bin_dir=`dirname $cmd_path`
            [ -d ${ch_root}${bin_dir} ] || mkdir -p ${ch_root}${bin_dir}
            [ -f ${ch_root}${cmd_path} ] || cp $cmd_path ${ch_root}${bin_dir}
            return 0
        else
            echo "Command not found."
            return 1
        fi
    }
     
    libcopy() {
        local lib_list=$(ldd `which --skip-alias $1` | grep -Eo '/[^[:space:]]+')
        for loop in $lib_list;do
            local lib_dir=`dirname $loop`
            [ -d ${ch_root}${lib_dir} ] || mkdir -p  ${ch_root}${lib_dir}
            [ -f ${ch_root}${loop} ] || cp $loop ${ch_root}${lib_dir}
        done
    }
     
     
    read -p "Please input a command: " command
     
    while [ "$command" != "quit" ];do
        if bincopy $command ;then
            libcopy $command
        fi
        read -p "Please input a command or quit: " command
    done
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# cat copycmd.sh
    [root@yinzhengjie ~]# bash copycmd.sh 
    Plesea input sysroot:>>> /mylinux/sysroot
    Please input a command: bash
    Please input a command or quit: modprobe
    Please input a command or quit: ifconfig
    Please input a command or quit: ls
    Please input a command or quit: mount
    Please input a command or quit: ping
    Please input a command or quit: cat
    Please input a command or quit: vim
    Please input a command or quit: blkid
    Please input a command or quit: cp
    Please input a command or quit: mv
    Please input a command or quit: df
    Please input a command or quit: insmod
    Please input a command or quit: quit
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# locate e1000
    /lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000
    /lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000e
    /lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000/e1000.ko
    /lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000e/e1000e.ko
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# cp /lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mylinux/sysroot/lib      #拷贝网卡驱动
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# tree /mylinux/sysroot/
    /mylinux/sysroot/
    ├── bin
    │   ├── bash
    │   ├── cat
    │   ├── cp
    │   ├── df
    │   ├── ls
    │   ├── mount
    │   ├── mv
    │   └── ping
    ├── boot
    ├── dev
    ├── etc
    ├── home
    ├── lib
    │   └── e1000.ko
    ├── lib64
    │   ├── ld-linux-x86-64.so.2
    │   ├── libacl.so.1
    │   ├── libattr.so.1
    │   ├── libblkid.so.1
    │   ├── libcap.so.2
    │   ├── libcrypt.so.1
    │   ├── libc.so.6
    │   ├── libdl.so.2
    │   ├── libfreebl3.so
    │   ├── libidn.so.11
    │   ├── libm.so.6
    │   ├── libnsl.so.1
    │   ├── libpthread.so.0
    │   ├── libresolv.so.2
    │   ├── librt.so.1
    │   ├── libselinux.so.1
    │   ├── libsepol.so.1
    │   ├── libtinfo.so.5
    │   ├── libutil.so.1
    │   └── libuuid.so.1
    ├── lost+found
    ├── media
    ├── mnt
    ├── opt
    ├── proc
    ├── root
    ├── sbin
    │   ├── blkid
    │   ├── ifconfig
    │   ├── insmod
    │   └── modprobe
    ├── sys
    ├── tmp
    ├── usr
    │   ├── bin
    │   │   └── vim
    │   └── lib64
    │       ├── libgpm.so.2
    │       └── perl5
    │           └── CORE
    │               └── libperl.so
    └── var
    
    22 directories, 36 files
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# tree /mylinux/sysroot/

     

    五.将硬盘放在另外一台虚拟机中使用

    1>.将我们制作好的虚拟机硬盘拷贝出来

    2>.在新的虚拟机中删除其自带的硬盘并添加一块新硬盘,选择"使用现有虚拟磁盘"并点击"下一步"

     

    3>.点击完成后,启动操作系统

     

    4>.启动虚拟机,发现这个菜单是咱们自定义的

     

    5>.按一下回车键盘就会进入到一个bash中

    6>.安装网卡驱动

    7>自制的Linux操作系统总结

      其实本篇博客案例的所制作的linux系统压根就不能在生产环境中使用,即使使用也是测试环境,这个这是告诉大家一个如何制作Linux的方案而已。
    
      有了上面的操作基础我们大致明白了制作操作系统的方法和步骤,根据根据咱们的需求来定制化咱们需要的软件环境。一般情况下官方发布的标准版已经够咱们自己使用了。
    
      博主推荐阅读:
        http://www.linuxfromscratch.org/lfs/download.html
        http://www.linuxfromscratch.org/lfs/downloads/stable/LFS-BOOK-9.0.pdf

     

  • 相关阅读:
    MySQL之ORM
    MySQL之索引补充
    MySQL存储过程
    c primer plus 7编程练习
    c语言中统计单词数目程序
    c语言统计输入字符数及行数
    c语言中getchar()、putchar()函数例子
    c primer plus 6编程练习
    c语言 %c 一次输出多个字符 (特殊程序)
    c语言 复合赋值运算符的优先级低于算术运算符
  • 原文地址:https://www.cnblogs.com/yinzhengjie/p/11967797.html
Copyright © 2011-2022 走看看