天天看點

打造個人專屬的微型linux--具體實作篇

離上一篇啟動原理間隔了這麼多天,讓大家久等了。這期間在制作過程中個人也遇到了些許問題,是以斷斷續續的才把此篇寫完。

問題是在initrd階段識别不到磁盤裝置,挂載不了rootfs,谷歌了查了很多資料都沒得到解決。剛開始用的是centos6.2的核心,6.2更新了initrd,裡面的init腳本和以前有很大的不同,于是我換到5.8的核心,還是不行,又切換到5.4的核心,問題依然存在。火大,又自己去官網下載下傳了2.6.18的核心源代碼,基于安裝的centos5.4的config重新編譯了一次(編譯太耗時間),問題依舊存在。。。那時候真的是特别無語。最後還是找馬哥交流了下,在這裡還要多謝馬哥的指導,找到了問題的關鍵所在。大家有什麼問題也可以在馬哥的群裡一起探讨,群号在馬哥的部落格,位址在我的部落格連結中

在開始之前說說個人的近況。值得高興的是pass了國家軟考網絡工程師,對于這個證書,個人覺得以後在國企或者政府機關工作還是有點用處的,一般在私企和外企作用不是很大。不過總是比沒有的好。

網工過了,下半年的計劃就是把CCNA給過了,這個已經耽擱了很久了。主要是對自己的en很不自信。。。呵呵。。對于CCIE。。。還是算了,網絡這塊,折騰到CCNP也就差不多了。。。

好了,有點啰嗦了,接下來進入正題

總結在上一篇的linux啟動原理,我們知道要啟動一個linux所必須的有的東西有以下幾個

1.bootloader:系統引導程式GRUB

2.kernel:系統核心

3.initrd:引導核心的ramdisk

4.init:系統第一個程序

5.使用者接口bash

5.init,bash等程式所依賴的庫檔案和腳本

為了避免各位看官看的迷糊,首先先說下大概的一個流程

整個實驗的流程:安裝bootloader--複制kernel到硬碟--提供initrd--配置GRUB--提供init,bash,等必須的軟體和庫檔案

由以上流程羅列出以下所需要的一些配置檔案,程式等元素

配置檔案 /etc/inittab /boot/grub/grub.conf

腳本 /etc/rc.d/rc.sysinit

必須的程式

kernel

/sbin/init

/bin/bash

庫檔案:用ldd檢視具體需要哪些庫檔案

知道了以上所需要的東西,接下來,就可以開始動手在一塊硬碟中一步步的打造一個微型的linux了

具體思路是在一個安裝好的linux主控端中添加一塊新硬碟,在這塊硬碟上安裝一個微型的linux,再用另外一台虛拟機加載那塊硬碟啟動微型linux

OK,just do it !

ps:以下都在虛拟機中實作

step1:

首先添加一塊硬碟

<a href="http://lustlost.blog.51cto.com/attachment/201207/3/2600869_1341285319MqEy.jpg"></a>

<a href="http://lustlost.blog.51cto.com/attachment/201207/3/2600869_1341285362lQzJ.jpg"></a>

step2:

啟動宿主linux系統,對新添加的硬碟進行分區。這裡分一個boot分區,一個根分區和一個swap分區

<a href="http://lustlost.blog.51cto.com/attachment/201207/3/2600869_1341285405CBnO.jpg"></a>

可以看到,boot分區用了100MB,根分區我隻使用了512MB。從中可以看出這是一個極其精簡的linux。SWAP分區用了128MB,是以下面我将隻會使用64MB的記憶體來啟動這個linux。

分區完成後進行格式化,指令如下

[root@localhost ~]# partprobe /dev/sdb

[root@localhost ~]# mkfs.ext2 /dev/sdb1

[root@localhost ~]# mkfs.ext3 /dev/sdb2

[root@localhost ~]# mkswap /dev/sdb3

step3:

在主控端上挂在boot分區和根分區

[root@localhost ~]# mkdir /mnt/{boot,sysroot} –p

[root@localhost mnt]# mount /dev/sdb1 /mnt/boot/ 

[root@localhost mnt]# mount /dev/sdb2 /mnt/sysroot/

下面會将宿主系統的核心,程式等需要的檔案移植到這2個目錄

step4:

安裝bootloader,這裡用比較常用的grub作為引導程式

[root@localhost mnt]# grub-install --root-directory=/mnt /dev/sdb #這裡指定root目錄時,會自動搜尋此目錄下的boot目錄,此指令就直接将grub全部安裝完成,包括第一段的MBR和第二段放置在boot目錄下的grub程式

Probing devices to guess BIOS drives. This may take a long time. 

Installation finished. No error reported. 

This is the contents of the device map /mnt/boot/grub/device.map. 

Check if this is correct or not. If any of the lines is incorrect, 

fix it and re-run the script `grub-install'.

(fd0)    /dev/fd0 

(hd0)    /dev/sda 

(hd1)    /dev/sdb

安裝好grub後,等放置好核心後再回來配置grub

step5:

在根目錄下移植系統必須的一些基本的程式

首先建立根目錄下所必須的目錄

[root@localhost sysroot]# mkdir -p root home proc sys dev etc/rc.d bin sbin lib var tmp opt boot misc mnt media usr

然後先移植init這個系統父程序,這個程序的配置檔案為/etc/inittab,加載配置檔案後會執行/etc/rc.d/rc.sysinit這個系統初始化腳本來對系統進行一系列的操作

注意移植的時候要移植到相對應的目錄

[root@localhost sysroot]# cp /sbin/init /mnt/sysroot/sbin/

除了主程式,還要移植相應的庫檔案,init所依賴的庫檔案可以使用ldd指令來檢視

[root@localhost sysroot]# ldd /sbin/init 

    linux-gate.so.1 =&amp;gt;  (0x00ca5000) 

    libnih.so.1 =&amp;gt; /lib/libnih.so.1 (0x00514000) 

    libnih-dbus.so.1 =&amp;gt; /lib/libnih-dbus.so.1 (0x00a1e000) 

    libdbus-1.so.3 =&amp;gt; /lib/libdbus-1.so.3 (0x006a9000) 

    libpthread.so.0 =&amp;gt; /lib/libpthread.so.0 (0x00d92000) 

    librt.so.1 =&amp;gt; /lib/librt.so.1 (0x0020e000) 

    libgcc_s.so.1 =&amp;gt; /lib/libgcc_s.so.1 (0x005e1000) 

    libc.so.6 =&amp;gt; /lib/libc.so.6 (0x00217000) 

    /lib/ld-linux.so.2 (0x0080c000)

查詢到所需的庫檔案後,将這些庫檔案移植到對應的目錄。

[root@localhost sysroot]# cp /lib/libnih.so.1 /lib/libnih-dbus.so.1 /lib/libdbus-1.so.3 /lib/libpthread.so.0 /lib/librt.so.1 /lib/libgcc_s.so.1 /lib/libc.so.6 /mnt/sysroot/lib/

用同樣的方法再移植bash

[root@localhost sysroot]# cp /bin/bash /mnt/sysroot/bin/

[root@localhost bin]# ln -s bash sh #不要忘了為bash建立連結檔案

[root@localhost sysroot]# ldd /bin/bash 

    linux-gate.so.1 =&amp;gt;  (0x004c4000) 

    libtinfo.so.5 =&amp;gt; /lib/libtinfo.so.5 (0x00e05000) 

    libdl.so.2 =&amp;gt; /lib/libdl.so.2 (0x00c1d000) 

    libc.so.6 =&amp;gt; /lib/libc.so.6 (0x00110000) 

    /lib/ld-linux.so.2 (0x00954000)

[root@localhost sysroot]# cp /lib/libtinfo.so.5 /lib/libdl.so.2 /lib/ld-linux.so.2 /mnt/sysroot/lib/

/lib/libc.so.6這個庫檔案是init和bash都需要使用到的,之前移植init時已經cp過去了,這次就不用重複cp了

其它需要用到的程式都可以使用這種方法進行移植,這裡隻提供了最基本的bash,其它程式可以按照自己的需要進行移植

這裡我再移植了一個ls指令,步驟這裡就不再羅列出了,等制作好後登入到這個微型linux後,出了bash内置的指令外,我将隻能在bash下使用ls指令

之前移植了init後,還沒有建立init的配置檔案,接下來就先建立init配置檔案inittab

[root@localhost bin]# vim /mnt/sysroot/etc/inittab

寫入下面2行

id:3:initdefault: 

si::sysinit:/etc/rc.d/rc.sysinit

表示預設啟動的運作級别為3,和系統開機時初始化腳本的位置

建立系統初始化腳本

[root@localhost bin]# vim /mnt/sysroot/etc/rc.d/rc.sysinit

#!/bin/bash 

echo -e "\t hello world ,welcome to the mini linux by lustlost" 

[root@localhost bin]# chmod +x /mnt/sysroot/etc/rc.d/rc.sysinit #不要忘了給腳本加上執行權限

step6:

[root@localhost dev]# yum groupinstall "Development Libraries" -y 

[root@localhost dev]# yum groupinstall "Development Tools" -y

好了,程式都移植好了,下面就可以移植核心和initrd了

我們直接使用主控端上的核心(之前忘了說了,主控端是最新的centos6.2 minimal版本,使用的是核心版本是2.6.32-220.el6.i686)

[root@localhost ~]# cp /boot/vmlinuz-2.6.18-308.el5 /mnt/boot/vmlinuz #核心的移植很簡單,直接cp就行了

接下來要移植引導核心的initrd.img了。如果主控端和要移植的微型linux的分區不一樣的話,解包initrd編輯一下裡面的init修改這一行

mkrootdev -t ext3 -o defaults,ro /dev/sda2

確定這一行指定的根檔案系統是對應的,然後重新打包

[root@localhost ~]# zcat /boot/initrd-2.6.18-164.el5.img | cpio –id #解包指令

[root@localhost tmp]# find . | cpio -o -H newc --quiet | gzip &amp;gt; /mnt/boot/initrd.gz #重新打包指令

然後繼續移植。。。我這裡主控端和微型linux分區是一樣的,是以就重新打包示範了

[root@localhost tmp]# cp /boot/initrd-2.6.18-308.el5.img /mnt/boot/initrd.img

step7:

移植好核心和initrd,就可以配置grub的引導配置檔案了

[root@localhost ~]# vi /mnt/boot/grub/grub.conf

<a href="http://lustlost.blog.51cto.com/attachment/201207/3/2600869_1341285448ZADC.jpg"></a>

OK ,曆經千辛萬苦,有工作全部就緒,我們開始建立一個虛拟機加載制作好的微型linux的硬碟,看看能不能啟動起來

建立自定義虛拟機,使用剛剛在主控端上制作的,上面放置着微型linux的硬碟

<a href="http://lustlost.blog.51cto.com/attachment/201207/3/2600869_1341285491lgdq.jpg"></a>

<a href="http://lustlost.blog.51cto.com/attachment/201207/3/2600869_1341285534vOf4.jpg"></a>

<a href="http://lustlost.blog.51cto.com/attachment/201207/3/2600869_1341285576nXJD.jpg"></a>

看到了麼?一個配置極其猥瑣和寒酸的虛拟機,隻搭載了64MB的記憶體,其餘的配置能删的都删了。

step8:

好了,開始啟動一下這個微型linux

開機grub啟動菜單

<a href="http://lustlost.blog.51cto.com/attachment/201207/3/2600869_13412856190euY.jpg"></a>

初始化各種硬體

<a href="http://lustlost.blog.51cto.com/attachment/201207/3/2600869_134128566291uw.jpg"></a>

成功進入系統

<a href="http://lustlost.blog.51cto.com/attachment/201207/3/2600869_1341285705aV37.jpg"></a>

使用一下ls指令

<a href="http://lustlost.blog.51cto.com/attachment/201207/3/2600869_1341285748HUm8.jpg"></a>

進入到bin目錄下,隻有bash和ls這2個程式,想要移植其它程式按照上面的方法進行移植就行了

<a href="http://lustlost.blog.51cto.com/attachment/201207/3/2600869_1341285791WORl.jpg"></a>

至此,一個微型的linux的建立成功,隻有64MB的記憶體就啟動起來了,各種擴充功能就可以按照自己的需求自由定制了

最後說下開篇說的我遇到的問題,其實很SB的一個問題,因為主控端是centos6.2的系統,由于6.2中使用了比較新的initramfs,裡面的腳本和以前的大不一樣,于是後來使用了5.8的核心,但之前建立的minilinux的虛拟機是依照redhat 6建立的,是以做好系統後一直啟動不了。後來在馬哥的提醒下重新建立了redhat 5的虛拟機,成功啟動了。。。

這裡我想說的是,有時候一個疏漏的小細節會導緻整個系統的失敗。以後工作中要切記不要遺漏細節。。。細節決定成敗。。。

本文轉自lustlost 51CTO部落格,原文連結:http://blog.51cto.com/lustlost/916852,如需轉載請自行聯系原作者

繼續閱讀

Dan Bin and Ren Zeping, the two top financial and financial leaders, named each other

Red Star Capital Bureau reported on October 13 that after the "mutual confrontation" due to different views on the A-share market a few days ago, private equity boss Dan Bin and Internet celebrity economist Ren Zeping still have no signs of "truce", and have shifted from market analysis to the investment performance of Dan Bin's private equity Oriental Harbor.

On the evening of October 12, Dan Bin forwarded the latest announcement of the performance of 10 billion private equity fund products in September, and the top 15 were covered by the products under his own management, but Bin said that "it is a pity that the performance cannot be fully displayed, otherwise the top 100 are all products managed by me." Early in the morning of October 13, Ren Zeping "replied" again: "If you are good, you will say that it is your own credit, and if you are bad, you will throw the blame on your company's employees and temporary workers." Red Star Capital Bureau noticed that as of press time, Ren Zeping's Weibo was no longer visible.

On October 13, the Red Star Capital Bureau sent interview requests to Ren Zeping and Dan Bin respectively, but did not receive a response as of press time.

Ren Zeping criticized the bears for stepping short

Dan Bin responded: Economists should be rational and objective

Recently, the A-share market has fluctuated violently, and various views are-for-tat. In the past few days, private equity boss Dan Bin and Internet celebrity economist Ren Zeping have attracted attention on Weibo.

On October 2, Dan Bin gave an analysis of the market before the National Day: "If it soars like this, there will be a sharp fall, and if it is trapped again this time, ......" On October 4, Ren Zeping posted on Weibo, saying, "It is expected that A-shares will close at the opening, and you can get off work early." Therefore, it is recommended to hold shares for the holiday before the holiday. However, as of press time, the Weibo content of the above two people is no longer visible.

Dan Bin and Ren Zeping, the two top financial and financial leaders, named each other

On October 8, the first trading day after the National Day holiday, the A-share market "shook" on the morning of the same day, and the Shanghai Composite Index soared by more than 10% at the opening, but then the rise narrowed sharply. As of the close of the day, the Shanghai Composite Index rose 4.59%, the Shenzhen Component Index rose 9.17%, the ChiNext Index rose 17.25%, the Beijing Stock Exchange 50 Index rose 24.71%, and more than 5,000 stocks in the two cities rose. But in the following days, A-shares moved lower.

On October 10, Dan Bin posted: "I warned on October 2...... And I was scolded for warning of the danger! On October 11, Dan Bin said, "Should the 'experts' and 'scholars' who shouted the big bull market and made shareholders rush in and some self-media authors come out to apologize?" ”

Dan Bin and Ren Zeping, the two top financial and financial leaders, named each other

On October 11, Ren Zeping issued an article saying: Recently, some grotesque views have not only been in vain, but also shorted China's economy and public policies at a critical time, which is professionally incompetent and morally shameless. Ren Zeping said that some people are singing short and bearish at this time, what do they think? Eat Chinese food, smash Chinese bowls? Even if you make a mistake, there is no need to be so bottomless, lacking both professional common sense and moral bottom line. It's okay to be bearish on the stock market, but also bearish on China's economy and macro policies, and only other people's economies and stock markets are good? Who knows that in the past few decades, China's economic growth rate has not only led United States, but also led the world.

Dan Bin and Ren Zeping, the two top financial and financial leaders, named each other

Although he was not named, Ren Zeping's comment area below Weibo pointed to Dan Bin, and netizens commented: "I am on the same page with Mr. Dan." ”

On October 12, Dan Bin posted a response on Weibo: Regarding Mr. Ren Zeping's "rhythm", I want to say that as an economist, you should be rational and objective, rather than using "conspiracy theories" and populism to try to divert attention and attention. I have no opinion on Mr. Ren Zeping himself, I used to often turn his articles, mainly because the data in the article is worth referencing. However, there are different views on his "opening and closing" and advocating a big bull market, especially in the context of "the big move of deposits to the stock market...... I think as an economist it is better to be more "rational and objective" and not impulsive!

However, Bin also stressed that there is no problem of going short in Oriental Harbor, and the US stocks, A-shares and Hong Kong stocks held by the fund he is responsible for have not moved, and there is no change in long-term investment, and he is optimistic about artificial intelligence and China's core high-yield assets. "I have no other opinions on Mr. Ren Zeping, the judgment on the direction of the stock market, seek common ground while reserving differences!"

Dan Bin and Ren Zeping, the two top financial and financial leaders, named each other

But Bin posted the latest performance of the funds under his management

Ren Zeping responded: Good shouting, bad dumping

Up to now, there is still no sign of a "truce" between Bin and Ren Zeping, and it has shifted from market analysis to the investment performance of Oriental Harbor.

According to the data, Oriental Harbor (full name "Shenzhen Oriental Harbor Investment Management Co., Ltd.") was founded in 2004 and is one of the first 33 institutions in China to obtain a private equity fund management license.

On the evening of October 12, Dan Bin forwarded the latest 10 billion private equity fund results in September announced by the private placement network on Weibo, and all of Dan Bin's performance display products won the top 15 of the 10 billion private equity product list. But Bin said, "It's a bit out of my imagination, I thought that the Oriental Harbor Fund I was responsible for would lose its first position after a surge at the end of September...... It's a pity that I can't show all the results, otherwise the top 100 are all products I manage."

Dan Bin and Ren Zeping, the two top financial and financial leaders, named each other
Dan Bin and Ren Zeping, the two top financial and financial leaders, named each other

Judging from the data, as of September 30, the net unit value of Oriental Harbor Marathon No. 15 was 1.7795 yuan, with an income of 45.62% this year; The net unit value of the global class B share of the Oriental Harbor Marathon is 7.7462 yuan, with a return of 44.03% since the beginning of this year; The net unit value of Oriental Harbor Tianen Marathon No. 2 is 2.8526 yuan, with a year-to-date gain of 44.01%.

In this regard, Ren Zeping said on October 13: Tell me some truth in the industry, there are grotesque people with radical styles, the company's product performance has suffered large-scale losses, cut in half, and several of them have been publicized every day, and they have lost so much to investors, and they have never formally apologized, and they shout and show off every day. If you are good, you will say that it is your own credit, and if you are bad, you will throw the blame on your company's employees and temporary workers, everyone knows.

"While reminding investors to pay attention to risks and be rational, he has a radical style, and uses investors' money to gamble aggressively and lose money for huge management fees. While talking about patriotism, he shorted A-shares, used the money of domestic investors to speculate in U.S. stocks, and even said that he was short-selling the Chinese market. There is no bottom line, hypocrisy, dishonesty, and people in the industry send 'but not the group'. A large number of small and medium-sized investors who have been cheated have no way to complain and are often blocked. I hope you keep your eyes open. Ren Zeping said. As of press time, Ren Zeping's Weibo is no longer visible.

Dan Bin and Ren Zeping, the two top financial and financial leaders, named each other

Red Star Capital Bureau noticed that judging from the performance of Oriental Harbor's products posted by Ren Zeping, some are not the latest results, and some are not managed by Dan Bin himself. Taking "Oriental Harbor Spring Breeze No. 3" as an example, the daily fund network shows that as of October 10 this year, the net unit value of the product was 0.2980 yuan, and the performance since its inception in May 2019 was -64.60%, significantly underperforming the CSI 300 Index. The fund manager of this product is not Dan Bin, but Ren Renxiong.

Dan Bin and Ren Zeping, the two top financial and financial leaders, named each other

In fact, Ren Zeping posted a screenshot of the loss of the above products on October 12, but Bin also mentioned in reply to netizens' comments: "Mr. Ren Zeping is very not objective, these two funds are operated by Mr. Ren Renxiong, and they are his own family!" He should indicate the fund manager. ”

It is worth mentioning that Ren Zeping's career has also been accompanied by controversy. In 2017, Ren Zeping joined Evergrande Group as Chief Economist (Vice President level) and President of Evergrande Economic Research Institute. At that time, his monthly salary was 1.25 million yuan, and it is speculated that his annual salary was as high as 15 million yuan, so he was also called "the most expensive economist".

In April 2022, after only one year as the chief economist of Soochow Securities, Ren Zeping stepped down. Since then, he has sold paid courses for knowledge through his official account "Zeping Macro", and last year he sold hair growth products. In January 2024, Beijing Funakang Biotechnology Co., Ltd., in which Ren Zeping is a shareholder, was fined 80,000 yuan by the Beijing Municipal Administration for Market Regulation for making false and misleading commercial promotions about the functions of its products.

Red Star News reporter Jiang Ziwen

Edited by Yang Cheng

(Download Red Star News, there are prizes for reporting!) )

Dan Bin and Ren Zeping, the two top financial and financial leaders, named each other