第 21.7 节 Linux 兼容层——基于 Gentoo Linux

构建基本系统

# sysrc linux_enable="YES"
# sysrc kld_list+="linux linux64"
# kldload linux64
# service linux start
# sysrc dbus_enable="YES" #一般桌面已经配置
# service dbus start #一般桌面已经配置
# wget https://mirrors.ustc.edu.cn/gentoo/releases/amd64/autobuilds/20230101T164658Z/stage3-amd64-systemd-20230101T164658Z.tar.xz #该链接不固定!自己找。
# mkdir -p /compat/gentoo
# tar xpvf stage3-amd64-systemd-20230101T164658Z.tar.xz -C /compat/gentoo --numeric-owner

编辑 ee /etc/fstab,加入:

# Device        Mountpoint              FStype          Options                      Dump    Pass#
devfs           /compat/gentoo/dev      devfs           rw,late                      0       0
tmpfs           /compat/gentoo/dev/shm  tmpfs           rw,late,size=1g,mode=1777    0       0
fdescfs         /compat/gentoo/dev/fd   fdescfs         rw,late,linrdlnk             0       0
linprocfs       /compat/gentoo/proc     linprocfs       rw,late                      0       0
linsysfs        /compat/gentoo/sys      linsysfs        rw,late                      0       0
/tmp            /compat/gentoo/tmp      nullfs          rw,late                      0       0
#/home           /compat/gentoo/home     nullfs          rw,late                      0       0
# mount -al # 检查有无编写错误

编辑:

ee /compat/gentoo/etc/portage/make.conf

加入:

MAKEOPTS="-j2"
GENTOO_MIRRORS="https://mirrors.ustc.edu.cn/gentoo"
FEATURES="-ipc-sandbox -mount-sandbox -network-sandbox -pid-sandbox -xattr -sandbox -usersandbox"

进行常见配置:

# mkdir -p /compat/gentoo/etc/portage/repos.conf # 此时位于 FreeBSD!
# cp /compat/gentoo/usr/share/portage/config/repos.conf /compat/gentoo/etc/portage/repos.conf/gentoo.conf # 此时位于 FreeBSD!
# cp /etc/resolv.conf /compat/gentoo/etc/ # 此时位于 FreeBSD!复制 DNS 解析。

换源

# ee /compat/gentoo/etc/portage/repos.conf/gentoo.conf # 此处位于 FreeBSD!

sync-uri = rsync://rsync.gentoo.org/gentoo-portage 修改为 sync-uri = rsync://mirrors.tuna.tsinghua.edu.cn/gentoo-portage

# chroot /compat/gentoo /bin/bash # 此处位于 Gentoo!

获取 Gentoo ebuild 数据库快照

# emerge-webrsync

换源后操作

测试安装 screenfetch:

ykla / # emerge -v screenfetch

 * IMPORTANT: 10 news items need reading for repository 'gentoo'.
 * Use eselect news read to view new items.


These are the packages that would be merged, in order:

Calculating dependencies... done!
Dependency resolution took 2.51 s.

[ebuild  N     ] app-misc/screenfetch-3.9.1::gentoo  USE="-X -curl" 76 KiB

Total: 1 package (1 new), Size of downloads: 76 KiB


>>> Verifying ebuild manifests

>>> Emerging (1 of 1) app-misc/screenfetch-3.9.1::gentoo
>>> Downloading 'https://mirrors.ustc.edu.cn/gentoo/distfiles/cc/screenfetch-3.9.1.tar.gz'
--2023-07-11 16:38:16--  https://mirrors.ustc.edu.cn/gentoo/distfiles/cc/screenfetch-3.9.1.tar.gz
Resolving mirrors.ustc.edu.cn... 2001:da8:d800:95::110, 202.141.160.110
Connecting to mirrors.ustc.edu.cn|2001:da8:d800:95::110|:443... failed: No route to host.
Connecting to mirrors.ustc.edu.cn|202.141.160.110|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 77350 (76K) [application/octet-stream]
Saving to: ‘/var/cache/distfiles/screenfetch-3.9.1.tar.gz.__download__’

/var/cache/distfile 100%[===================>]  75.54K  --.-KB/s    in 0.05s   

2023-07-11 16:38:17 (1.48 MB/s) - ‘/var/cache/distfiles/screenfetch-3.9.1.tar.gz.__download__’ saved [77350/77350]

 * screenfetch-3.9.1.tar.gz BLAKE2B SHA512 size ;-) ...                                                   [ ok ]
>>> Unpacking source...
>>> Unpacking screenfetch-3.9.1.tar.gz to /var/tmp/portage/app-misc/screenfetch-3.9.1/work
>>> Source unpacked in /var/tmp/portage/app-misc/screenfetch-3.9.1/work
>>> Preparing source in /var/tmp/portage/app-misc/screenfetch-3.9.1/work/screenFetch-3.9.1 ...
>>> Source prepared.
>>> Configuring source in /var/tmp/portage/app-misc/screenfetch-3.9.1/work/screenFetch-3.9.1 ...
>>> Source configured.
>>> Compiling source in /var/tmp/portage/app-misc/screenfetch-3.9.1/work/screenFetch-3.9.1 ...
>>> Source compiled.
>>> Test phase [not enabled]: app-misc/screenfetch-3.9.1

>>> Install app-misc/screenfetch-3.9.1 into /var/tmp/portage/app-misc/screenfetch-3.9.1/image
>>> Completed installing app-misc/screenfetch-3.9.1 into /var/tmp/portage/app-misc/screenfetch-3.9.1/image

 * Final size of build directory: 166 KiB
 * Final size of installed tree:    5 KiB


>>> Installing (1 of 1) app-misc/screenfetch-3.9.1::gentoo
 * checking 4 files for package collisions
>>> Merging app-misc/screenfetch-3.9.1 to /
--- /usr/
--- /usr/share/
--- /usr/share/doc/
>>> /usr/share/doc/screenfetch-3.9.1/
>>> /usr/share/doc/screenfetch-3.9.1/CHANGELOG.bz2
>>> /usr/share/doc/screenfetch-3.9.1/README.mkdn.bz2
>>> /usr/share/doc/screenfetch-3.9.1/TODO.bz2
--- /usr/bin/
>>> /usr/bin/screenfetch
>>> app-misc/screenfetch-3.9.1 merged.

>>> Recording app-misc/screenfetch in "world" favorites file...

>>> Completed (1 of 1) app-misc/screenfetch-3.9.1::gentoo

 * GNU info directory index is up-to-date.

 * IMPORTANT: 10 news items need reading for repository 'gentoo'.
 * Use eselect news read to view new items.

可以正常安装软件。

ykla / # screenfetch
grep: warning: stray \ before "
grep: warning: stray \ before "
         -/oyddmdhs+:.                ykla@ykla
     -odNMMMMMMMMNNmhy+-`             OS: Gentoo 
   -yNMMMMMMMMMMMNNNmmdhy+-           Kernel: x86_64 Linux 6.2.10
 `omMMMMMMMMMMMMNmdmmmmddhhy/`        Uptime: 1h 26m
 omMMMMMMMMMMMNhhyyyohmdddhhhdo`      Packages: 295
.ydMMMMMMMMMMdhs++so/smdddhhhhdm+`    Shell: bash 5.1.16
 oyhdmNMMMMMMMNdyooydmddddhhhhyhNd.   Resolution: No X Server
  :oyhhdNNMMMMMMMNNNmmdddhhhhhyymMh   WM: Not Found
    .:+sydNMMMMMNNNmmmdddhhhhhhmMmy   Disk: 8.9G / 45G (20%)
       /mMMMMMMNNNmmmdddhhhhhmMNhs:   CPU: Intel Core i5-3230M @ 2x 2.594GHz
    `oNMMMMMMMNNNmmmddddhhdmMNhs+`    RAM: 3209MiB / 4038MiB
  `sNMMMMMMMMNNNmmmdddddmNMmhs/.     
 /NMMMMMMMMNNNNmmmdddmNMNdso:`       
+MMMMMMMNNNNNmmmmdmNMNdso/-          
yMMNNNNNNNmmmmmNNMmhs+/-`            
/hMMNNNNNNNNMNdhs++/-`               
`/ohdmmddhys+++/:.`                  
  `-//////:--.                       
ykla / # 

shell 脚本

脚本内容:

#/bin/sh

rootdir=/compat/gentoo
fetch https://mirrors.ustc.edu.cn/gentoo/releases/amd64/autobuilds/latest-stage3-amd64-systemd.txt
gentoodownload=$(echo |sed -n '3p'  latest-stage3-amd64-systemd.txt|awk -F ' ' '{print $1}')
rm latest-stage3-amd64-systemd.txt


url="https://mirrors.ustc.edu.cn/gentoo/releases/amd64/autobuilds/"

echo "Begin to install latest Gentoo Linux ..."
echo "check modules ..."

# check linux module
if [ "$(sysrc -n linux_enable)" = "NO" ]; then
        echo "linux module should be loaded. Continue?(Y|n)"
        read answer
        case $answer in
                [Nn][Oo]|[Nn])
                        echo "linux module not loaded"
                        exit 1
                        ;;
                [Yy][Ee][Ss]|[Yy]|"")
                        sysrc linux_enable=YES
                        ;;
        esac
fi
echo "start linux"
service linux start

# check dbus
if ! /usr/bin/which -s dbus-daemon;then
        echo "dbus-daemon not found. install it [Y|n]"
        read  answer
        case $answer in
            [Nn][Oo]|[Nn])
                echo "dbus not installed"
                exit 2
                ;;
            [Yy][Ee][Ss]|[Yy]|"")
                pkg install -y dbus
                ;;
        esac
    fi

if [ "$(sysrc -n dbus_enable)" != "YES" ]; then
        echo "dbus should be enable. Continue?(Y|n)"
        read answer
        case $answer in
            [Nn][Oo]|[Nn])
                        echo "dbus not running"
                        exit 2
                        ;;
            [Yy][Ee][Ss]|[Yy]|"")
                        sysrc dbus_enable=YES
                        ;;
        esac
fi
echo "start dbus"
service dbus start

echo "now we will bootstrap gentoo"

fetch ${url}/$gentoodownload
mkdir -p ${rootdir}
tar zxvf stage3-amd64-systemd*.tar.xz -C ${rootdir} --numeric-owner
rm stage3-amd64-systemd*.tar.xz

if [ ! "$(sysrc -f /boot/loader.conf -qn nullfs_load)" = "YES" ]; then
        echo "nullfs_load should load. continue? (Y|n)"
        read answer
        case $answer in
            [Nn][Oo]|[Nn])
                echo "nullfs not load"
		exit 3
                ;;
            [Yy][Ee][Ss]|[Yy]|"")
                sysrc -f /boot/loader.conf nullfs_load=yes
                ;;
        esac
fi

if ! kldstat -n nullfs >/dev/null 2>&1;then
        echo "load nullfs module"
        kldload -v nullfs
fi

echo "mount some fs for linux"
echo "devfs ${rootdir}/dev devfs rw,late 0 0" >> /etc/fstab
echo "tmpfs ${rootdir}/dev/shm tmpfs rw,late,size=1g,mode=1777 0 0" >> /etc/fstab
echo "fdescfs ${rootdir}/dev/fd fdescfs rw,late,linrdlnk 0 0" >> /etc/fstab
echo "linprocfs ${rootdir}/proc linprocfs rw,late 0 0" >> /etc/fstab
echo "linsysfs ${rootdir}/sys linsysfs rw,late 0 0" >> /etc/fstab
echo "/tmp ${rootdir}/tmp nullfs rw,late 0 0" >> /etc/fstab
#echo "/home ${rootdir}/home nullfs rw,late 0 0" >> /etc/fstab
mount -al

echo "for Gentoo Linux, we should change 'compat.linux.osrelease' to upgrade Linux kernel version, continue? (Y|n)"
read answer
case $answer in
	[Nn][Oo]|[Nn])
		echo "close to success"
		exit 4
		;;
	[Yy][Ee][Ss]|[Yy]|"")
		echo "compat.linux.osrelease=6.2.10" >> /etc/sysctl.conf
		sysctl compat.linux.osrelease=6.2.10
                ;;
esac
echo "complete!"
echo "to use: chroot ${rootdir} /bin/bash"
echo ""
echo " I will set resolv.conf to ali dns"
echo "continue?[Y|n]"
read answer
case $answer in
	[Nn][Oo]|[Nn])
		echo "set your gentoo by yourself.bye!"
		exit 0
		;;
	[Yy][Ee][Ss]|[Yy]|"")
		echo "nameserver 223.5.5.5" >> ${rootdir}/etc/resolv.conf
    


echo "Now write MAKEOPTS FEATURES in /compat/gentoo/etc/portage/make.conf -- using USTC mirrors for GENTOO_MIRRORS"		
echo "MAKEOPTS=\"-j2\"" >> /${rootdir}/etc/portage/make.conf
echo "GENTOO_MIRRORS=\"https://mirrors.ustc.edu.cn/gentoo\"" >> ${rootdir}/etc/portage/make.conf
echo "FEATURES=\"-ipc-sandbox -mount-sandbox -network-sandbox -pid-sandbox -xattr -sandbox -usersandbox\"" >> ${rootdir}/etc/portage/make.conf

echo "Now setting soft sources --Using TUNA mirror for emerge-webrsync"	
mkdir -p ${rootdir}/etc/portage/repos.conf
cp ${rootdir}/usr/share/portage/config/repos.conf ${rootdir}/etc/portage/repos.conf/gentoo.conf 
sed -i "" 's/rsync.gentoo.org/mirrors.tuna.tsinghua.edu.cn/' ${rootdir}/etc/portage/repos.conf/gentoo.conf

echo " I will run emerge-webrsync"   
chroot ${rootdir} /bin/bash -c "emerge-webrsync"
    
    echo "all done."
    echo "Now you can run '#chroot /compat/gentoo/ /bin/bash' Into gentoo"

                ;;
esac

最后更新于

FreeBSD 中文社区 2024