Linux 手動製作U盤系統

   這兩天學習了Linux的系統方面的東西,學會了一些簡單的排錯和用現成的文件來製作一個U盤的Linux系統,下面就來演示一下詳細的步驟。


1、在centos6.9上插入u盤 看下U盤的相關信息wKiom1lh4E7zj5KQAABoAT7CETE350.png

(這邊爲了方便操作我就切換到xshell上進行操作)

2、利用fdisk命令對U盤進行分區格式化

[root@centos6 ~]# fdisk /dev/sdb

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/sdb: 32.2 GB, 32245809152 bytes
256 heads, 63 sectors/track, 3905 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          39      314464+  83  Linux
/dev/sdb2              40        1340    10491264   83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos6 ~]# mkfs.ext4 /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
78624 inodes, 314464 blocks
15723 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
39 block groups
8192 blocks per group, 8192 fragments per group
2016 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729, 204801, 221185

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

This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@centos6 ~]# mkfs.ext4 /dev/sdb2


3、創建一個掛載點,並將我們剛剛建立的2個U盤分區掛載上去

[root@centos6 ~]# mkdir -p /tmp/boot
[root@centos6 ~]# ls /tmp
boot            keyring-dQVPwJ  keyring-MtYRmg  keyring-U1wqmD       virtual-root.4yczcz  virtual-root.ivKObv  virtual-root.QfwDEj  virtual-root.YpwmBN
keyring-0PXRKt  keyring-FAGGVf  keyring-OjUrUw  keyring-U6CkKb       virtual-root.935Iku  virtual-root.Kf3cCN  virtual-root.QKOJ49  virtual-root.yzuNVP
keyring-0zm5yK  keyring-fctEWf  keyring-oVuEAV  keyring-uIU214       virtual-root.BZJAZn  virtual-root.kFzhZg  virtual-root.SzhArD  virtual-root.z2ZJDM
keyring-4SQsBp  keyring-Jj9Uah  keyring-PPS6qi  keyring-UR5rTa       virtual-root.csJuJc  virtual-root.m8oh4c  virtual-root.tF2RFN  virtual-root.Zp1uZO
keyring-9jyhhf  keyring-jjOXEk  keyring-QmUXxK  keyring-xJe77w       virtual-root.Dl64O8  virtual-root.mHOha1  virtual-root.U67uRD
keyring-awemTb  keyring-jNtfXv  keyring-QXIXud  keyring-ZgyZiQ       virtual-root.FBwmZL  virtual-root.P46v4l  virtual-root.wCeOAD
keyring-cG67Bz  keyring-k906K1  keyring-SZmKKd  keyring-ZyIOHx       virtual-root.FRTIbN  virtual-root.PaWX5z  virtual-root.XJiOZD
keyring-CGwjId  keyring-LAL2Yu  keyring-ta1two  virtual-root.0PDZHS  virtual-root.FwrG1I  virtual-root.qDRD78  virtual-root.yMpchT
[root@centos6 ~]# mount /dev/sdb1 /tmp/boot/
[root@centos6 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2      100660656 6056588  89484068   7% /
/dev/sda1         991512   34696    905616   4% /boot
/dev/sdb1         296341    2062    278556   1% /tmp/boot

4、利用grub-install命令將我們的grub相關信息寫入U盤的boot文件中

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

5、查看一下我們寫入的東西,可以明顯的看到/dev/sdb上已經有了MBR的引導文件

[root@centos6 ~]# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sr0     11:0    1   3.7G  0 rom  
sda      8:0    0   200G  0 disk 
├─sda1   8:1    0  1000M  0 part /boot
├─sda2   8:2    0  97.7G  0 part /
├─sda3   8:3    0  48.8G  0 part 
├─sda4   8:4    0     1K  0 part 
├─sda5   8:5    0     1G  0 part 
├─sda6   8:6    0     2G  0 part 
└─sda7   8:7    0 203.9M  0 part 
sdb      8:16   1    30G  0 disk 
├─sdb1   8:17   1 307.1M  0 part /tmp/boot
└─sdb2   8:18   1    10G  0 part 
[root@centos6 ~]# ls /tmp/boot/
grub  lost+found
[root@centos6 ~]# hexdump -^C
[root@centos6 ~]# hexdump -C -n 512 /dev/sdb
00000000  eb 48 90 45 58 46 41 54  20 20 20 00 00 00 00 00  |.H.EXFAT   .....|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 03 02  |................|
00000040  ff 00 00 20 01 00 00 00  00 02 fa 90 90 f6 c2 80  |... ............|
00000050  75 02 b2 80 ea 59 7c 00  00 31 c0 8e d8 8e d0 bc  |u....Y|..1......|
00000060  00 20 fb a0 40 7c 3c ff  74 02 88 c2 52 f6 c2 80  |. ..@|<.t...R...|
00000070  74 54 b4 41 bb aa 55 cd  13 5a 52 72 49 81 fb 55  |tT.A..U..ZRrI..U|
00000080  aa 75 43 a0 41 7c 84 c0  75 05 83 e1 01 74 37 66  |.uC.A|..u....t7f|
00000090  8b 4c 10 be 05 7c c6 44  ff 01 66 8b 1e 44 7c c7  |.L...|.D..f..D|.|
000000a0  04 10 00 c7 44 02 01 00  66 89 5c 08 c7 44 06 00  |....D...f.\..D..|
000000b0  70 66 31 c0 89 44 04 66  89 44 0c b4 42 cd 13 72  |pf1..D.f.D..B..r|
000000c0  05 bb 00 70 eb 7d b4 08  cd 13 73 0a f6 c2 80 0f  |...p.}....s.....|
000000d0  84 f0 00 e9 8d 00 be 05  7c c6 44 ff 00 66 31 c0  |........|.D..f1.|
000000e0  88 f0 40 66 89 44 04 31  d2 88 ca c1 e2 02 88 e8  |[email protected]........|
000000f0  88 f4 40 89 44 08 31 c0  88 d0 c0 e8 02 66 89 04  |[email protected]..|
00000100  66 a1 44 7c 66 31 d2 66  f7 34 88 54 0a 66 31 d2  |f.D|f1.f.4.T.f1.|
00000110  66 f7 74 04 88 54 0b 89  44 0c 3b 44 08 7d 3c 8a  |f.t..T..D.;D.}<.|
00000120  54 0d c0 e2 06 8a 4c 0a  fe c1 08 d1 8a 6c 0c 5a  |T.....L......l.Z|
00000130  8a 74 0b bb 00 70 8e c3  31 db b8 01 02 cd 13 72  |.t...p..1......r|
00000140  2a 8c c3 8e 06 48 7c 60  1e b9 00 01 8e db 31 f6  |*....H|`......1.|
00000150  31 ff fc f3 a5 1f 61 ff  26 42 7c be 7f 7d e8 40  |1.....a.&B|..}.@|
00000160  00 eb 0e be 84 7d e8 38  00 eb 06 be 8e 7d e8 30  |.....}.8.....}.0|
00000170  00 be 93 7d e8 2a 00 eb  fe 47 52 55 42 20 00 47  |...}.*...GRUB .G|
00000180  65 6f 6d 00 48 61 72 64  20 44 69 73 6b 00 52 65  |eom.Hard Disk.Re|
00000190  61 64 00 20 45 72 72 6f  72 00 bb 01 00 b4 0e cd  |ad. Error.......|
000001a0  10 ac 3c 00 75 f4 c3 00  00 00 00 00 00 00 00 00  |..<.u...........|
000001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 01  |................|
000001c0  01 00 83 ff 3f 26 3f 00  00 00 c1 98 09 00 00 00  |....?&?.........|
000001d0  01 27 83 ff ff ff 00 99  09 00 00 2b 40 01 00 00  |.'.........+@...|
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200

6、將我們本身系統裏面的虛擬磁盤文件和內核文件拷貝到我們的U盤系統上面的boot文件夾裏,並查看一下

[root@centos6 ~]# cd /tmp/boot/grub/
[root@centos6 grub]# pwd
/tmp/boot/grub
[root@centos6 grub]# cp /boot/vmlinuz-2.6.32-696.el6.x86_64 /tmp/boot/
[root@centos6 grub]# cp /boot/initramfs-2.6.32-696.el6.x86_64.img /tmp/boot/
[root@centos6 grub]# cd ..
[root@centos6 boot]# pwd
/tmp/boot
[root@centos6 boot]# ls
grub  initramfs-2.6.32-696.el6.x86_64.img  lost+found  vmlinuz-2.6.32-696.el6.x86_64


7、手動編寫啓動的配置文件

[root@centos6 boot]# vim ./grub/grub.conf 
[root@centos6 boot]# cat  ./grub/grub.conf 
default=0
timeout-3
title=minilinux
kernel /vmlinuz-2.6.32-696.el6.x86_64  root=UUID=f47fa85a-8703-4694-bb54-067e90d2fbdf  selinux=0 init=/bin/bash  
initrd /initramfs-2.6.32-696.el6.x86_64.img


8、利用tree命令直接看一下/boot目錄下的所有東西

[root@centos6 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

9、再創建一個文件夾,把/dev/sdb2掛載上去,並創建根目錄下的常見文件夾

[root@centos6 boot]# mkdir /tmp/rootfs
[root@centos6 boot]# mount /dev/sdb2 /tmp/rootfs/
[root@centos6 boot]# cd /tmp/rootfs/
[root@centos6 rootfs]# ls
lost+found
[root@centos6 rootfs]# mkdir -pv {root,bin,sbin,bin,lib,lib64,var,usr,etc,dev,sys,proc,tmp,mnt,media,home}
mkdir: created directory `root'
mkdir: created directory `bin'
mkdir: created directory `sbin'
mkdir: created directory `lib'
mkdir: created directory `lib64'
mkdir: created directory `var'
mkdir: created directory `usr'
mkdir: created directory `etc'
mkdir: created directory `dev'
mkdir: created directory `sys'
mkdir: created directory `proc'
mkdir: created directory `tmp'
mkdir: created directory `mnt'
mkdir: created directory `media'
mkdir: created directory `home'
[root@centos6 rootfs]# tree
.
├── bin
├── dev
├── etc
├── home
├── lib
├── lib64
├── lost+found
├── media
├── mnt
├── proc
├── root
├── sbin
├── sys
├── tmp
├── usr
└── var

16 directories, 0 files


10、寫一個腳本,把我們需要的命令和命令所依賴的庫文件全部拷貝到我們的U盤系統裏面

[root@centos6 bin]# vim  copycmd.sh
[root@centos6 bin]# cat copycmd.sh 
#!/bin/bash



 
ch_root="/tmp/rootfs"
[ ! -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@centos6 bin]# bash copycmd.sh 
Please input a command: bash
Please input a command or quit: ls
Please input a command or quit: ifconfig
Please input a command or quit: cp
Please input a command or quit: mv
Please input a command or quit: rm
Please input a command or quit: cat
Please input a command or quit: tree
Please input a command or quit: reboot
Please input a command or quit: ping
Please input a command or quit: blkid
Please input a command or quit: lsblk
Please input a command or quit: ip
Please input a command or quit: quit


11、查看一下我們U盤系統裏面的相關信息

[root@centos6 bin]# tree /tmp/rootfs/
/tmp/rootfs/
├── bin
│   ├── bash
│   ├── cat
│   ├── cp
│   ├── ls
│   ├── lsblk
│   ├── mv
│   ├── ping
│   └── rm
├── dev
├── etc
├── home
├── lib
├── lib64
│   ├── ld-linux-x86-64.so.2
│   ├── libacl.so.1
│   ├── libattr.so.1
│   ├── libaudit.so.1
│   ├── libblkid.so.1
│   ├── libcap.so.2
│   ├── libc.so.6
│   ├── libdl.so.2
│   ├── libgcc_s.so.1
│   ├── libidn.so.11
│   ├── libnih.so.1
│   ├── libpthread.so.0
│   ├── libresolv.so.2
│   ├── librt.so.1
│   ├── libselinux.so.1
│   ├── libtinfo.so.5
│   └── libuuid.so.1
├── lost+found
├── media
├── mnt
├── proc
├── root
├── sbin
│   ├── blkid
│   ├── ifconfig
│   ├── ip
│   └── reboot
├── sys
├── tmp
├── usr
│   └── bin
│       └── tree
└── var

17 directories, 30 files


12、查看一下我們本身系統的網卡模塊,並且把它拷貝一份到我們的U盤系統上

[root@centos6 bin]# ethtool -i eth0
Cannot get driver information: No such device
[root@centos6 bin]# ethtool -i eth2
driver: e1000
version: 7.3.21-k8-NAPI
firmware-version: 
bus-info: 0000:02:01.0
supports-statistics: yes
supports-test: yes
supports-eeprom-access: yes
supports-register-dump: yes
supports-priv-flags: no
[root@centos6 bin]# locate e1000
/lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000
/lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000e
/lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000/e1000.ko
/lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000e/e1000e.ko
/usr/src/kernels/2.6.32-696.el6.x86_64/drivers/net/e1000
/usr/src/kernels/2.6.32-696.el6.x86_64/drivers/net/e1000e
/usr/src/kernels/2.6.32-696.el6.x86_64/drivers/net/e1000/Makefile
/usr/src/kernels/2.6.32-696.el6.x86_64/drivers/net/e1000e/Makefile
/usr/src/kernels/2.6.32-696.el6.x86_64/include/config/e1000.h
/usr/src/kernels/2.6.32-696.el6.x86_64/include/config/e1000e.h
[root@centos6 bin]# cp /lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000/e1000.ko   /tmp/rootfs/l
lib/        lib64/      lost+found/ 
[root@centos6 bin]# cp /lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000/e1000.ko   /tmp/rootfs/lib
[root@centos6 bin]# bash copycmd.sh 
Please input a command: insmod
Please input a command or quit: quit

13、最後查看一次系統相關信息,檢查一下自己的各項配置,就可以用sync命令把信息同步一樣,把U盤退出去插在別的電腦或在本機啓動時選擇使用

[root@centos6 bin]# tree /tmp/rootfs/
/tmp/rootfs/
├── bin
│   ├── bash
│   ├── cat
│   ├── cp
│   ├── ls
│   ├── lsblk
│   ├── mv
│   ├── ping
│   └── rm
├── dev
├── etc
├── home
├── lib
│   └── e1000.ko
├── lib64
│   ├── ld-linux-x86-64.so.2
│   ├── libacl.so.1
│   ├── libattr.so.1
│   ├── libaudit.so.1
│   ├── libblkid.so.1
│   ├── libcap.so.2
│   ├── libc.so.6
│   ├── libdl.so.2
│   ├── libgcc_s.so.1
│   ├── libidn.so.11
│   ├── libnih.so.1
│   ├── libpthread.so.0
│   ├── libresolv.so.2
│   ├── librt.so.1
│   ├── libselinux.so.1
│   ├── libtinfo.so.5
│   └── libuuid.so.1
├── lost+found
├── media
├── mnt
├── proc
├── root
├── sbin
│   ├── blkid
│   ├── ifconfig
│   ├── insmod
│   ├── ip
│   └── reboot
├── sys
├── tmp
├── usr
│   └── bin
│       └── tree
└── var

17 directories, 32 files
[root@centos6 bin]# sync



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章