簡介
在做嵌入式開發時,常常發現主機環境會造成工具的使用影響,不同的工具會需要不同版本的作業環境。總不能對每套工具買一臺主機吧?
Multipass / Ducker / VirtualBox 的系統代價也很高,虛擬環境不能使用主機已有的資源,或還需要限制資源。
有沒有簡單而能達成工作環境切換的輕量方式呢?
Schroot 是一個強大的工具,允許用戶程序在沙箱環境中隔離運行.
使用者在單個 Ubuntu 系統上創建和切換不同的環境。 這些環境可以是不同的發行版、不同版本的軟體包,甚至是不同的操作系統。
Schroot 通過隔離用戶空間和內核空間,實現環境之間的完全隔離,用戶工作目錄仍能直接使用,內核空間其實還是同一套。這樣的系統代價比 VIrtualBox 低多了。
需要注意的是,有一好沒有兩好。輕量的內核公用,表示不會虛擬出硬體。比如虛擬環境提供 Apache 網路服務時,就要避開主機已用的端口。
我的 筆記本目前安裝 Ubuntu 22.04。之前嘗試 24.04時發生崩潰,把Windows都破壞了。。。現在嘗試用 24.04 和 20.04兩個版本來跑 schroot,試試看是否能工作。。。
設置 schroot 的虛擬環境
工具安裝:
sudo apt install schroot debootstrap
debootstrap 可以透過網路下載安裝系統,相當方便。
schroot 的設定檔名稱是 /etc/schroot/schroot.conf,裡面有一堆已註解掉的範例。
這裏簡單的介紹自定的 u24 設定檔的內容:
#自定的虛擬環境代號以 [] 定義,到下個 [] 或設定檔結束前,都是對此環境的設置
[u24]
# 環境的簡短說明
description=Ubuntu focal
# 虛擬環境安裝的資料夾路徑
directory=/var/u24
# 允許使用這個虛擬環境的用戶清單,用逗號分隔多個用戶名稱
users=cyue,root
# 允許使用這個虛擬環境的用戶群組清單,用逗號分隔多個用戶群組名稱
groups=sbuild
# 這個虛擬環境的執行位置,可以是目錄、區塊裝置、llvm/btrfs/zfs 的 snapshot。
# 一般在一個資料夾運行環境,設成 'directory'
type=directory
添加這個 u24 虛擬環境:
# in sudoer:
cat << EOF >> /etc/schroot/schroot.conf
[u24]
description=用最新的Ubuntu 24.04 LTS環境工作
directory=/var/u24
users=cyue,root
groups=sbuild
type=directory
EOF
添加 u20 虛擬環境:
# in sudoer:
cat << EOF >> /etc/schroot/schroot.conf
[u20]
description=用最新的Ubuntu 20.04 LTS環境工作
directory=/var/u20
users=cyue,root
groups=sbuild
type=directory
EOF
下載安裝系統到設定的目錄
使用 debootstrap 從 ubuntu 臺灣服務器下載 noble 的最簡單的 buildd 變體,到設定檔指定的目錄:
U24:
sudo mkdir /var/u24
sudo debootstrap --variant=buildd --arch amd64 noble /var/u24 http://tw.archive.ubuntu.com/ubuntu/
U20:
sudo mkdir /var/u20
sudo debootstrap --variant=buildd --arch amd64 focal /var/u20 http://tw.archive.ubuntu.com/ubuntu/
使用 root 登環境安裝基礎工具
可添加 sudo命令,之後被允許的 sudoer 可以在虛擬環境中自行安裝工具。
不添加的話,只有 root能安裝,限制一般用戶在此環境提權工作。
# root 賬戶:
# 登入 u24
schroot -c u24
# 檢查系統版號
cat /etc/lsb-release
# apt 初始化
apt-get update
# 添加 sudo
apt install sudo nano
# (可選)添加其他工具
apt install vim
# 退出 root的 24.04 登入
exit
登入環境後, 命令提示的最前端會添加內含環境名的括號,以資鑑別。
在此爲 (u24)。
一般用戶 schroot 登入工作
語法: schroot -c 環境 [ -u 賬戶]
schroot -c u24
接下來的各種開發實驗環境就要自行安裝了。
添加 ATP 源
buildd 的源太少只有一行 cat /etc/atp/sources.list:
deb http://tw.archive.ubuntu.com/ubuntu [branch] main
從 host 的source源,更改 branch 名後提供,執行 sudo pico /etc/apt/sources.list :
- u24(noble) 修改:
# deb cdrom: noble main restricted
deb-src http://archive.ubuntu.com/ubuntu noble main restricted #Added by software-properties
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://tw.archive.ubuntu.com/ubuntu/ noble main restricted
deb-src http://tw.archive.ubuntu.com/ubuntu/ noble main universe restricted multiverse
## Major bug fix updates produced after the final release of the distribution.
deb http://tw.archive.ubuntu.com/ubuntu/ noble-updates main restricted
deb-src http://tw.archive.ubuntu.com/ubuntu/ noble-updates main universe restricted multiverse
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
deb http://tw.archive.ubuntu.com/ubuntu/ noble universe
# deb-src http://tw.archive.ubuntu.com/ubuntu/ noble universe
deb http://tw.archive.ubuntu.com/ubuntu/ noble-updates universe
# deb-src http://tw.archive.ubuntu.com/ubuntu/ noble-updates universe
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
deb http://tw.archive.ubuntu.com/ubuntu/ noble multiverse
# deb-src http://tw.archive.ubuntu.com/ubuntu/ noble multiverse
deb http://tw.archive.ubuntu.com/ubuntu/ noble-updates multiverse
# deb-src http://tw.archive.ubuntu.com/ubuntu/ noble-updates multiverse
## N.B. software from this repository may not have been tested as
deb http://tw.archive.ubuntu.com/ubuntu/ noble-backports main restricted universe multiverse
deb-src http://tw.archive.ubuntu.com/ubuntu/ noble-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu noble-security main restricted
deb-src http://security.ubuntu.com/ubuntu noble-security main universe restricted multiverse
deb http://security.ubuntu.com/ubuntu noble-security universe
# deb-src http://security.ubuntu.com/ubuntu noble-security universe
deb http://security.ubuntu.com/ubuntu noble-security multiverse
# deb-src http://security.ubuntu.com/ubuntu noble-security multiverse
- u20 (focal) 修改:
# deb cdrom: focal main restricted
deb-src http://archive.ubuntu.com/ubuntu focal main restricted #Added by software-properties
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://tw.archive.ubuntu.com/ubuntu/ focal main restricted
deb-src http://tw.archive.ubuntu.com/ubuntu/ focal main universe restricted multiverse
## Major bug fix updates produced after the final release of the distribution.
deb http://tw.archive.ubuntu.com/ubuntu/ focal-updates main restricted
deb-src http://tw.archive.ubuntu.com/ubuntu/ focal-updates main universe restricted multiverse
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
deb http://tw.archive.ubuntu.com/ubuntu/ focal universe
# deb-src http://tw.archive.ubuntu.com/ubuntu/ focal universe
deb http://tw.archive.ubuntu.com/ubuntu/ focal-updates universe
# deb-src http://tw.archive.ubuntu.com/ubuntu/ focal-updates universe
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
deb http://tw.archive.ubuntu.com/ubuntu/ focal multiverse
# deb-src http://tw.archive.ubuntu.com/ubuntu/ focal multiverse
deb http://tw.archive.ubuntu.com/ubuntu/ focal-updates multiverse
# deb-src http://tw.archive.ubuntu.com/ubuntu/ focal-updates multiverse
## N.B. software from this repository may not have been tested as
deb http://tw.archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://tw.archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu focal-security main restricted
deb-src http://security.ubuntu.com/ubuntu focal-security main universe restricted multiverse
deb http://security.ubuntu.com/ubuntu focal-security universe
# deb-src http://security.ubuntu.com/ubuntu focal-security universe
deb http://security.ubuntu.com/ubuntu focal-security multiverse
# deb-src http://security.ubuntu.com/ubuntu focal-security multiverse
更新:
sudo apt update;sudo apt upgrade
/dev/shm 等系統需求
參考: How-To: Mounting /dev in a chroot environment
https://wiki.archlinux.org/title/Chroot#Using_chroot
在做 AGL 開發時, bitbake 需要 存取 /dev/shm。 之前的笨方法,直接 chmod 777 /dev/shm 先。。。
後來爬文的答案:
在 chroot 環境中工作時,需要掛載一些特殊的文件系統,以便所有程式都能正常工作。
例如,grub 需要存取您的磁碟裝置……
大多數特殊文件系統可能使用以下方式掛載:
# mount fstype -t fstype /var/u24/mountpoint
如果 fstype 為 sysfs,proc 和 /dev, 則不遵循此規則。
為了鏡像 /dev 到虛擬環境,您需要實際將 /var/u24/dev 綁定到主機系統的 /dev 。
這可以通過主機系統下命令完成(到開機流程):
# mount --bind /dev /var/u24/dev
或者用縮寫參數:
# mount -B /dev /var/u24/dev
這樣,/var/u24/dev 將填充與主機的 /dev 相同的內容,這將允許您在 chroot 中執行 grub-update 和更多作。
其他
在做 AGL 開發時, bitbake 需要 存取 /dev/shm。 不知道怎麼調,chmod 777 先。。。
schroot是脫胎於 chroot,讓一般用戶可以切換虛擬用戶環境的工具,但虛擬環境與主機是共用資源的。例如,網路的端口 80 已經被主機的 Apache 用掉了,那麼虛擬環境裡的 Apache 就不能再用此端口。
我們用的 buildd 很小,需要的工具/環境還是要自行安裝。例如多國語言:
# locals
dpkg-reconfigure locales
sudo apt-get install language-pack-en
locale-gen en_US.UTF-8