Linux(2)ubuntu rootfs根文件系统制作

简介: Linux(2)ubuntu rootfs根文件系统制作

0x00 介绍

debootstrap 和 minbase 是两种不同的工具,用于在 Debian 和其他基于 Debian 的发行版中创建新的基础安装。debootstrap 是一种常用的工具,可用于创建新的基础安装。它通过从指定的源下载和安装所需的软件包来工作。 debootstrap 可以在本地或远程主机上使用,并支持安装软件包的自定义选择。minbase 是一种更轻量级的工具,用于创建新的基础安装。

0x01 安装依赖

安装debootstrap和qemu-user-static

# apt install apt-transport-https qemu qemu-user-static binfmt-support debootstrap
sudo apt-get update
sudo apt-get install qemu-user-static #minbase用这个 qemu
sudo apt-get install debootstrap #

0x02 准备脚本

创建ch-mount.sh文件,写入以下脚本

#!/bin/bash
# 
function mnt() {
  echo "MOUNTING..."
  sudo mount -t proc /proc ${2}proc
  sudo mount -t sysfs /sys ${2}sys
  sudo mount -o bind /dev ${2}dev
  sudo mount -o bind /dev/pts ${2}dev/pts
  echo "CHROOT..."
  sudo chroot ${2}
  echo "Success!"
}
function umnt() {
  echo "UNMOUNTING"
  sudo umount ${2}proc
  sudo umount ${2}sys
  sudo umount ${2}dev/pts
  sudo umount ${2}dev
}
if [ "$1" == "-m" ] && [ -n "$2" ] ;
then
    mnt $1 $2
elif [ "$1" == "-u" ] && [ -n "$2" ];
then
    umnt $1 $2
else
  echo ""
  echo "Either 1'st, 2'nd or both parameters were missing"
  echo ""
  echo "1'st parameter can be one of these: -m(mount) OR -u(umount)"
  echo "2'nd parameter is the full path of rootfs directory(with trailing '/')"
  echo ""
  echo "For example: ch-mount -m /media/sdcard/"
  echo ""
  echo 1st parameter : ${1}
  echo 2nd parameter : ${2}
fi

创建 mkrootfs.sh文件,写入以下脚本,其中2048取决于你的根文件系统实际大小,比如安装了桌面环境后若大小超过2048M,就改大一点

#!/bin/bash
# 
dd if=/dev/zero of=ubuntu-rootfs.img bs=1M count=2048
sudo  mkfs.ext4  ubuntu-rootfs.img
rm -r rootfs
mkdir  rootfs
sudo mount ubuntu-rootfs.img rootfs/
sudo cp -rfp ubuntu-rootfs/*  rootfs/
sudo umount rootfs/
e2fsck -p -f ubuntu-rootfs.img
resize2fs  -M ubuntu-rootfs.img

chmod a+x ch-mount.sh和 chmod a+x mkrootfs.sh给执行权限。

0x03 minbase构建

ubuntu-base 是Ubuntu官方构建的ubuntu最小文件系统,包含debain软件包管理器,基础包大小通常只有几十兆,其背后有整个ubuntu软件源支持,ubuntu软件一般稳定性比较好,基于ubuntu-base按需安装Linux软件,深度可定制…,常用于嵌入式rootfs构建。

嵌入式常见的几种文件系统构建方法:busybox、yocto、builroot,我觉得它们都不如Ubuntu方便,强大的包管系统,有强大的社区支持,可以直接apt-get install来安装新软件包。本文介绍了如何基于Ubuntu-base构建完整的ubuntu 系统。

# 1.下载minbase版本
wget http://cdimage.ubuntu.com/ubuntu-base/releases/20.04.5/release/ubuntu-base-20.04.5-base-arm64.tar.gz
# 2.新建文件夹
mkdir ubuntu-base-20.04.5-rootfs 
# 3.解压minbase tar到新建文件夹
tar -xpf ubuntu-base-20.04.5-base-arm64.tar.gz  -C ubuntu-base-20.04.5-rootfs
# 4.拷贝网络配置,minbase是没有网络配置的,不然挂载进去上不了网
cp -b /etc/resolv.conf ubuntu-base-20.04.5-rootfs/etc/resolv.conf
# 5.拷贝qemu
cp /usr/bin/qemu-aarch64-static ubuntu-base-20.04.5-rootfs/usr/bin/
# 6.进入qemu
./ch-mount.sh -m ubuntu-base-20.04.5-rootfs #mount
#-------------------------qemu空间-------------------------------------
# 7.更新,这里面就是minbase,随便怎么玩都行
apt-get update
# 8.安装些常用工具
apt-get -y install  vim nfs-common  sudo ssh net-tools ethtool wireless-tools  xfce4-power-manager xinit  network-manager iputils-ping rsyslog   bash-completion lxtask htop  synaptic  --no-install-recommends
# 9.设置root密码
passwd root #连续输入两次密码即可
# 10.退出arm64模拟文件系统
exit
#-------------------------qemu空间-------------------------------------
./ch-mount.sh -u ubuntu-base-20.04.5-rootfs #umount
./mkrootfs.sh #制作镜像,注意这里要看脚本写的哪些文件夹,要对应上。

0x04 qemu-debootstrap构建

debootstrap 是一个用于在目标系统中安装基本的 Debian 系统的工具。它通过下载包的构建器,从指定的 Debian 源下载包并安装到目标系统中。

要使用 debootstrap,需要在终端中输入以下命令:

sudo debootstrap [options] <suite> <target> [<mirror> [<script>]]

其中,<suite> 是要安装的 Debian 版本,例如 stable、testing 或 unstable。<target> 是要安装的目标目录,<mirror> 是 Debian 源的 URL,<script> 是可选的安装脚本。

例如,要安装最新版本的 Debian stable 版到 /mnt/debian 目录,可以使用以下命令:

sudo debootstrap stable /mnt/debian Index of /debian

更多关于 debootstrap 的用法,请查看 debootstrap 工具的文档。

# 1.下载一个arch为arm64的,focal版本(20.04),下载到ubuntu-arm64-focal_min4文件夹
# sudo debootstrap --arch arm64 bullseye /mnt/debian_arm64  https://mirrors.tuna.tsinghua.edu.cn/debian/
sudo qemu-debootstrap --arch=arm64 focal ubuntu-arm64-focal_min8   http://mirrors.ustc.edu.cn/ubuntu-ports  
# 2.拷贝qemu
cp /usr/bin/qemu-aarch64-static ubuntu-arm64-focal_min6/usr/bin/
# 3.进入qemu
./ch-mount.sh -m ubuntu-arm64-focal_min6 #mount
#-------------------------qemu空间-------------------------------------
# 4.设置root密码
passwd root #连续输入两次密码即可
# 5.退出arm64模拟文件系统
exit
#-------------------------qemu空间-------------------------------------
./ch-mount.sh -u ubuntu-arm64-focal_min6 #umount
./mkrootfs.sh #制作镜像,注意这里要看脚本写的哪些文件夹,要对应上。
4.10 Warty Warthog(长疣的疣猪)
5.04 Hoary Hedgehog(灰白的刺猬)
5.10 Breezy Badger(活泼的獾)
 
6.06(LTS) Dapper Drake(整洁的公鸭)
6.10 Edgy Eft(急躁的水蜥)
7.04 Feisty Fawn(坏脾气的小鹿)
7.10 Gutsy Gibbon(勇敢的长臂猿)
 
8.04(LTS) Hardy Heron(耐寒的苍鹭)
8.10 Intrepid Ibex (勇敢的野山羊)
9.04 Jaunty Jackalope(得意洋洋的怀俄明野兔)
9.10 Karmic Koala(幸运的考拉)
 
10.04(LTS) Lucid Lynx(清醒的猞猁)
10.10 Oneiric Ocelot(梦幻的豹猫)
11.04 Natty Narwhal(敏捷的独角鲸)
11.10 Oneiric Ocelot(有梦的虎猫)
 
12.04(LTS) Precise Pangolin(精准的穿山甲)
12.10 Quantal Quetzal(量子的绿咬鹃)
13.04 Raring Ringtail(铆足了劲的猫熊)
13.10 Saucy Salamander(活泼的蝾螈)
 
14.04(LTS) Trusty Tahr (可靠的塔尔羊)(LTS)
14.10 Utopic Unicorn(乌托邦独角兽)
15.04 Vivid Vervet (活泼的小猴)
15.10 Wily Werewolf (狡猾的狼人)
 
16.04(LTS) Xenial Xerus (好客的非洲地松鼠)
16.10 Yakkety Yak(牦牛)
17.04 Zesty Zapus(开心的跳鼠)
17.10 Artful Aardvark(机灵的土豚)
 
18.04(LTS) Bionic Beaver(仿生海狸)
18.10 Cosmic Cuttlefish(宇宙墨鱼)
19.04 Disco Dingo(舞动的灵犬)
19.10 Eoan Ermine(白貂)
20.04(LTS) Focal Fossa(专注的马达加斯加长尾狸猫)
在 /etc/apt/sources.list 文件中,将软件源的地址改为 https://mirrors.aliyun.com/ubuntu-ports/
deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
 
deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
 
deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
 
# deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
 
deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

0x05 常见问题

  1. 如果跑arm芯片 , debootstrap构建arch要选arm64

进去也可以检查一下依赖库是否已经是aarch64的

  1. 打包出错

执行./mkrootfs.sh出错, 直接重启终端 , 不是重启机器!! 还有问题sudo umount 对应的文件夹

0x06 其他记录

  1. 镜像源

企业镜像

阿里巴巴开源镜像站:阿里巴巴开源镜像站-OPSX镜像站-阿里云开发者社区

华为开源镜像站:华为开源镜像站_软件开发服务_华为云

腾讯开源镜像站:腾讯软件源

网易开源镜像站:欢迎访问网易开源镜像站

搜狐开源镜像站:Index of /

大学镜像

清华大学开源镜像站:清华大学开源软件镜像站 | Tsinghua Open Source Mirror

华中科技大学开源镜像站:http://mirror.hust.edu.cn/

浙江大学开源镜像站:http://mirrors.zju.edu.cn/

北京理工大学开源镜像站:http://mirror.bit.edu.cn/

中国科技大学开源镜像站:USTC Open Source Software Mirror

# 换了以下的不行,最终用的是中科大的源

sudo sed -i 's/mirrors.aliyun.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list

sudo sed -i 's/mirrors.tuna.tsinghua.edu.cn/mirrors.huaweicloud.com/g' /etc/apt/sources.list

sudo sed -i 's/mirrors.huaweicloud.com/mirrors.cloud.tencent.com/g' /etc/apt/sources.list

linux-headers 和 linux-image

linux-headers 和 linux-image 是两个 deb 包,可安装在 Debian/Ubuntu 系统中。

linux-headers 包含各种头文件,可以让设备具有本地编译驱动的能力。

linux-images 包含编译内核时产生的驱动模块,将这些模块安装到设备中,设备才能 modprobe/insmod

进行使用。如果设备使用了 extboot,自行编译的 linux-image 内还会包含内核,安装即可直接升级内核,免去烧写步骤。

相关文章
|
2天前
|
Ubuntu Linux
查看Linux系统架构的命令,查看linux系统是哪种架构:AMD、ARM、x86、x86_64、pcc 或 查看Ubuntu的版本号
查看Linux系统架构的命令,查看linux系统是哪种架构:AMD、ARM、x86、x86_64、pcc 或 查看Ubuntu的版本号
23 3
|
4天前
|
编解码 Linux 程序员
深度探索Linux操作系统 —— 构建根文件系统2
深度探索Linux操作系统 —— 构建根文件系统
22 12
|
4天前
|
Linux Shell 网络安全
深度探索Linux操作系统 —— 构建根文件系统1
深度探索Linux操作系统 —— 构建根文件系统
20 6
|
5天前
|
存储 监控 Linux
|
5天前
|
存储 监控 Linux
|
1天前
|
存储 人工智能 数据管理
深入理解Linux操作系统之文件系统管理探索人工智能:从理论到实践的旅程
【8月更文挑战第30天】在探索Linux的无限可能时,我们不可避免地会遇到文件系统管理这一核心话题。本文将深入浅出地介绍Linux文件系统的基础知识、操作命令及高级技巧,帮助你更有效地管理和维护你的系统。从基础概念到实践应用,我们将一步步揭开Linux文件系统的神秘面纱。
|
5天前
|
算法 Linux 索引
Linux0.11 根文件系统挂载(四)
Linux0.11 根文件系统挂载(四)
7 0
|
7天前
|
存储 Linux 网络安全
【Azure 存储服务】如何把开启NFS 3.0协议的Azure Blob挂载在Linux VM中呢?(NFS: Network File System 网络文件系统)
【Azure 存储服务】如何把开启NFS 3.0协议的Azure Blob挂载在Linux VM中呢?(NFS: Network File System 网络文件系统)
|
9天前
|
Ubuntu Shell 网络安全
安装了ubuntu虚拟机后发现shell无法连接 ubuntu开启ssh连接
【8月更文挑战第23天】安装了ubuntu虚拟机后发现shell无法连接
46 6
|
2天前
|
Ubuntu Shell C++
在Ubuntu18.04上安装ros2的环境,ros2的常用命令:播放包、录制包等
在Ubuntu18.04上安装ros2的环境,ros2的常用命令:播放包、录制包等
9 1
下一篇
云函数