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

Source: Internet
Author: User
Tags gz file

We have completed the first three steps. We will analyze the implementation of the entire shell script again:

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.

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

Okay. That's just to help you sort out your ideas. Let's look back at the previous episode ~ Next let's continue! Run the shell script to the end!

Step 4: port a variety of commands we need:

Script analysis:

To port commands, we need to first copy the commands on our host system.
However, copying commands is not enough. We need to copy the library file corresponding to the commands.
Different commands have different styles and different/identical library files.
We need to identify which database files are copied, which files are copied, and whether there are duplicates ~
Finally, we need to know, instead of just copying a command, we need to cyclically let the user choose what command is needed. Till the end of the replication process ~

OK. Let's start with the plan!

Function copylib {# We still use functions to facilitate future calls. # Copy command if which $1 &>/dev/NULL; then # determine whether the user-entered command is really a command. You cannot enter 1 or copy 1 ~ =. = AA = 'which $1 | tail-1 | awk '{print $1} ''cp "$ AA" $2 "$ AA" # If the judgment is successful, copy ~ Else echo "$1 is not exist" fi # copy lib # Start to copy the Lib library file corresponding to the command in 'ldd "$ AA" | grep "/lib/" | SED '1, $ s @ ^ [[: Space:] /lib @ AAA =>/lib @ G' | awk-F "=>" '{print $2}' | cut-d ""-F1 '; do libpath = 'echo $ lib | SED's @\(. * \)/[^ [: Space:] \ {1, \} @ \ 1 @ g'' # We use a for loop, to determine the Lib contained in the command we entered, and align them, list [-d $ {2} $ libpath] | mkdir $ {2} $ libpath # determines whether the directory we are about to copy exists. If it does not exist, create [! -E $2/$ lib] & CP-F $ Lib $ {2} $ libpath & Echo "$ lib done" # Start copying ~ And prompts the copy effect ~~ Done} # Here, this function is over. We are going to call it below. First, you must know that there are two commands that must be copied. One is Bash, the other is INIT, and both are very important commands. Echo "============================================== ============================ "echo-e" Please input command with \ 033 [31mbash \ 033 [0 m and \ 033 [31minit \ 033 [0 m !!!!! "Read-P" command you want? (Quit for 'q'): "cmd1 # is because it is too important. Therefore, we use the previous statement red to warn users that the two must be copied first. # Then we create a loop to help users repeatedly add various commands. Until [$ cmd1 = 'q']; do # Of Course, you cannot always enter it, therefore, we only need to press 'q' copylib $ cmd1/mnt/sysroot # To execute the function and pass the command we entered. Echo-e "Please input command with \ 033 [31mbash \ 033 [0 m and \ 033 [31minit \ 033 [0 m !!!!! "Read-P" which command you want use? (Quit for 'q'): "interval 1done

OK ~ In this way, we have various commands in Linux ~ Next, we will add the kernel to it!
Let's run Linux !!!!!

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

Step 5: Get a kernel! Let our Linux have the heart!

When manually creating our own Linux, we already know that our kernel uses the RedHat Kernel File directly.
Then modify initrd, which can Virtualize a small Linux file in the memory to facilitate loading the root directory system.
After that, we just need to port it in.

Next, let's start ~!

Function ydfq {# is still a function -. -Tmpdir = 'mktemp-D/tmp/little. xx' # create a Temporary Folder. We will unzip initrd. *. IMG later ~ CD $ tmpdirzcat/boot/initrd-'uname-R '. IMG | cpio-ID &>/dev/null # extract and expand the initrd file in the Temporary Folder to prepare modifications. Sed-I's @ ^ Echo scanning and logging ing dmraid supported devices @ # & @ g 'initsed-I 'S @ ^ Echo scanning logical volumes @ # & @ G' initsed-I's @ ^ LVM vgscan -- ignorelockingfailure # & @ G' initsed-I's @ ^ echo activating logical volumes # & @ G' initsed-I's @ ^ LVM vgchange -Ay -- ignorelockingfailure vol0 @ # & @ G' initsed-I's @ ^ resume label = SWAP-sda3 @ # & @ G' init # comment out useless lines in the init file ~ Sed-I "s @ \ (mkrootdev. * \)/. * @ \ 1 sda2 @" init # It is useful to change the init file. For example, we need to determine which disk to execute at that time. Find. | cpio-h newc-o -- Quiet | gzip-9>/mnt/boot/initrd.gz # recreate the file and compress it into the initrd.gz file ~ CP/boot/vmlinuz-'uname-R'/mnt/boot/vmlinuz # copy the prepared files to our partition !} Ydfq/mnt/sysroot # Start of function execution -. -Okay, forgive me for always writing it back.

OK ~ We have the heart of Linux ~ The next step is the last step !!! Give a boot to the hard disk and let him know how to run it !!!!

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

Step 6: boot file!

Now that we need guidance, we need to mention our great grub ~ It can directly install an MBR on the hard disk ~ Let it have boot loader ~
In this way, the hard disk will know what to do after it is started ~~!
Of course, we are the master of our system, so we need to add your own stuff in it !!

Let's get started!

# Here we need to pass four values to our function ##$ 1 = which hard disk we want to install bootloader ##$ 2 = our primary partition, is the partition where the Kernel File is located ##$ 3 = kernel name ##$ 4 = initrd name ~ # Function grub {# all kinds of functions -. -If ['basename $ 2' = boot]; then # determines whether the user-entered primary partition is the primary partition we need, but this is certainly true, because-is specified manually below -. -Grub-install -- root-directory =/mnt $1 &>/dev/null # Install grub and go to our partition. Then we have bootloader! Cat> $2/GRUB/grub. conf <EOF # define our own Boot Method ~~ Default = 0 timeout = 10 title Little linux root (hd0, 0) kernel/$ {3} Ro root =/dev/sda2 quiet initrd/$ {4} eofelse echo "it's not boot! Exit & please set it again! "Fi} grub $ DSK/mnt/boot vmlinuz initrd.gz # Well, the function starts to execute -. -Echo "Congratulation! Your Mini Linux is install OK! "# Display celebration information! Tell the user that we have installed it!

OK ~~!!!! Everything is done ~

Next, let's test ~

According to this writing, we can say that the one-click installation of Linux is basically implemented, and you have made it yourself !!!!!

~

Hmm ~

Have you understood it? Yes -. -Do it yourself ~~

 

Of course, if you find any difference or comments, please feel free to contact me ~

Can we make progress together after mutual discussion !!!!

WeiYan

2011.8.4

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.