# linuxjailonbsd 無聊測試在 FreeBSD 安裝 Debian Buster ~~Jail~~ Chroot 的成功或失敗筆記 * [隨手紀錄版本](https://hackmd.io/@holishing/HJfp1mhKS) * [穩定(ㄐㄧㄡˋ)版](https://github.com/holishing/linuxjailonbsd) ## 參考資料 [Setting up a (Debian) Linux jail on FreeBSD](https://forums.freebsd.org/threads/68434) ## 動機? * Debian GNU/kFreeBSD almost被放生 (開發進度追不上實際需求) * Linux Binary 相容層雖然無法相容到最新格式的 Linux binary,但自 FreeBSD 12.2 後能支援到 GNU/Linux 3.2.0 格式 ( 13-CURRENT: 3.10.0 ) 的 binary (可能部份 syscall 還無法完整支援),因此想測試這功能能支援到的極限在哪邊 ## 預計跟參考資料不一樣的地方 * 直接上 Debian + `openrc` 等套件 (不刻意拔 `systemd`,~~反正也用不到~~) * 整理 `apt update` 遇到 mmap 問題的 workaround * 整理用以上方法會遇到的疑點 * 嘗試整理出相關自動化腳本或是可以匯進 iocage * https://www.ixsystems.com/community/resources/fn11-2-iocage-jails-plex-tautulli-sonarr-radarr-lidarr-jackett-transmission-organizr.58/ ## 步驟 ### 檔案系統建立 * 檔案系統選擇: * ZFS -> 建立一般 jail 或搭配其他管理工具,如:`iocage`...等 * 選定 jail 所在 root 目錄 ### 建立 rootfs #### 方法 1. `debootstrap` * `pkg install debootstrap` * `debootstrap --foreign --arch=amd64 buster /zroot/iocage/jail/debian10 http://debian.ccns.ncku.edu.tw/debian/` (自行改成你喜歡的 Debian 鏡像站,可以加上 `--include="sysv-rc locales locales-all"` 等套件) * 建立預計掛載的 dev, proc, sys 目錄,掛上去 * chroot 進去 debian root,利用 dpkg 把設定跑完: ``` dpkg --force-depends -i /var/cache/apt/archives/*.deb ``` #### 方法 2. 匯入已構建好的 rootfs * 來源: * `docker export $container_id > rootfs.tar` (on linux environment) * `tar -xvf rootfs.tar -C /zroot/iocate/jail/debian10/root/` (in FreeBSD jail host) * 設定 jail 後,把 Debian jail 打開 ### 使套件管理程式正常運作 * 修改 apt.conf 設定,設定 APT::Cache-Start 上限 ( 自行創立設定檔: `/etc/apt/apt.conf.d/99aptcache` ), 使 `apt update` 可以繞過 mmap 相關問題 ``` APT::Cache-Start 1000000000; ``` * 修改 `/etc/apt/source.list` ,加入 `stretch-backports`,使 Debian Stretch 可以測試使用較新的部份套件 (not updated) * `kldload pty` 使 Debian 上面的 sshd 可以被連線 ## 問題 * 為何非得要用 `pty` 這個模組才能讓 Debian 的 openssh/dropbear 登入成功後開啟 shell? * 一些 `/dev` 上面的東西 (例如 share memory) 在 Debian jail 仍無法設定正常使用,影響到 PttBBS, (OpenSSH server, DropBear 可能有但相對問題較少),是 FreeBSD jail 的安全機制? 有方法可以設定? (to be checked) ## PttBBS 編譯測試 * ~~編譯 [ptt/pttbbs](https://github.com/ptt/pttbbs) 這個開源專案會找不到 `explicit_bzero()` 相關函式,可能少裝哪些套件或是少設定某些參數?~~ * Update: FreeBSD 12.2 已經可以在純 chroot 環境幾乎完整編譯 PttBBS 並在 `bbsadm` 權限使用 `/home/bbs/bin/mbbsd -D -e utf8 -h 127.0.0.1 -u new` 執行 non-daemon 模式完整功能 ### 某一個可以編譯執行 PttBBS 的 Debian Jail: - PttBBS 版本 ``` bbs@jailhost:~/pttbbs$ git log --oneline fb827331 (HEAD -> master) Makefiles: let libraries in common/fbs be compiled optionally 829b9d8b (origin/master, origin/HEAD) mbbsd: UI fixes on email verification 4d2b3185 mbbsd: Remove the old reg code logics 6bdd3689 mbbsd: Warn admin when changing email to those which would be rejected b50d0274 mbbsd: Fix not deleting email in userec_t when deleting it from verifydb c9f7a031 mbbsd: Remove extra space between options 446c2bc3 Use a good srandom seed 6c792c1a Use a good random source for captcha codes 7296640e Fix /recover not working in shell login c5eff6a6 mbbsd: Do CAPTCHA test before going into account recovery 4696318d sample: Add password changed notice email template c98a8cd3 mbbsd: Audit log for password reset 689313fb sample: Add recover account email template 89bf7422 mbbsd: Implement account recovery ``` * Jail Config: (not updated) ```conf debian { host.hostname = debian.local.test; # Hostname interface = alc0; ip4.addr = *****; # IP address of the jail ip6.addr = *****; path = "/compat/debian"; # Path to the jail exec.start = "/etc/init.d/rc 3"; # Start command exec.stop = "/etc/init.d/rc 0"; # Stop command persist; devfs_ruleset = "4"; # devfs ruleset mount.devfs; # Mount devfs inside the jail mount.fstab = /etc/fstab.debian; allow.sysvipc; # allow.raw_sockets; # allow.mount; # allow.mount.devfs; ```