第 21.8 节 Linux 兼容层——基于 Rocky Linux

待补充:目前 FreeBSD 官方已有相关 Ports。

脚本内容如下:

#!/bin/sh

ROOT_DIR=/compat
DIST=rocky
DIST_FULLNAME="Rocky Linux"
VER=9
SUB_VER=2
TIME_VER="latest"
SORT=Base
FILE=Rocky-${VER}-Container-${SORT}.${TIME_VER}.x86_64.tar.xz
SUBDIR=""
URL=https://mirrors.ustc.edu.cn/${DIST}/${VER}.${SUB_VER}/images/x86_64
UPDATE_CMD="dnf makecache"
UPGRADE_CMD="dnf upgrade -y"
INSTALL_CMD="dnf install -y"
UPDATE=1
UPGRADE=1
INSTALL=1


echo "Starting to install ${DIST_FULLNAME}"
sleep 0.5


# check.sh
echo "Checking relative modules"

if [ ! "$(sysrc -n linux_enable)" = "YES" ]; then
	echo "linux service is not enabled. Enable it now?(Y|n)"
	read ANSWER
	case ${ANSWER} in
		[Nn][Oo]|[Nn])
			echo "Warning: You MUST start linux service with \"service linux start\" EACH TIME FREEBSD IS REBOOTED."
			echo "Sure to continue without enabling linux service?(y|N)"
			read ANSWER
			case ${ANSWER} in
				[Yy][Ee][Ss]|[Yy])
					echo "WARNING: linux moudle not enabled"
					;;
				[Nn][Oo]|[Nn]|"")
					echo "Enabling linux moudle"
					sysrc linux_enable=YES
					;;
				*)
					echo "Aborting."
					exit 4
					;;
			esac
			;;
		[Yy][Ee][Ss]|[Yy]|"")
			echo "Enabling linux moudle"
			sysrc linux_enable=YES
			;;
		*)
			echo "Aborting."
			exit 4
			;;
	esac
fi

echo "Starting linux service"
service linux start

if ! which -s dbus-daemon; then
	echo "dbus-daemon not found. Install dbus now?(Y|n)"
	read ANSWER
	case ${ANSWER} in
		[Nn][Oo]|[Nn])
			echo "Abort. dbus not installed"
			exit 2
			;;
		[Yy][Ee][Ss]|[Yy]|"")
			echo "Installing dbus"
			pkg install -y dbus
			;;
		*)
			echo "Aborting."
			exit 4
			;;
	esac
fi

if [ ! "$(sysrc -n dbus_enable)" = "YES" ]; then
	echo "dbus is not enabled. Enable it now?(Y|n)"
	read ANSWER
	case ${ANSWER} in
		[Nn][Oo]|[Nn])
			echo "WARNING: You MUST start dbus with \"service dbus start\" EACH TIME FREEBSD IS REBOOTED."
			echo "Sure to continue without enabling dbus?(y|N)"
			read ANSWER
			case ${ANSWER} in
				[Yy][Ee][Ss]|[Yy])
					echo "Warning: dbus service not enabled"
					;;
				[Nn][Oo]|[Nn]|"")
					echo "Enabling dbus service"
					sysrc dbus_enable="YES"
					;;
				*)
					echo "Aborting."
					exit 4
					;;
			esac
			;;
		[Yy][Ee][Ss]|[Yy]|"")
			echo "Enabling dbus service"
			sysrc dbus_enable="YES"
			;;
		*)
			echo "Aborting."
			exit 4
			;;
	esac
fi

if [ -z "$(ps aux | grep -p dbus | grep -v grep)" ]; then
	echo "Starting dbus service"
	service dbus start
fi

if [ ! "$(sysrc -f /boot/loader.conf -qn nullfs_load)" = "YES" ]; then
	echo "nullfs should be loaded when boot. Set nullfs_load to YES?(Y|n)"
	read ANSWER
	case ANSWER in
		[Nn][Oo]|[Nn])
			echo "Warning: You MUST load nullfs with \"kldload -v nullfs\" EACH TIME FREEBSD IS REBOOTED."
			echo "Sure to continue without setting nullfs_load to YES?(y|N)"
			read ANSWER
			case ANSWER in
				[Yy][Ee][Ss]|[Yy])
					echo "Warning: nullfs_load not set"
					;;
				[Nn][Oo]|[Nn]|"")
					echo "Setting nullfs_load to YES"
					sysrc -f /boot/loader.conf nullfs_load="YES"
					;;
				*)
					echo "Aborting."
					exit 4
					;;
			esac
			;;
		[Yy][Ee][Ss]|[Yy]|"")
			echo "Setting nullfs_load to YES"
			sysrc -f /boot/loader.conf nullfs_load="YES"
			;;
		*)
			echo "Aborting."
			exit 4
			;;
	esac
fi

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


# pre_extract.sh


echo "${DIST_FULLNAME} will be installed in ${ROOT_DIR}/${DIST}"

echo "Downloading basic system"
fetch ${URL}/${FILE}

echo "Exacting basic system"
sleep 0.5
mkdir -p ${ROOT_DIR}/${DIST}
tar xvpf ${FILE} ${SUBDIR:-} -C ${ROOT_DIR}/${DIST} --numeric-owner

echo "Mounting recuired file system"
sleep 0.5
echo "devfs	${ROOT_DIR}/${DIST}/dev	devfs	rw,late	0	0" >> /etc/fstab
echo "tmpfs	${ROOT_DIR}/${DIST}/dev/shm	tmpfs	rw,late,size=1g,mode=1777	0	0" >> /etc/fstab
echo "fdescfs	${ROOT_DIR}/${DIST}/dev/fd	fdescfs	rw,late,linrdlnk	0	0" >> /etc/fstab
echo "linprocfs	${ROOT_DIR}/${DIST}/proc	linprocfs	rw,late	0	0" >> /etc/fstab
echo "linsysfs	${ROOT_DIR}/${DIST}/sys	linsysfs	rw,late	0	0" >> /etc/fstab
echo "/tmp	${ROOT_DIR}/${DIST}/tmp	nullfs	rw,late	0	0" >> /etc/fstab
#echo "/home	${ROOT_DIR}/${DIST}/home	nullfs	rw,late	0	0" >> /etc/fstab
mount -al


# setup.sh
echo "Should ${DIST_FULLNAME} use Alibaba DNS or local resolv.conf?( (A)li | (l)ocal | (c)ancel )"
read ANSWER
case ${ANSWER} in
	[Aa][Ll][Ii]|[Aa]|"")
		echo "Setting Alibaba DNS"
		echo "nameserver 223.5.5.5" >> ${ROOT_DIR}/${DIST}/etc/resolv.conf
		DNS_CONFIGURED=1
		;;
	[Ll][Oo][Cc][Aa][Ll]|[Ll])
		echo "Using local reslov.conf"
		cp /etc/resolv.conf ${ROOT_DIR}/${DIST}/etc/resolv.conf
		DNS_CONFIGURED=1
		;;
	*)
		echo "Cancled."
		echo "You have to edit ${ROOT_DIR}/${DIST}/etc/resolv.conf by yourself!"
		DNS_CONFIGURED=0
		;;
esac

echo "Do you want to use ustc source for ${DIST_FULLNAME}?(Y|n)"
read ANSWER
case ${ANSWER} in
	[Yy][Ee][Ss]|[Yy]|"")
		echo "Setting ustc source"
		chroot ${ROOT_DIR}/${DIST} /bin/bash -c "sed \
			-e 's|^mirrorlist=|#mirrorlist=|g' \
			-e 's|^#baseurl=http://dl.rockylinux.org/\$contentdir|baseurl=https://mirrors.ustc.edu.cn/rocky|g' \	
			-i.bak \
			/etc/yum.repos.d/rocky-extras.repo \
			/etc/yum.repos.d/rocky.repo"
		;;
	[Nn][Oo]|[Nn])
		echo "Will not set ustc source. Skipping upgrade and software installation in case of unexpected time consumption."
		UPDATE=0
		UPGRADE=0
		INSTALL=0
		;;
	*)
		echo "Aborting."
		exit 4
		;;
esac

if [ ${DNS_CONFIGURED} = 1 ]; then
	if [ ${UPDATE} = 1 ]; then
		echo "Updating ${DIST_FULLNAME}"
		chroot ${ROOT_DIR}/${DIST} /bin/bash -c "${UPDATE_CMD}"
	fi
	if [ ${UPGRADE} = 1 ]; then
		echo "Upgrading ${DIST_FULLNAME}"
		chroot ${ROOT_DIR}/${DIST} /bin/bash -c "${UPGRADE_CMD}"
	fi
else
	echo "DNS is not set, skipping upgrade"
fi

if [ ${DNS_CONFIGURED} = 1 ]; then
	if [ ${INSTALL} = 1 ]; then
		echo "Installing vim"
		chroot ${ROOT_DIR}/${DIST} /bin/bash -c "${INSTALL_CMD} vim"
	fi
else
	echo "DNS is not set, skipping installation"
fi


echo "Cleaning up"
rm ${FILE}

echo "Done."
echo "Now you can switch to ${DIST_FULLNAME} with \"chroot ${ROOT_DIR}/${DIST} /bin/bash\""

最后更新于

FreeBSD 中文社区 2024