Use shell scripts to automate your own micro-Linux! (1)

Source: Internet
Author: User

Two days ago, I wrote about how to use the host RedHat system to streamline, call, and modify the system to create a tiny Linux of my own!

See: http://blog.csdn.net/wei2856059/article/details/6654801

 

However, many people are lazy ~~, So I wrote another script and implemented it step by step. Use a script to install a micro-Linux based on the user's purpose.

Then let's look at how to implement it:

 

Bytes ----------------------------------------------------------------------------------------------------------------------

Implementation Process Analysis:

1. Partition: Format and partition the selected disk.

2. Attach the disks in user-defined partitions to a directory of our host. It is convenient to edit micro Linux.

3. Write configuration information for micro-Linux

4. Put the bash and other commands we need in micro Linux

5. Add the configuration files to the kernel and must be loaded at startup.

6. Install the boot program on your hard disk.

 

Okay ~ The process has been analyzed. Now let's start writing !!!

Bytes -----------------------------------------------------------------------------------------------------------------------

Step 1: implement automatic partitioning !!

Let's look at the problem separately, so it becomes like this:

Write a script to delete all partition information in a hard disk and divide it into three partitions:
1. prompt the user to specify the disk to operate. If there is a partition in the hard disk, display the partition information and determine whether the partition is still mounted;
If yes, the partition and the corresponding mount point are displayed. Otherwise, the partition is not mounted;
2. the user is prompted that the next operation will destroy all data on the hard disk and ask the user if they want to continue; for example: Continue (y/n )?
If the user chooses to continue, all the partition information on the hard disk will be erased (if the partition is still in the Mount status, unmount it first); otherwise, exit;
3. Divide the hard disk into three primary partitions:
The first primary partition, 50 m, ext3 File System
The second primary partition, 512 M, ext3 File System
The third primary partition, 256 m, swap File System

OK to start implementation:

#! /Bin/bash #: Title: all_mini_linux.sh #: Synopsis: #: datetime: 00:36:35 #: Version: 0.1 #: Author: Weiyan #: Options: # Read-P "the target disk:" DSK # prompt to select the hard disk to be partitioned if fdisk-L $ DSK | grep "^/dev" &>/dev/NULL; then # determine the partition status of the hard disk. Fdisk-L $ DSK | grep "^/dev" parts = 'fdisk-L $ DSK | grep "^/dev" | awk '{print $1} ''for I in $ parts; do # use a loop to indicate the status of each partition on the hard disk and whether the partition is mounted. If Mount | grep "$ I" &>/dev/NULL; then mount | grep "$ I" else echo "$ I is not mounted. "fi donefispart () {# create a function named Spart. This function prompts you to prepare partitions. When the partition is allowed, start partitioning! Read-P "Warning !!!!! Next you do it will wipe your disk !!!! Continue (y/n )? "Choice case $ choice in Y | Y) for I in $ parts; do if Mount | grep" $ I "&>/dev/NULL; then Fuser-km $ I umount $ I fi DONE dd If =/dev/Zero of = $ DSK BS = 512 COUNT = 1 &>/dev/null # Replace the hard disk selected by the user. Make it an empty disk. Echo 'n' P 1 + 50mnp2 + 512mnp3 + 256mt382w' | fdisk $ DSK &>/dev/null # pass the partition information to fdisk to start partitioning. Partprobe $ DSK sleep 1 # After partitioning, run the sleep command to "Sleep" the system for 1 second. In this way, the partition information can be truly synchronized to the hard disk without formatting errors. Mke2fs-J $ {DSK} 1 &>/dev/null # format each partition and format the virtual partition separately. Mke2fs-J $ {DSK} 2 &>/dev/null mkswap $ {DSK} 3 &>/dev/null return 0; n | N) return 1 ;;*) return 2; esac} Spart # executes this Spart function. [$? -EQ 0] & Echo "the disk wipe success !!!!!!! "| Echo" failure "# judge the return value of the function. If it is 0, a prompt is displayed, indicating that the partition is successful.

In this way, we have a complete partition and formatted the hard disk. What about next?

Mount the hard disk!

Bytes ---------------------------------------------------------------------------------------------------------------------

Step 2: Mount!

So I sorted out step 2 and found that it actually implemented the following functions?

Write a script to mount the partitions created in the preceding steps to a directory:
1. Write a function that accepts two parameters to complete the following functions:
Parameter 1 is the device corresponding to the/boot partition, which is mounted to/mnt/boot after being passed to the function;
Parameter 2 is the device corresponding to the/partition and is mounted to/mnt/sysroot after being passed to the function;
Note: If the above Mount Points/mnt/boot and/mnt/sysroot do not exist in advance, you need to create them first. If they exist in advance and have been mounted and used by a device, first, you need to uninstall the original mounted device;
2. Pass Partition 1 and partition 2 implemented in the first script to some functions for execution;

Then, the goal is ready. Let's implement it!

Function guazai {# create a function named guazai [-D/mnt/boot] | mkdir-P/mnt/boot # first, judge whether the directory to be mounted is located, if not, it is created. Umount/mnt/boot &>/dev/null # When a directory exists, unmount it first, whether or not it has been mounted. (Let me be lazy -. -) Mount $1/mnt/boot # mount the directory with the specified parameter 1 to the mount point. [-D/mnt/sysroot] | mkdir-P/mnt/boot # Same as above. Determine the second one. Umount/mnt/sysroot &>/dev/null Mount $2/mnt/sysroot} guazai $ {DSK} 1 $ {DSK} 2 # execute this function, and pass it two partition parameters to be mounted.

OK. Mounting is complete. What about next?

Bytes --------------------------------------------------------------------------------------------------------------------------

Step 3: Write configuration information!

Analyze the configuration information we want to write:

First, we need to create various folders on our second partition, such as/bin/sbin/usr/root/home/tmp/etc.

Then we will create the/etc/inittab file.

Then create the file/etc/fstab that reads the mounting information of the partition information.

What then? Create the/etc/rc. d/rc. sysinit file ~

OK ~ Well analyzed! Start writing!

# Here we use the function to pass the parameter. The parameter is only $1, while $1 is the directory mounted to partition 2/mnt/sysrootfunction rootfs {# Use the function to implement various functions, it is convenient for other users or other programs to call and write more functions for future consideration! If [-d $1]; then if Mount | grep "$1" &>/dev/NULL; then # determine whether our partition 2 is correctly mounted with CD $1 mkdir {boot, Proc, sys, Dev, home, root, ETC/{RC. d, sysconfig, init. d}, bin, sbin, Lib, usr/{bin, sbin, Lib, include}, var/{log, run}, TMP, MNT, opt, media}-PV &>/dev/null # create the required folders in this directory. (Although the sparrow is small, it is easy to understand.) chmod 1777 tmp/# create inittabcat> $1/etc/inittab <EOF # create inittab and write the following information ID: 3: initdefault: Si :: sysinit:/etc/rc. d/RC. sysiniteof # create RC. sysinit # create RC. write the following information to sysinit: CAT >1 1/etc/rc. d/RC. sysinit <EOF #! /Bin/bashecho-e "\ t \ twelcome to \ 033 [031; 1mlittle \ 033 [0 m Linux... "Mount-n-o remount, RW/Mount-n-A/bin/basheof chmod + x etc/inittab # grant them the execution permissions respectively (you don't always want to see them there, but they cannot be used -. -?) Chmod + x etc/rc. d/rc. sysinit # create fstab to write the information about the partition to be mounted at that time. Note that it must be mounted at "time. Cat> $1/etc/fstab <EOF/dev/sda2/ext3 defaults 0 0/dev/sda1/boot ext3 defaults 0 0 0 sysfs/sys sysfs defaults 0 0 0 proc/ proc proc defaults 0 0eof else read-P "error, the $1 not mount! Contiune (Y | N )? "Mot # If we find that partition 2 is not mounted yet! Stop! Case $ mot in # However, this should not be suspended; otherwise, we will not do it in the previous step !? Y | Y) rootfs/mnt/sysroot; *) echo "exit ...... "Return 5; esac fi} rootfs/mnt/sysroot # Finally, run this function and pass it to/mnt/sysroot ~~ OK ~

Okay! We have completed the first three steps! Summary:

 

We divide the selected hard disk into three partitions and format them.

Then we mount the required partitions to the directory we set!

Then we write configuration information into the partition.

 

Next, let's take a look at the second article ~ Complete the following three steps !! OK, come on ~ Change the page. Let's continue !!

Related Article

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.