LINUX operating system on USB flash disk

Source: Internet
Author: User
The LINUX operating system on the USB flash disk-general Linux technology-Linux programming and kernel information. The following is a detailed description. Summary

This article describes how to create a Linux boot system on a USB storage device.

First, let's take a look at the USB device in LINUX. the Linux USB driver has three different USB master controller options, because there are three different types of USB chips on the motherboard and PCI Card. The "EHCI" driver is designed to support new high-speed USB 2.0 chips. The "OHCI" driver is used to provide support for USB chips on non-PC systems (as well as on PC boards with SiS and ALi chipsets. The "UHCI" driver is used to support USB implementations on most other PC boards, including Intel and. You only need to select the type corresponding to the USB support you want to enable "? HCI driver.

Enable "USB support" and appropriate "? After HCI "USB master controller driver, you only need to perform a few steps to enable and run USB. Enable the "Preliminary USB device filesystem" and SCSI computer standard interface, because our USB storage device simulates access to the SCSI hard disk.

1. About the kernel

The usb-storage module is included in the kernel, but the usb flash disk initialization process is slower than the execution of/sbin/init. As a result, the kernel has been started and the usb flash disk has not yet completed initialization, therefore, the root file system is not loaded, and the execution of the/sbin/init command is definitely not successful. Therefore, you must modify the system kernel initialization Code as follows:


/Init/do_mounts.c
......
* Allow the user to distinguish between failed open
* And bad superblock on root device.
*/
-Printk ("VFS: Cannot open root device" % s "or % s ",
+ Printk ("VFS: Cannot open root device" % s "or % s,
Retrying in 1 second. ", root_device_name, kdevname (ROOT_DEV ));
-Printk ("Please append a correct" root = "boot option ");
-Panic ("VFS: Unable to mount root fs on % s ",
-Kdevname (ROOT_DEV ));
+ Printk ("You may need to append a correct" root = "boot option ");
+ Printk ("or wait for the root device to become ready .");
+
+/* Wait 1 second and try again,
+ * Allowing time for hubs/devices to become ready */
+ Set_current_state (TASK_INTERRUPTIBLE );
+ Schedule_timeout (HZ );
+ Goto retry;
}
Panic ("VFS: Unable to mount root fs on % s", kdevname (ROOT_DEV ));
Out:
......



Recompile the kernel as needed.

You can also load the initrd. imgfile to the memory when starting the kernel, wait 3 seconds for the USB flash drive to complete initialization, and then execute the/sbin/init command.

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 = 1 M 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



2. Create the startup configuration and directory structure of the USB flash drive


(/Dev,/proc,/bin,/etc,/lib,/usr,/tmp,/var)



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:


# Fdisk-l/dev/sda
Disks/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. It is best to use the corresponding USB flash drive tool to create a disk.

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

Combine the compiled kernel with initrd. copy the imgfile to the boot directory, compile the module to the lib/modules directory, copy the/boot/grub file to the boot directory, and 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/bzImage ro root =/dev/sda2
Initrd/boot/initrd. img // if there is no initrd. img, do not use this sentence.



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.

For commonly used tools, it is recommended to use tinylogin http://tinylogin.busybox.net or busybox http://www.busybox.net, they can recognize that you save no space.

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 proc usr sbin 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. If your kernel uses Devfs, you only need to create this directory.

Create the proc usr sbin directory. Can you use ln? S bin sbin establishes a sbin connection.

6. Create the lib directory

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.

Generally, the following files should be generated:


/Lib:
? Rwxr? Xr? X 1 root 45415 Nov 1 :39 ld? 2.0.7.so
Lrwxrwxrwx 1 root 11 Nov 1 :39 ld? Linux. so.2?>
Ld? 2.0.7.so
? Rwxr? Xr? X 1 root 731548 Nov 1 :39 libc? 2.0.7.so
Lrwxrwxrwx 1 root 13 Nov 1 :39 libc. so.6?>
Libc? 2.0.7.so
Lrwxrwxrwx 1 root 17 Nov 1 :39 libcom_err.so.2?>
Libcom_err.so.2.0
? Rwxr? Xr? X 1 root 6209 Nov 1 :39 libcom_err.so.2.0
? Rwxr? Xr? X 1 root 153881 Nov 1 :39 libcrypt? 2.0.7.so
Lrwxrwxrwx 1 root 17 Nov 1 :39 libcrypt. so.1?>
Libcrypt? 2.0.7.so
? Rwxr? Xr? X 1 root 12962 Nov 1 :39 libdl? 2.0.7.so
Lrwxrwxrwx 1 root 14 Nov 1 :39 libdl. so.2?>
Libdl? 2.0.7.so
Lrwxrwxrwx 1 root 14 Nov 1 :39 libpam. so.0?>
Libpam. so.0.64
? Rwxr? Xr? X 1 root 26906 Nov 1 :39 libpam. so.0.64
Lrwxrwxrwx 1 root 19 Nov 1 :39 libpam_misc.so.0?>
Libpam_misc.so.0.64
? Rwxr? Xr? X 1 root 7086 Nov 1 libpam_misc.so.0.64
? R? Xr? Xr? X 1 root 35615 Nov 1 :39 libproc. so.1.2.6
Lrwxrwxrwx 1 root 19 Nov 1 :39 libtermcap. so.2?>
Libtermcap. so.2.0.8
? Rwxr? Xr? X 1 root 12041 Nov 1 :39 libtermcap. so.2.0.8
? Rwxr? Xr? X 1 root 12874 Nov 1 :39 libutil? 2.0.7.so
Lrwxrwxrwx 1 root 16 Nov 1 :39 libutil. so.1?>
Libutil? 2.0.7.so
Drwx ?? X ?? X 3 root 1024 Nov 1 modules
Drwx ?? X ?? X 2 root 1024 Nov 1 security

/Lib/modules:
Drwx ?? X ?? X 4 root 1024 Nov 1 :39 2.0.35

/Lib/modules/2.0.35:
Drwx ?? X ?? X 2 root 1024 Nov 1 block
Drwx ?? X ?? X 2 root 1024 Nov 1 cdrom

/Lib/modules/2.0.35/block:
Drwx ?????? 1 root 7156 Nov 1 :39 loop. o

/Lib/modules/2.0.35/cdrom:
Drwx ?????? 1 root 24108 Nov 1 :39 cdu31a. o

/Lib/security:
? Rwx ?? X ?? X 1 root 8771 Nov 1 :39 pam_permit.so



3. Create a configuration file

1. Edit etc/init. d/rcS? The file name is determined by etc/inittab.

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
Hostname USB boot



2. Edit the etc/fstab file

The content is as follows:


/Dev/sda2/ext2 defaults 1 1
None/proc defaults 0 0



3. Create the etc/inittab file. Do not use the init file generated by busybox.


Id: 2: initdefault:
Si: sysinit:/etc/rcS
1: 2345: respawn:/sbin/getty 9600 tty1
2: 23: respawn:/sbin/getty 9600 tty2



4. Create the nsswitch. conf file.


Passwd: files
Shadow: files
Group: files
Hosts: files
Services: files
Networks: files
Protocols: files
Rpc: files
Ethers: files
Netmasks: files
Bootparams: files
Automount: files
Aliases: files
Netgroup: files
Publickey: files


5. Create the/etc/pam. conf file.


OTHER auth optional/lib/security/pam_permit.so
OTHER account optional/lib/security/pam_permit.so
OTHER password optional/lib/security/pam_permit.so
OTHER session optional/lib/security/pam_permit.so

6. Create a passwd, group, and shadow file.

The simplest way is to copy the original system file and then edit it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.