编辑
2024-01-08
工作知识
0
请注意,本文编写于 516 天前,最后修改于 451 天前,其中某些信息可能已经过时。

目录

ubuntu2004的chroot环境
镜像地址
获取环境
准备chroot
配置源
禁止一些低级操作
提示chroot
装包
打包
导入docker

ubuntu2004的chroot环境

工作需要ubuntu2004的chroot开发环境,故这里记录搭建ubuntu2004的chroot环境。

镜像地址

如下:

https://releases.ubuntu.com/20.04.6/

获取环境

mkdir iso && mount ubuntu-20.04.6-desktop-amd64.iso iso/ unsquashfs iso/casper/filesystem.squashfs umount iso && rm -rf iso

准备chroot

为了能够chroot的一些编译工作正常,更真实的模拟实际环境,这里需要和linux启动一致,挂载必要的文件系统如下:

vim 1-chroot #!/bin/bash CHROOT_DIR=/root/sdk/chroot/squashfs-root [ ! -d ${CHROOT_DIR} ] && exit cd ${CHROOT_DIR} mountpoint -q ./proc || mount -t proc proc ./proc mountpoint -q ./sys || mount -t sysfs sysfs ./sys mountpoint -q ./dev || mount -t devtmpfs devtmpfs ./dev mountpoint -q ./dev/pts || mount -t devpts devpts ./dev/pts chroot .

为了能够全局使用,可以放在/usr/local/sbin/下面

mv 1-chroot /usr/local/sbin/

到这里,chroot环境已经基本成型,下面需要针对这个chroot环境做一些易用的改动。

配置源

配置源之前先设置dns,这里是chroot环境,所以直接修改resolv.conf文件,这里以114为例

nameserver 114.114.114.114

然后修改源地址为国内,修改文件/etc/apt/sources.list如下

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted deb http://mirrors.aliyun.com/ubuntu/ focal universe deb http://mirrors.aliyun.com/ubuntu/ focal-updates universe deb http://mirrors.aliyun.com/ubuntu/ focal multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-updates multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted deb http://mirrors.aliyun.com/ubuntu/ focal-security universe deb http://mirrors.aliyun.com/ubuntu/ focal-security multiverse

最后update一下即可

禁止一些低级操作

为了避免reboot和一些低级操作,这里需要处理一下(在特殊情况下会生效),故如下

vim /etc/profile.d/chroot.sh hint_reboot(){ info="Forbidden reboot!" echo -e "\033[31m${info}\033[0m" } hint_poweroff(){ info="Forbidden poweroff!" echo -e "\033[31m${info}\033[0m" } hint_halt(){ info="Forbidden halt!" echo -e "\033[31m${info}\033[0m" } alias reboot='hint_reboot' alias poweroff='hint_poweroff' alias halt='hint_halt'

提示chroot

为了提示自己是在chroot环境中,修改bashrc如下:

vim /root/.bashrc # 1.修改TERM为xterm-color TERM=xterm-color cd source /etc/profile.d/chroot.sh # 2.修改\h为chroot 54 if [ "$color_prompt" = yes ]; then 55 # PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' 56 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@chroot\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' 57 else 58 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' 59 fi ......

装包

基础开发环境包安装

apt-get install git ssh make gcc libssl-dev liblz4-tool expect \ g++ patchelf chrpath gawk texinfo chrpath diffstat binfmt-support \ qemu-user-static live-build bison flex fakeroot cmake gcc-multilib \ g++-multilib unzip device-tree-compiler ncurses-dev rsync gdb-multiarch \ openocd qemu-system-arm cargo doxygen apt-get install libgucharmap-2-90-dev bzip2 expat gpgv2 \ cpp-aarch64-linux-gnu libgmp-dev libmpc-dev bc python-is-python3 \ python2 libsqlite3-dev esmtp codespell python-ply mutt tig u-boot-tools \ meson ninja-build u-boot-tools build-essential devscripts breeze-dev \ debhelper extra-cmake-modules libcap-dev libxcb-composite0-dev \ libdrm-dev libgbm-dev libxcb-cursor-dev libxcb-damage0-dev \ libxcb-glx0-dev libxcb-randr0-dev libxcb-util-dev # 可能出现图形化服务器选择lightdm/gdm apt-get install libtool qtmultimedia5-dev \ libdbusmenu-qt5-dev libfcitx-qt5-dev libfcitx5-qt-dev \ libgsettings-qt-dev libkf5bluezqt-dev libkf5networkmanagerqt-dev \ liblightdm-qt5-3-dev libpolkit-qt5-1-dev libpoppler-qt5-dev \ libqt5charts5-dev libqt5opengl5-dev libqt5sensors5-dev \ libqt5svg5-dev libqt5texttospeech5-dev libqt5virtualkeyboard5-dev \ libqt5waylandclient5-dev libqt5webchannel5-dev libqt5webkit5-dev \ libqt5x11extras5-dev libqt5xdg-dev libqt5xdgiconloader-dev \ libqtav-dev libqtav-private-dev qt5-default qtbase5-dev \ qtbase5-dev-tools qtbase5-private-dev qtchooser \ qtconnectivity5-dev qtdeclarative5-dev qtdeclarative5-dev-tools \ qtdeclarative5-private-dev qtlocation5-dev qtmultimedia5-dev \ qtpositioning5-dev qtquickcontrols2-5-dev qtscript5-dev \ qttools5-dev qttools5-dev-tools qtwayland5-dev-tools \ qtwebengine5-dev qtwebengine5-private-dev libkdecorations2-dev \ libkf5xmlgui-dev libkf5crash-dev kscreenlocker-dev kinit-dev pkg-kde-tools

打包

umount dev/pts dev proc sysfs && cd ..
tar cvzf ubuntu-2004-develop.tar.gz squashfs-root/
至此获得ubuntu-2004-develop.tar.gz是可用于x86的ubuntu2004的chroot

导入docker

docker import ubuntu-2004-develop.tar.gz x86-ubuntu2004-dev:v1
这样docker内也能正常使用此环境