Linux U盘系统(学习系统启动过程)

本文描述了Red Hat Enterprise Linux 5环境下制作U盘系统,旨在了解linux操作系统的系统启动过程。
系统启动过程
1、加电自检(POST代码)
2、boot loader
GRUB -- /boot/grub/grub.conf
kernel
initrd.img
3、INIT
/sbin/init ----- /etc/inittab
/etc/rc.d/rc.sysinit
/etc/rc.d/rc RUNLEVEL
/etc/rc$runlevel.d/S* start
/etc/rc$runlevel.d/K* stop
/etc/X11/prefdm

4、登录
文本 login
图形 gdm/kdm
自制一个U盘系统
准备工作:在真实机中把yum配置好
[root@localhost /]# ls /mnt/Server/ 这是软件包所处的目录
[root@localhost /]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
rhel-debuginfo.repo
[root@localhost yum.repos.d]# cp rhel-debuginfo.repo pg.repo
[root@localhost yum.repos.d]# vim pg.repo
[root@localhost yum.repos.d]# cat pg.repo
[rhel-server]
name=Red Hat Enterprise Linux
baseurl=file:///mnt/Server
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release






1、准备U盘,并分区,格式化成ext3(可选)文件系统[root@localhost pg]# fdisk -l
[root@localhost pg]# fdisk -l
Disk /dev/sda: 4003 MB, 4003463168 bytes
84 heads, 20 sectors/track, 4654 cylinders
Units = cylinders of 1680 * 512 = 860160 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 5 4655 3905600 c W95 FAT32 (LBA)

[root@localhost pg]# fdisk /dev/sda

The number of cylinders for this disk is set to 4654.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): d
Selected partition 1

Command (m for help): p

Disk /dev/sda: 4003 MB, 4003463168 bytes
84 heads, 20 sectors/track, 4654 cylinders
Units = cylinders of 1680 * 512 = 860160 bytes

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): 1
First cylinder (1-4654, default 1):
Using default value 1
Last cylind[root@localhost pg]# mount -t ext3 /dev/sda1 /neter or +size or +sizeM or +sizeK (1-4654, default 4654):
Using default value 4654

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
[root@localhost pg]# partprobe /dev/sda
[root@localhost pg]# ls /dev/sda1
/dev/sda1
--------------
[root@localhost pg]# mkfs.ext3 /dev/sda1


2、挂载U盘,并把相关文件拷到U盘中
[root@localhost pg]# mount -t ext3 /dev/sda1 /net

把/目录树结构在u盘创建好
]# yum --installroot=/net -y install filesystem

/boot
这个目录应该有vmlinuz-x.x.x initrd-x.x.x.img
/boot/grub
这个目录中应该有grub的相关文件
grub还要安装到u盘的首扇区中(grub-install)
/bin /sbin /lib /usr/lib
这些个目录下应该有一些命令和库文件
bash ls cp mv
mount
ifconfig
fdisk
mkfs.ext3


基本上你需要的程序比较多,大概有以下一些常用的:
coreutils setup grub vim-enhanced
vim-common gpm perl iptables
openssh openssh-clients
module-init-tools iputils grep awk sed
procps tar bash gcc make rp-pppoe
passwd libuser e2fsprogs util-linux
net-tools SysVinit kernel


-----------------------
a.关于vmlinuz文件:
]# cp /boot/vmlinuz-2.6.18-194.el5 /net/boot/
b.关于initrd.img文件: 为了包含u盘的驱动,需要手动生成initrd.img(包含usb-storage)
]# modinfo -F filename usb-storage

]# mkinitrd --with=usb-storage /net/boot/initrd-usb-2.6.18-194.img `uname -r`
c. 使用此命令grub-install将grub安装到u盘的首扇区中
grub-install 需要安装grub软件包生成
]# yum --installroot=/net -y install grub
]# grub-install /dev/sda 最后执行这一步,还有一些必要工作没做

d. ]# cat /net/boot/grub/grub.conf
default=0
timeout=5
title RHEL5U5 usb linux for pg
root (hd0,0)
kernel /boot/vmlinuz-2.6.18-194.el5 ro root=/dev/sda1
initrd /boot/initrd-usb-2.6.18-194.img

e. 为了生成以下这些常用的命令,装包
bash ls cp mv
mount
ifconfig
fdisk
mkfs.ext3

yum bash ls cp mv
mount
ifconfig
fdisk
mkfs.ext3
yum --installroot=/net -y install coreutils setup grub vim-enhanced vim-common gpm perl iptables openssh openssh-clients module-init-tools iputils grep awk sed procps tar bash gcc make rp-pppoe passwd libuser e2fsprogs util-linux net-tools SysVinit kernel pam

f.在U盘系统启动的时候,需要读fstab文件,挂载文件系统
]# cp /etc/fstab /net/etc

]# vim /net/etc/fstab
]# cat /net/etc/fstab
/dev/sda1 / ext3 defaults 0 0

开始安装GRUB程序
]# chroot /net/
bash-3.2#
]# chroot /net/
bash-3.2# grub-install /dev/sda
Probing devices to guess BIOS drives. This may take a long time.
No suitable drive was found in the generated device map. 这个错误意味着需要去写devices.map文件
bash-3.2# ls /dev/sda
ls: /dev/sda: 没有那个文件或目录
bash-3.2# ls /dev/sda1
ls: /dev/sda1: 没有那个文件或目录
bash-3.2# mknod /dev/sda b 8 0
bash-3.2# mknod /dev/sda1 b 8 1
bash-3.2# cat /etc/mtab
/dev/sda1 / ext3 rw 0 0
bash-3.2# cat /boot/grub/device.map
(fd0) /dev/fd0
(hd0) /dev/sda

bash-3.2# grub-install /dev/sda
Could not find device for /dev/mapper/*
Could not find device for /dev/mapper/*
Could not find device for /dev/mapper/*
Installation finished. No error reported.
This is the contents of the device map /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


]# chroot /net/
bash-3.2# passwd root 修改密码不成功,将导致 u盘系统启动后,用root登录不成功。
Changing password for user root.
passwd: Authentication token manipulation error
解决方法:
vim /net/etc/passwd
root:*:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
这样再把root密码设置为你想要的!


检查 以下过程是否正确:
/boot/grub/grub.conf
/etc/inittab ---默认运行级别应该是3
/etc/fstab
/etc/passwd
/etc/shadow
pam程序包做验证的
ls /dev/sda /dev/sda1
]# chroot /net/
bash-3.2# passwd root 这一步必须能成功
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章