搭建最小ubuntu20.04系统

1.下载ubuntu rootfs

 去ubuntu官网下载自己想要的rootfs: 我下载的是 ubuntu-base-20.04.3-base-arm64.tar.gz

wget http://cdimage.ubuntu.com/ubuntu-base/releases/20.04.3/release/ubuntu-base-20.04.3-base-arm64.tar.gz #下载

sudo mkdir rootfs #创建文件夹
sudo tar xzvf ubuntu-base-20.04.3-base-arm64.tar.gz -C rootfs #解压到rootfs

2. 安装 qemu-user-static搭建arm64模拟环境

sudo apt install qemu-user-static  #安装qemu

sudo cp /usr/bin/qemu-arm-static rootfs/usr/bin/  #arm
sudo cp /usr/bin/qemu-aarch64-static rootfs/usr/bin/  #arm64

3.网络联网 

 拷贝一下DNS配置,不然等下chroot进去没法上网

sudo mv rootfs/etc/resolv.conf rootfs/etc/resolv.conf.bak
sudo cp /etc/resolv.conf rootfs/etc/

4.创建自动挂载脚本,exit 后自动卸载

vi ch-mount.sh

添加如下:
#!/bin/bash

function mnt() {
    echo "MOUNTING"
    sudo mount -t proc /proc ${1}/proc
    sudo mount -t sysfs /sys ${1}/sys    
    sudo mount -o bind /dev ${1}/dev
    #sudo mount -o bind /run ${1}/run 
    sudo mount -o bind /dev/pts ${1}/dev/pts
    sudo chroot ${1}
}

function umnt() {
    echo "UNMOUNTING"
    sudo umount ${1}/proc
    sudo umount ${1}/sys
    sudo umount ${1}/dev/pts
    #sudo umount ${1}/run
    sudo umount ${1}/dev
}


if [ -n "$1" ] ;
then
    mnt $1
    umnt $1
else
    echo ""
    echo "Either 1'st parameters were missing"
    echo ""
    echo "1'st parameter is the full path of rootfs directory(with trailing '/')"
    echo ""
    echo "For example: ./ch-mount rootfs"
    echo ""
    echo 1st parameter : ${1}
fi

5.进入系统

使用上面的脚本一键挂载chroot进入新系统

./ch-mount.sh rootfs
#进入后显示下面这样说明成功了
root@ubuntu:/# 

6.安装vim,替换软件源

chmod 777 -R /tmp/
export LC_ALL=C
apt update
apt install nano #编辑必备, 方便更换源,不然太慢了
apt install apt-transport-https ca-certificates #避免证书问题报错
mv /etc/apt/sources.list /etc/apt/sources.list.bak #备份
nano /etc/apt/sources.list

用如下替换:https://mirror.tuna.tsinghua.edu.cn/help/ubuntu-ports/
##——ubuntu20.04————————————————————————————————————————————————————————————————————————————————————
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-proposed main restricted universe multiverse

##——ubuntu18.04————————————————————————————————————————————————————————————————————————————————————
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-proposed main restricted universe multiverse

apt update

7.安装一些ubuntu必备的软件,按需安装

apt install language-pack-en-base sudo ssh vim kmod htop rsyslog net-tools ethtool ifupdown iputils-ping  isc-dhcp-client
apt intalll netplan.io #网络支持
apt install libx11-dev libgles2 libpangoft2-1.0-0 libharfbuzz0b libgles2 libdatrie1 libxkbcommon0 libwayland-egl1 libwayland-cursor0 libunwind8 libpng16-16 libpixman-1-0 libjpeg-turbo8 libinput10 libgles2 libevdev2 libcairo2 device-tree-compiler iso-codes libffi6 libncursesw5 libpython3-stdlib libpython3.8-minimal libpython3.8-stdlib libreadline7 libtinfo5 python3-minimal python3.8 libglvnd0 libdrm-common libdrm2 libegl-mesa0 libegl1 libegl1-mesa libgtk-3-0 libgbm1 libglapi-mesa libwayland-server0 libx11-xcb1 libxcb-dri3-0 libxcb-present0 libxcb-sync1 libxcb-xfixes0 libxshmfence1 libgstreamer1.0-0 libgstreamer-plugins-bad1.0-0 libpangocairo-1.0-0 libasound2 libasound2-data libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python python-minimal python2.7 python2.7-minimal
apt install bash-completion build-essential btrfs-progs cmake curl dnsutils htop iotop isc-dhcp-client iputils-ping kmod lin

8.系统基础修改

useradd -s /bin/bash -m -G adm,sudo yourname
useradd -G sudo -m -s /bin/bash jetson

passwd yourname
passwd root

chmod 4755 /usr/bin/sudo #不然会sudo报错sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set

##设置主机名
echo 'ubuntu.yourname' > /etc/hostname
## 设置自动更新DNS
dpkg-reconfigure resolvconf

9.配置登录的串口(重要)

## 修改此文件中内的串口设备名字,改为你设备的实际名称
/etc/systemd/system/getty.target.wants/[email protected]
##ConditionPathExists=/dev/ttyAMA0 此字段

10. ubuntu18.04 LTS 后,使用netplan配置网络

vi rootfs/etc/netplan/netcfg.yaml -'EOF'
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
    dhcp4: true
EOF

11.退出arm64模拟文件系统

exit

12.注意事项

kernel 需支持 
CONFIG_DEVTMPFS
CONFIG_DEVTMPFS_MOUNT
因为udev会/dev/,用的类型是devtmpfs
>>>>>>>>>rootfs 启动流程
>kernel--->/sbin/init(systemd)
>/sbin/init---->mount proc sys dev
>udveadm  trigger 创建所有设备
>启动服务

13.roofs ext4 生成

#1. 生成一个适当大小的数据块,这个大小参考du -h ubuntu-rootfs
dd if=/dev/zero of=rootfs.ext4 bs=1M count=500
#2. 格式化 
mkfs.ext4 rootfs.ext4
#3. 挂载此空镜像
mount -o loop rootfs.ext4 /mnt
#4.向挂载好的文件系统中写入需要加入的文件
sudo cp ubuntu-rootfs/* /mnt -rf
#5.卸载根文件系统
umount /mnt
#6. 收缩 rootfs.ext4
e2fsck -p -f rootfs.ext4
#7. 将rootfs.ext4 烧写到指定设备分区

你可能感兴趣的:(ubuntu构建,ubuntu,linux,运维)