Article Title: Tips: Create a Linux operating system flash drive. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Creating a Linux boot disk on a USB flash disk is of more practical significance for system maintenance personnel. You can use Linux to build a small kernel and a network environment, quickly identifies and resolves network faults and transfers files.
Download related software: e3, bvi, and Liuux kernel.
I. Compile the kernel
First, the computer's motherboard must support the USB hard drive boot mode, and the used USB flash drive is the boot type.
Taking Linux-2.4.20 as an example. During compilation, be sure not to compile unnecessary modules, such as sound card drivers, so that the compiled kernel is as small as possible. To support usb Flash boot, you must compile SCSI devices, usb core, usb-storage, Loopback device support, RAM disk support, and initrd into the kernel.
Many people may ask why the usb-storage module is already included in the kernel to create an initrd. imgfile? This is because the initialization process of the USB flash drive is slower than the execution of/sbin/init. As a result, the kernel has been started and the initialization of the USB flash drive has not been completed. Therefore, the root file system has not been loaded, at this time, the execution of the/sbin/init command is definitely not successful. Create an initrd. imgfile to load the initrd. imgfile to the memory when the kernel is started. wait 3 seconds for the USB flash drive to complete initialization and then execute the/sbin/init command. The procedure is as follows.
1. Create an initrd. imgfile
# mkdir -p /mnt/initrd # cd /tmp # mkinitrd /tmp/initrd.gz 2.4.20-usb
|
2. decompress the initrd. imgfile and modify the startup script linuxrc.
# gunzip initrd.gz# mount -o loop /tmp/initrd /mnt/initrd# cp /sbin/busybox /mnt/initrd/bin# cd /mnt/initrd/bin# ln -s busybox sleep# vi /mnt/initrd/linuxrc
|
Add the following content:
echo 'wait 3 seconds.....' /bin/sleep 3
|
3. regenerate the initrd. imgfile
Because the default initrd file is large (4 MB), you must reduce it to speed up the startup of the USB flash drive. The procedure is as follows:
# mkdir -p /mnt/initrdusb# cd /tmp# dd if =/dev/zero of= /tmp/initrdusb bs=1M count=1# mke2fs -m 0 initrdusb # mount -o loop /tmp/initrdusb /mnt/initrdusb # cp -a /mnt/initrd/* /mnt/initrdusb # umount /mnt/initrd # umount /mnt/initrdusb # cd /tmp # gzip -9 initrdusb # cp initrdusb.gz /boot/initrd-2.4.20-usb.img
|
4. test whether the compiled kernel is properly started.
Test whether the compiled kernel is started normally and whether the information about the USB flash drive is visible during startup.
2. Create a Flash Boot Disk
1. Divide the USB flash drive into two partitions.
The size of the partition size for Linux depends on which system maintenance tools are installed. The result is as follows:
# modprobe usb-storage # fdisk -l /dev/sda Disk /dev/sda: 16 heads, 63 sectors, 126 cylinders Units = cylinders of 1008 * 512 bytes Device Boot Start End Blocks Id System /dev/sda1 1 102 51376+ 6 FAT16 /dev/sda2 103 126 12096 83 Linux
|
Note: If you need To Boot Windows 98 with a USB flash drive, the slice size must be set to 63. You can use the fdisk x command to expand and modify the parameters of heads, sectors, and cylinders.
2. Create and generate ext2 partitions
# mke2fs -m 0 /dev/sda2 # mkdir -p /mnt/sda2 # mount /dev/sda2 /mnt/sda2 # cd /mnt/sda2
|
3. Create a boot directory
Copy the compiled kernel and initrd-2.4.20-usb.img files to the boot directory, the compiled modules to the lib/modules directory, and copy the/boot/grub files to the boot directory, edit boot/grub/menu. the content of the lst file is as follows:
timeout 10 color 0x17 0x70 default 0 title Windows 98 rootnoverify (hd0,0) makeactive chainloader +1 title GNU/Linux Redhat 8.0 (2.4.20-usb) root (hd0,1) kernel /boot/vmlinuz-2.4.20-usb ro root=/dev/sda2 initrd /boot/initrd-2.4.20-usb.img
|
To install grub, perform the following operations:
Grub> root (hd1, 1)
Grub> setup (hd1)
4. Create a bin directory
Copy System maintenance tools such as insmod, fsck, and mkdosfs as needed. Be sure to use the ldd command to check the shared library files. These files need to be copied to the lib directory according to the original path. Due to disk space restrictions, busybox commands are used to replace some common Linux commands. The main reason is that the busybox file is very small and is statically linked, including many common Linux commands (such as cat, init, ifconig, and route), use ln-s busybox to establish symbolic connections to these files. You can re-compile busybox as needed, including vi and other commands, or use small e3 to replace vi.
In addition, if bash is used, you must edit and cut/etc/termcap and the following files:
/bin/bash /etc/termcap /usr/share/terminfo/l/linux /usr/share/terminfo/k/klone+acs /usr/share/terminfo/k/klone+color /usr/share/terminfo/k/klone+sgr
|
5. Create the dev directory
Run the cp-a command to copy common device files, including devices such as console, tty1, tty2, tty3sda, sda1, sda2, hda, hdb, and hda1.
6. Edit etc/init. d/rcS
The content is as follows:
#!/bin/sh PATH=/sbin:/bin export PATH mount -n -t proc none /proc umount /initrd mount -n -o remount,rw / mount -n -o remount,rw -t proc none /proc ifconfig lo 127.0.0.1
|
7. Edit the etc/fstab file
The content is as follows:
/dev/sda2 / ext2 defaults 1 1 none /proc proc defaults 0 0
|
3. Application Example
Here is an example of transferring files. Run:
$ Tar cf-win98 | nc-l-p 5555
Note: You can use the USB flash drive to start the machine on machine B and telnet to machine A to execute the above commands.
Run the following command on machine B with a USB flash drive:
# mount -t vfat -o codepage=938,iocharset=cp936 /dev/hda5 /mnt/2dos # cd /mnt/2dos # modprobe eepro00 # ifconfig eth0 192.168.1.10 # route add default gw 192.168.1.1 # ping 192.168.10.5 # nc 192.168.10.5 5555 | tar xf -
|
If you cannot create a Windows 98 boot disk by using the format/s command, you can use the following method:
# mkdosfs -F 16 /dev/sda1 # xxd -c 16 /dev/sda1 | less
|
Record the Boot Record 32 ~ 35 bytes (in this example, 60 91 01 00, which records the total number of sectors in the FAT partition ), use the Boot disk tool provided by longke to create a Windows 98 Boot disk. Then, use the bvi tool in Linux or the tool that can edit the binary file to modify the 32 ~ Boot Record ~ 35 bytes is the original content. In this way, we can implement a three-way startup (Windows Dual Boot + Linux boot ).