Linux学习-手动制作一个Linux系统

制作思路

  1. 在现有的Linux系统上添加一块硬盘/dev/sdb,给硬盘/dev/sdb上分两个分区,一个是/boot,一个是/,并将其格式化。
  2. 在/dev/sdb制作一个独立的Linux系统,里面的文件需要一个一个拷贝,将来作为/dev/sda的硬盘,首先需要先安装grub。
  3. 作为独立运行的Linux系统,必须有内核文件和initramfs文件。
  4. 然后拷贝对应的程序。
  5. 制作完成。

制作过程

  • 给虚拟机添加一块硬盘在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  • 格式化磁盘/dev/sdb
[root@localhost ~]# 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 0xfdff3257.
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): +50M

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

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: 0xfdff3257

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1           7       56196   83  Linux
/dev/sdb2               8          72      522112+  83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
  • 格式化磁盘
[root@localhost ~]# mke2fs -j /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
14056 inodes, 56196 blocks
2809 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=57671680
7 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961

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

This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# mke2fs -j /dev/sdb2
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
130560 inodes, 522112 blocks
26105 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
64 block groups
8192 blocks per group, 8192 fragments per group
2040 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

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

This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
  • 创建两个目录,进行文件系统挂载
[root@localhost ~]# mount /dev/sdb1 /tye/boot
[root@localhost ~]# mount /dev/sdb2 /tye/sysroot
  • 安装grub到目标磁盘
[root@localhost ~]# grub-install --root-directory=/tye /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 /tye/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@localhost ~]# cd /tye/boot
[root@localhost boot]# ls
grub  lost+found
[root@localhost boot]# cd grub
[root@localhost grub]# ls
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

  • 复制内核文件和initrd文件到/tye/boot目录下
[root@localhost grub]# cp /boot/{initramfs-2.6.32-754.el6.x86_64.img,vmlinuz-2.6.32-754.el6.x86_64} /tye/boot/
  • 建立grub.conf文件
[root@localhost grub]# vim grub.conf
default=0
timeout=3
title tye linux
  kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=/dev/sda2 selinux=0 init=/bin/bash
  initrd /initramfs-2.6.32-754.el6.x86_64
  • 创建一级目录,并复制文件
[root@localhost sysroot]# mkdir home opt sys proc bin sbin usr/{bin,sbin} etc/rc.d dev tmp media mnt -pv
mkdir: created directory `home'
mkdir: created directory `opt'
mkdir: created directory `sys'
mkdir: created directory `proc'
mkdir: created directory `bin'
mkdir: created directory `sbin'
mkdir: created directory `usr'
mkdir: created directory `usr/bin'
mkdir: created directory `usr/sbin'
mkdir: created directory `etc'
mkdir: created directory `etc/rc.d'
mkdir: created directory `dev'
mkdir: created directory `tmp'
mkdir: created directory `media'
mkdir: created directory `mnt'

  • 通过脚本拷贝命令文件和库文件
#!/bin/bash
# 拷贝命令
destdir=/tye/sysroot
logfile=/root/scripts/copylog.txt
#拷贝命令
function copy_cmd() {
  #获得路径
  local cmd_dir=`dirname $1`
  local cmd_name=`basename $1`
  local cmd_destdir=$destdir$cmd_dir/$cmd_name
  cp $1 $cmd_destdir &>/dev/null
  #echo -e "\t $1 copyto $cmd_destdir successfully!!"
  #记录复制日志
  echo -e "\t $1 copyto $cmd_destdir successfully!!" >> $logfile
}
#拷贝依赖库文件
function copy_lib() {
  #通过ldd获取命令所依赖的库文件列表
  local lib_list=`ldd $1 | egrep -o "/.* "`
  for i in $lib_list;
  do
    local lib_destdir=$destdir$i
    if [ -e $destdir$i ];then
       continue
    elif [ -d `dirname $lib_destdir` ];then
       cp $i $lib_destdir
       #记录复制日志
       echo -e "\t $i copyto $lib_destdir successfully!!" >> $logfile
    else
       mkdir -pv $lib_destdir &> /dev/null
       cp $i $lib_destdir
       #记录复制日志
       echo -e "\t $i copyto $lib_destdir successfully!!" >> $logfile
    fi
  done
}
if [ -d $destdir ];then
    mkdir -pv $destdir &>/dev/null
fi
#死循环,清空屏幕,然后输入命令
while true ;do
  #tput clear
  cat << EOF
###########################################################
***                    命令复制脚本                     ***
***                    输入一个命令                     ***
***                    按q,Q退出脚本                    ***
###########################################################
EOF
  read -p "请输入一个可执行命令[/bin/bash]:" cmd
  if [ $cmd == 'q' -o $cmd == 'Q' ];then
    unset cmd
    break
  fi
  #判断输入的命令是否存在
  if [ -n $cmd ];then
    if type $cmd ;then
      copy_cmd $cmd
      copy_lib $cmd
    else
      echo "cmd is not existed"
   fi
  fi
done
  • 复制网卡驱动,添加网络
[root@localhost ~]# modinfo e1000
filename:       /lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000/e1000.ko
version:        7.3.21-k8-NAPI
license:        GPL
description:    Intel(R) PRO/1000 Network Driver
author:         Intel Corporation, <linux.nics@intel.com>
retpoline:      Y
[root@localhost ~]# cp /lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /tye/sysroot/lib64/
  • 以上操作全部完成。就可以关机,然后把/dev/sdb这个硬盘拆下来,放到新的虚拟机上运行了~。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值