Making USB boot disk for Linux

Time:2022-6-10

USB flash memory devices (USB flash drives) are generally only the size of key chains, and the storage capacity is mostly 32MB and 64MB. Due to the advantages of being light and exquisite, easy to use, easy to carry and so on, especially compared with the floppy disk, which has the characteristics of large capacity, safety and reliability, fast reading speed and so on, more and more people use this device to replace the floppy disk and floppy disk to exchange files between PCs. At present, the new motherboard BIOS supports the startup mode of USB floppy disk and hard disk, so for Windows 98systemMaintenance andinstallWork is becoming more and more convenient. However, the manufacturer only provides tools for making Windows 98 startup disks, not Linux startup disks. Create a Linux boot disk on the USB drive. ForsystemMaintenance personnel have more practical significance. You can use Linux to build a compact kernel and buildnetworkEnvironment, rapid judgment and eliminationnetworkFailure and transfer of documents. Taking red hat 8.0 as an example, this paper uses the dual boot USB disk of LANCO to build the Linux boot disk of the USB disk.

Download related software: E3, BVI, liuux kernel.

Compile kernel

First, the motherboard of the computer must support the startup mode of the USB hard disk, and the USB disk used is a startup USB disk.

I take linux-2.4.20 as an example. When compiling, be sure not to compile unnecessary modules, such as sound card driver and other driver modules, so as to make the compiled kernel as small as possible. To support USB drive startup, SCSI devices, usbcore, USB storage, loopback device support, RAM disk support, and initrd must be compiled into the kernel.

Many people will ask why initrd IMG file? This is because the initialization process of the USB disk is slower than that of /sbin/init. As a result, the kernel has been started and the initialization of the USB disk has not been completed. Therefore, the root filesystemIf it is not loaded, the /sbin/init command cannot be executed successfully. By establishing initrd IMG file, which loads initrd The IMG file is stored in memory. Wait for 3 seconds for the USB disk to complete the initialization, and then execute the /sbin/init command. specificoperationThe steps are as follows.

1. create initrd IMG file

  # mkdir -p /mnt/initrd
  # cd /tmp
  # mkinitrd /tmp/initrd.gz 2.4.20-usb

  2. Unzip initrd IMG file, modifying 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:

  echo ‘wait 3 seconds…..’
  /bin/sleep 3

  3. Regenerate initrd IMG file

Since the initrd file created by default is relatively large (4MB), it must be reduced in order to speed up the startup of the USB drive. specificoperationAs 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 starts normally

Test whether the compiled kernel starts normally and whether the relevant information of the USB flash disk can be seen during the startup process.

Create USB boot disk

  1. Divide the USB drive into two partitions

The size of Linux partition capacity is completely determined byinstallWhich?systemMaintenance tools, the results are 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 use a USB drive to boot Windows 98, the sector size must beset upFor 63 sectors, you can modify the heads, sectors, and cylinders parameters through the X command extension of fdisk.

  2. Creating and generating ext2 partitions

  # mke2fs -m 0 /dev/sda2
  # mkdir -p /mnt/sda2
  # mount /dev/sda2 /mnt/sda2
  # cd /mnt/sda2

  3. Create boot directory

Connect the compiled kernel with initrd-2.4.20-usb Copy the IMG file to the boot directory, the compiled modules to the lib/modules directory, and the /boot/grub file to the boot directory. Edit the boot/grub/menu Lst file, 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

  installGrub, specificoperationAs follows:

  grub> root (hd1,1)
  grub> setup (hd1)

  4. Create bin directory

Copy according to the needs of self worksystemMaintenance tools, such as utilities such as insmod, fsck, and mkdosfs. Be sure to use the LDD command to check those shared library files. These files need to be copied to the Lib directory according to the original path. Due to disk space limitations, busybox commands are used to replace some common linux commands. The main reason is that busybox files are very small and statically linked. Many common linux commands (such as cat, init, ifconig, route) use ln -s busybox to establish symbolic connections of these files. Busybox can be recompiled according to personal needs, including VI and other commands, or the compact E3 can be used to replace VI.

In addition, if Bash is used, you must edit and trim /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 dev directory

Use the CP -a command to copy common device files, including console, tty1, tty2, tty3sda, sda1, sda2, HDA, HDB, and hda1.

  6. Edit etc/init d/rcS

The contents are 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 contents are as follows:

  /dev/sda2 / ext2 defaults 1 1
  none /proc proc defaults 0 0

Application examples

Take an example of transferring files. On a working Linux machine a (ip=192.168.100.5):

  $ tar cf – win98 | nc -l -p 5555

Note: you can use the USB flash drive to start the machine on machine B, and telnet logs in to machine a to execute the above commands.

On machine B, use the USB drive to start the execution:

  # 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 use the format /s command to create a USB disk Windows 98 startup disk, you can use a flexible method. SpecificallyoperationAs follows:

  # mkdosfs -F 16 /dev/sda1
  # xxd -c 16 /dev/sda1 | less

At this time, record the 32 ~ 35 bytes of boot record (in this example, 60910100, which records the total number of sectors in the fat partition), then use the tool for making boot disk provided by Longco to make the Windows 98 boot disk, and then use BVI under Linux or the tool that can edit binary files to modify the 32 ~ 35 bytes of boot record to the original content. In this way, we can realize the real Three boot mode (Windows dual boot + Linux boot). “