码迷,mamicode.com
首页 > 系统相关 > 详细

自制linux系统——打造属于自己的linux系统

时间:2016-09-19 06:48:25      阅读:663      评论:0      收藏:0      [点我收藏+]

标签:创建boot分区   安装grub   手写grub.conf文件   手写fstab文件   复制linux命令及库文件   

    很多时候我们直接使用别人帮我们打包好的一个linux系统,总感觉某些工具或者命令多余或者效果不好,那我们是否可以自制一个比较符合自己需求的linux系统出来了,答案是肯定的,而且实现这个目标也并困难,需要的时间也不会太长。接下来就给大家分享如何自制的过程。


环境需求:

    1、Vmware虚拟机

    2、Centos6.8光盘镜像IS0文件

    3、Vmware上安装一份Centos6.8的操作系统


第一步:装载一块新硬盘到虚拟机的Centos6.8系统上,并保证系统正常读取

  [root@Centos6 ~]# lsblk

  NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

  sda      8:0    0   20G  0 disk 

  ├─sda1   8:1    0  200M  0 part /boot

  ├─sda2   8:2    0    5G  0 part /

  └─sda3   8:3    0    5G  0 part /test

  sr0     11:0    1  3.7G  0 rom  

  [root@Centos6 ~]# echo "- - -" > /sys/class/scsi_host/host0/scan 

  [root@Centos6 ~]# lsblk

  NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

  sda      8:0    0   20G  0 disk 

  ├─sda1   8:1    0  200M  0 part /boot

  ├─sda2   8:2    0    5G  0 part /

  └─sda3   8:3    0    5G  0 part /test

  sr0     11:0    1  3.7G  0 rom  

  sdb      8:16   0   20G  0 disk 

  [root@Centos6 ~]# 


第二步:分区格式化并挂载,boot分区200M,根分区2G,统一格式化成ext4文件系统,boot分区卷标:boot,根分区卷标:root,boot分区挂载到/mnt/boot,根分区挂载到/mnt/root

  [root@Centos6 ~]# 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 0x7a85194e.

  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): +200M


  Command (m for help): n

  Command action

     e   extended

     p   primary partition (1-4)

  p

  Partition number (1-4): 2

  First cylinder (27-2610, default 27): 

  Using default value 27

  Last cylinder, +cylinders or +size{K,M,G} (27-2610, default 2610): +2G


  Command (m for help): w

  The partition table has been altered!


  Calling ioctl() to re-read partition table.

  Syncing disks.

  [root@Centos6 ~]# lsblk

  NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT

  sda      8:0    0    20G  0 disk 

  ├─sda1   8:1    0   200M  0 part /boot

  ├─sda2   8:2    0     5G  0 part /

  └─sda3   8:3    0     5G  0 part /test

  sr0     11:0    1   3.7G  0 rom  

  sdb      8:16   0    20G  0 disk 

  ├─sdb1   8:17   0 203.9M  0 part 

  └─sdb2   8:18   0     2G  0 part 

  [root@Centos6 ~]# mkfs.ext4 -L boot /dev/sdb1

  mke2fs 1.41.12 (17-May-2010)

  Filesystem label=boot

  OS type: Linux

  Block size=1024 (log=0)

  Fragment size=1024 (log=0)

  Stride=0 blocks, Stripe width=0 blocks

  52208 inodes, 208812 blocks

  10440 blocks (5.00%) reserved for the super user

  First data block=1

  Maximum filesystem blocks=67371008

  26 block groups

  8192 blocks per group, 8192 fragments per group

  2008 inodes per group

  Superblock backups stored on blocks: 

  8193, 24577, 40961, 57345, 73729, 204801


  Writing inode tables: done                            

  Creating journal (4096 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@Centos6 ~]# mkfs.ext4 -L root /dev/sdb2

  mke2fs 1.41.12 (17-May-2010)

  Filesystem label=root

  OS type: Linux

  Block size=4096 (log=2)

  Fragment size=4096 (log=2)

  Stride=0 blocks, Stripe width=0 blocks

  131648 inodes, 526128 blocks

  26306 blocks (5.00%) reserved for the super user

  First data block=0

  Maximum filesystem blocks=541065216

  17 block groups

  32768 blocks per group, 32768 fragments per group

  7744 inodes per group

  Superblock backups stored on blocks: 

  32768, 98304, 163840, 229376, 294912


  Writing inode tables: done                            

  Creating journal (16384 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@Centos6 ~]# mkdir /mnt/boot

  [root@Centos6 ~]# mkdir /mnt/root

  [root@Centos6 ~]# mount /dev/sdb1 /mnt/boot

  [root@Centos6 ~]# mount /dev/sdb2 /mnt/root

  [root@Centos6 ~]# 


第三步:创建boot分区的所有文件

复制当前操作系统的内核文件和文件系统驱动加载文件到自制linux系统的boot分区下

  [root@Centos6 ~]# cp /boot/vmlinuz-2.6.32-642.el6.x86_64 /boot/initramfs-2.6.32-642.el6.x86_64.img /mnt/boot

  [root@Centos6 ~]# ll /mnt/boot

  total 29167

  -rw-------. 1 root root 25587665 Sep 11 12:54 initramfs-2.6.32-642.el6.x86_64.img

  drwx------. 2 root root    12288 Sep 11 12:40 lost+found

  -rwxr-xr-x. 1 root root  4264528 Sep 11 12:54 vmlinuz-2.6.32-642.el6.x86_64

  [root@Centos6 ~]# 


安装grub到自制linux系统的boot分区下

  [root@Centos6 ~]# grub-install --root-directory=/mnt /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 /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

  [root@Centos6 ~]# tree /mnt/boot

  /mnt/boot

  ├── grub

  │   ├── device.map

  │   ├── e2fs_stage1_5

  │   ├── fat_stage1_5

  │   ├── ffs_stage1_5

  │   ├── 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-642.el6.x86_64.img

  ├── lost+found

  └── vmlinuz-2.6.32-642.el6.x86_64


  2 directories, 15 files

  [root@Centos6 ~]# 


手工创建grub.conf文件

  [root@Centos6 ~]# clear

  [root@Centos6 ~]# vim /mnt/boot/grub/grub.conf

  [root@Centos6 ~]# cat /mnt/boot/grub/grub.conf

      default=0

      timeout=3

  title Centos6.8--lovefirewall

      root (hd0,0)

      kernel /vmlinuz-2.6.32-642.el6.x86_64 root=UUID=062befd6-d27d-4e45-8675-e9b5771ac934 selinux=0 init=/bin/bash

      initrd /initramfs-2.6.32-642.el6.x86_64.img

  [root@Centos6 ~]# 


第四步:创建根分区的一级目录,创建fstab文件,复制自制linux系统上所需要的命令及其库文件

  [root@Centos6 ~]# mkdir /mnt/root/{boot,etc,dev,bin,sbin,lib,lib64,home,root,proc,sys,usr,var,opt,mnt,media}

  [root@Centos6 ~]# ls /mnt/root

  bin   dev  home  lib64       media  opt   root  sys  var

  boot  etc  lib   lost+found  mnt    proc  sbin  usr

  [root@Centos6 ~]# cd /mnt

  [root@Centos6 mnt]# vim root/etc/fstab

  [root@Centos6 mnt]# cat root/etc/fstab

  UUID=8c0add68-89a7-43b6-ade0-10e1647c2531 /boot ext4 defaults 1 1 

  UUID=062befd6-d27d-4e45-8675-e9b5771ac934 /     ext4 defaults 1 2

  [root@Centos6 mnt]# 


由于库文件较多,纯手工复制太过麻烦,在这里我就使用我之前已经编写好的专用于复制命令及库文件的shell脚本来操作(需要本脚本的可以到博文附件中自行下载)

  [root@Centos6 mnt]# ls

  boot  copycmd.sh  root

  [root@Centos6 mnt]# ./copycmd.sh

  Please input a command name or quit: bash

  Please input a command name or quit: poweroff

  Please input a command name or quit: reboot

  Please input a command name or quit: ls

  Please input a command name or quit: pwd

  Please input a command name or quit: cp

  Please input a command name or quit: mv

  Please input a command name or quit: rm

  Please input a command name or quit: touch

  Please input a command name or quit: mkdir

  Please input a command name or quit: cat

  Please input a command name or quit: head

  Please input a command name or quit: tail

  Please input a command name or quit: cut

  Please input a command name or quit: sort

  Please input a command name or quit: uniq

  Please input a command name or quit: paste

  Please input a command name or quit: dd     

  Please input a command name or quit: diff

  Please input a command name or quit: du

  Please input a command name or quit: df

  Please input a command name or quit: clear

  Please input a command name or quit: vim

  Please input a command name or quit: grep

  Please input a command name or quit: egrep

  Please input a command name or quit: fgrep

  Please input a command name or quit: sed

  Please input a command name or quit: awk

  Please input a command name or quit: date

  Please input a command name or quit: clock

  Please input a command name or quit: cal

  Please input a command name or quit: chown

  Please input a command name or quit: chmod

  Please input a command name or quit: lsblk

  Please input a command name or quit: blkid

  Please input a command name or quit: fdisk

  Please input a command name or quit: kpartx

  Please input a command name or quit: mkfs    

  Please input a command name or quit: mkfs.ext3

  Please input a command name or quit: mkfs.ext4

  Please input a command name or quit: mke2fs

  Please input a command name or quit: mkswap

  Please input a command name or quit: swapon

  Please input a command name or quit: swapoff

  Please input a command name or quit: mount

  Please input a command name or quit: umount

  Please input a command name or quit: tune2fs

  Please input a command name or quit: e2label

  Please input a command name or quit: dumpe2fs           

  Please input a command name or quit: e2fsck

  Please input a command name or quit: fuser

  Please input a command name or quit: kill

  Please input a command name or quit: find

  Please input a command name or quit: gzip

  Please input a command name or quit: bzip2

  Please input a command name or quit: xz

  Please input a command name or quit: zip

  Please input a command name or quit: tar

  Please input a command name or quit: gunzip

  Please input a command name or quit: unxz

  Please input a command name or quit: bunzip2

  Please input a command name or quit: cpio

  Please input a command name or quit: lscpu

  Please input a command name or quit: shutdown

  Please input a command name or quit: man

  Please input a command name or quit: more

  Please input a command name or quit: less

  Please input a command name or quit: basename

  Please input a command name or quit: dirname

  Please input a command name or quit: file

  Please input a command name or quit: which

  Please input a command name or quit: whereis

  Please input a command name or quit: stat

  Please input a command name or quit: wc

  Please input a command name or quit: tr

  Please input a command name or quit: bc

  Please input a command name or quit: tee

  Please input a command name or quit: tree

  Please input a command name or quit: test

  Please input a command name or quit: fsck

  Please input a command name or quit: free

  Please input a command name or quit: zcat

  Please input a command name or quit: lsmod

  Please input a command name or quit: quit

  [root@Centos6 mnt]# 


第五步:检验自制linux系统的boot分区和根分区上必须的文件是否完整

制作完成后boot分区的上所有文件(如下所列文件一个都不能少)

  [root@Centos6 mnt]# tree boot

  boot

  ├── 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-642.el6.x86_64.img

  ├── lost+found

  └── vmlinuz-2.6.32-642.el6.x86_64


  2 directories, 16 files

  [root@Centos6 mnt]# 


制作完成后自制linux根分区的上所有文件,/etc/fstab文件一定不能少,否则系统无法自动分区信息,也无法找到根,bash的命令文件和库文件一定不能少,否则系统没shell入口,我们是无法与操作系统交互的,其它命令没有的话进入系统后就没什么功能可用,但不会影响系统的启动。

  [root@Centos6 mnt]# tree root

  root

  ├── bin

  │   ├── awk

  │   ├── basename

  │   ├── bash

  │   ├── cat

  │   ├── chmod

  │   ├── chown

  │   ├── cp

  │   ├── cpio

  │   ├── cut

  │   ├── date

  │   ├── dd

  │   ├── df

  │   ├── egrep

  │   ├── fgrep

  │   ├── find

  │   ├── grep

  │   ├── gunzip

  │   ├── gzip

  │   ├── kill

  │   ├── ls

  │   ├── lsblk

  │   ├── mkdir

  │   ├── more

  │   ├── mount

  │   ├── mv

  │   ├── pwd

  │   ├── rm

  │   ├── sed

  │   ├── sort

  │   ├── tar

  │   ├── touch

  │   ├── umount

  │   └── zcat

  ├── boot

  ├── dev

  ├── etc

  │   └── fstab

  ├── home

  ├── lib

  ├── lib64

  │   ├── ld-linux-x86-64.so.2

  │   ├── libacl.so.1

  │   ├── libattr.so.1

  │   ├── libaudit.so.1

  │   ├── libblkid.so.1

  │   ├── libbz2.so.1

  │   ├── libcap.so.2

  │   ├── libcom_err.so.2

  │   ├── libcrypt.so.1

  │   ├── libc.so.6

  │   ├── libdbus-1.so.3

  │   ├── libdevmapper.so.1.02

  │   ├── libdl.so.2

  │   ├── libe2p.so.2

  │   ├── libext2fs.so.2

  │   ├── libfreebl3.so

  │   ├── libgcc_s.so.1

  │   ├── libm.so.6

  │   ├── libncurses.so.5

  │   ├── libncursesw.so.5

  │   ├── libnih-dbus.so.1

  │   ├── libnih.so.1

  │   ├── libnsl.so.1

  │   ├── libpcre.so.0

  │   ├── libproc-3.2.8.so

  │   ├── libpthread.so.0

  │   ├── libreadline.so.6

  │   ├── libresolv.so.2

  │   ├── librt.so.1

  │   ├── libselinux.so.1

  │   ├── libsepol.so.1

  │   ├── libtinfo.so.5

  │   ├── libudev.so.0

  │   ├── libutil.so.1

  │   ├── libuuid.so.1

  │   └── libz.so.1

  ├── lost+found

  ├── media

  ├── mnt

  ├── opt

  ├── proc

  ├── root

  ├── sbin

  │   ├── blkid

  │   ├── clock

  │   ├── dumpe2fs

  │   ├── e2fsck

  │   ├── e2label

  │   ├── fdisk

  │   ├── fsck

  │   ├── fuser

  │   ├── kpartx

  │   ├── lsmod

  │   ├── mke2fs

  │   ├── mkfs

  │   ├── mkfs.ext3

  │   ├── mkfs.ext4

  │   ├── mkswap

  │   ├── poweroff

  │   ├── reboot

  │   ├── shutdown

  │   ├── swapoff

  │   ├── swapon

  │   └── tune2fs

  ├── sys

  ├── usr

  │   ├── bin

  │   │   ├── bc

  │   │   ├── bunzip2

  │   │   ├── bzip2

  │   │   ├── cal

  │   │   ├── clear

  │   │   ├── diff

  │   │   ├── dirname

  │   │   ├── du

  │   │   ├── file

  │   │   ├── free

  │   │   ├── head

  │   │   ├── less

  │   │   ├── lscpu

  │   │   ├── man

  │   │   ├── paste

  │   │   ├── stat

  │   │   ├── tail

  │   │   ├── tee

  │   │   ├── test

  │   │   ├── tr

  │   │   ├── tree

  │   │   ├── uniq

  │   │   ├── unxz

  │   │   ├── vim

  │   │   ├── wc

  │   │   ├── whereis

  │   │   ├── which

  │   │   ├── xz

  │   │   └── zip

  │   └── lib64

  │       ├── libgpm.so.2

  │       ├── liblzma.so.0

  │       ├── libmagic.so.1

  │       └── perl5

  │           └── CORE

  │               └── libperl.so

  └── var


  21 directories, 124 files

  [root@Centos6 mnt]#


第六步:关闭虚拟机,移除用于自制linux系统的硬盘,新建一个虚拟机,直接使用移除下来这块硬盘,开机启动系统,检验是否能正常启动并进入系统

技术分享


技术分享


技术分享


技术分享

当然这只是一个纯内核加了一些基本的命令组合成的一个微linux系统,要想让它能够产生比较强大的功能,还需要做较多的后续工作,当然能进行到这一步,说明我们自制linux的心愿已经实现了。

本文出自 “爱情防火墙” 博客,请务必保留此出处http://183530300.blog.51cto.com/894387/1853825

自制linux系统——打造属于自己的linux系统

标签:创建boot分区   安装grub   手写grub.conf文件   手写fstab文件   复制linux命令及库文件   

原文地址:http://183530300.blog.51cto.com/894387/1853825

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!