筆記中記錄的是製做Debian 9的根文件系統,可是若是你跟着作一遍,Ubuntu的系統也是能夠自行製做的,無非是在構建文件系統的時候把發行版本換成Ubuntu的發行版,還有鏡像服務器。html
由於我製做這個根文件系統是爲了配合樹莓派的,因此當前筆記只記錄如何製做根文件系統,要想自定義完整的從內核開始製做到可燒錄到樹莓派的.img文件,可參考我另外一篇筆記 。node
經過vm虛擬機安裝的虛擬環境Linux,如何安裝網上一搜就有了,按照步驟安裝便可linux
編譯機:Ubuntu 18.04-desktop-amd64git
能用普通用戶執行的,不要用root用戶來執行,等須要用到root用戶時再切換。github
安裝如下依賴,有效防止報錯bootstrap
Cannot check Release signature; keyring file not available /usr/share/keyrings/debian-archive-keyring.gpgubuntu
$ sudo apt-get install debian-archive-keyring
複製代碼
安裝所需依賴,使用debootstrap命令建立文件系統。建立個工做目錄。vim
# 新建工做目錄 build
$ mkdir ~/build && cd ~/build
# 安裝必要依賴 debootstrap就是構建的命令
$ sudo apt-get install qemu qemu-user-static binfmt-support debootstrap
# 切換到root
$ su
# 構建文件系統的命令
$ debootstrap --arch=arm64 --foreign stretch linux-rootfs http://ftp.cn.debian.org/debian/
# 切換到普通用戶
$ su [user]
# qemu-aarch64-static是其中的關鍵,能在 x86_64 主機系統下 chroot 到 arm64 文件系統
$ sudo cp -a /usr/bin/qemu-aarch64-static ~/build/linux-rootfs/usr/bin/qemu-aarch64-static
複製代碼
--arch:指定製做的文件系統是什麼架構的bash
--foreign:在與主機架構不相同時須要指定此參數,僅作初始化的解包服務器
stretch:這個是Debian 9的發行版本號,爲何沒用最新的Debian 10的buster,由於更換國內的鏡像源老是有點問題Debian 發行版
linux-rootfs:這個是要存放文件系統的文件夾,能夠不用先建立,執行上述命令會自動建立此文件夾,也能夠先建立
ftp.cn.debian.org/debian/ :這個是中國鏡像服務器地址,Debian 全球鏡像站
接下來咱們能夠經過chroot進入到製做好的文件系統。 chroot wiki
這裏提供一個腳本文件來進入咱們的根文件系統,最好不要使用root用戶執行此腳本 ch-mount.sh
# 此腳本有兩個參數 -u 是取消掛載 -m 是掛載,爲何要掛載本機的設備文件,我也不太清楚
$ ./ch-mount.sh -m linux-rootfs
# 執行腳本後,沒有報錯會進入文件系統,顯示 I have no name ,這是正常的,不要慌張,我當時就有點懵逼,這是由於尚未初始化。
I have no name!@node2:/#
# 如下命令是在根文件系統中執行的命令
# 進行第二步,初始化文件系統,會把一個系統的基礎包等所有初始化
$ debootstrap/debootstrap --second-stage
# 初始化好了之後,退出文件系統,再次進入後就顯示root了
$ exit
# 再次進入時,不須要執行腳本,使用chroot命令便可,由於ch-mount腳本是爲了掛載本機文件與文件系統的關聯而已
$ sudo chroot linux-rootfs
複製代碼
若是腳本報錯 :/bin/sh^M:bad interpreter: No such file or directory,這是由於文件格式的錯誤,可經過如下方式解決
$ vim ch-mount.sh
# 設置文件格式爲unix 而後保存退出
:set ff=unix
:wq
複製代碼
上面完成後,如今在文件系統內,須要對文件系統進行一些DIY,安裝一些咱們必要的工具,和配置網絡等等,系統爲Debain 9,其餘發行版請自行更換命令。
要確保進入文件系統後有網絡,通常若是沒有網絡,能夠先把 /etc/resolv.conf 文件拷貝到 linux-rootfs/etc/resolv.conf,由於我構建的文件系統會默認把本機的 resolv.conf 拷貝進去,因此我沒有手動拷貝。
# 若是遇到沒法拉取 https 源的狀況,請先使用 http 源並安裝
$ apt install apt-transport-https
$ cp /etc/apt/source.list /etc/apt/source.list_bak
# 這裏用的vim.tiny是構建文件系統是自帶的,跟vim同樣也能夠編輯文件,把文件內容所有替換爲如下內容
$ vim.tiny /etc/apt/source.list
# 默認註釋了源碼鏡像以提升 apt update 速度,若有須要可自行取消註釋
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ stretch main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ stretch main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ stretch-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ stretch-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ stretch-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ stretch-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security stretch/updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security stretch/updates main contrib non-free
複製代碼
# 先ping www.baidu.com 看下是否有網絡,沒有網絡須要退出文件系統,把宿主機的reslov.conf文件拷貝到相應位置便可。
$ ping www.baidu.com
$ apt-get update
# 先設置root用戶的密碼
$ passwd
複製代碼
# 這兩個環境變量能夠自行修改
$ USER=pi
$ HOST=raspberry
$ useradd -G sudo -m -s /bin/bash $USER
$ passwd $USER
複製代碼
# 設置時區
$ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 使能串口,若是隻是爲了製做根文件系統,能夠不用運行這個命令,我是爲了樹莓派的串口調試纔開啓的
$ ln -s /lib/systemd/system/serial-getty\@.service /etc/systemd/system/getty.target.wants/serial-getty@ttyAMA0.service
複製代碼
# 安裝音頻管理
$ apt-get install alsa-utils libasound2-dev
# 安裝 vim 和 ssh
$ apt-get install vim ssh
# 安裝網絡管理工具
$ apt-get install ifupdown net-tools
# 安裝其餘依賴,若是還有其餘依賴須要安裝能夠繼續向後加入
$ apt-get install udev sudo wget curl
複製代碼
$ echo $HOST > /etc/hostname
$ echo "127.0.0.1 localhost.localdomain localhost" > /etc/hosts
$ echo "127.0.0.1 $HOST" >> /etc/hosts
$ echo "auto eth0" > /etc/network/interfaces.d/eth0
$ echo "iface eth0 inet dhcp" >> /etc/network/interfaces.d/eth0
複製代碼
以上所有完成後,咱們的根文件系統就製做好了,退出文件系統後調用 ch-mount.sh 腳本取消掛載就行了。
$ exit
$ ./ch-mount -u linux-rootfs
複製代碼
文件系統製做好了之後,關於用法,如今的我只知道能夠打包進.img鏡像文件中,其餘用法請自行查閱吧。
在使用 apt 命令安裝工具時,一直有警告信息:perl: warning: Setting locale failed.
$ apt-get install locales
$ dpkg-reconfigure locales
# 進入界面後,向下選上 en_US.UTF-8 和 zh_CN.UTF-8 選擇ok
# 進入下個界面後選擇默認的zh_CN.UTF-8 就能夠解決了。
# 用命令測試是否不報錯
$ perl -e exit
複製代碼
這裏爲何時vim.tiny呢,是由於這個命令在文件系統中自帶的,若是安裝了vim的話,用vim也能夠
$ apt-get install locales
# 打開文件後找到 en_US.UTF-8 和 zh_CN.UTF-8 刪掉 # 好 取消註釋
$ vim.tiny /etc/locale.gen
$ locale-gen
# 建立 /etc/locale.conf
$ vim.tiny /etc/locale.conf
LANG=zh_CN.UTF-8
# 用命令測試是否不報錯
$ perl -e exit
複製代碼