Skip to content

Commit

Permalink
Support to switch login user
Browse files Browse the repository at this point in the history
  • Loading branch information
2moe committed Apr 23, 2021
1 parent c7ba8b2 commit 935ecaa
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .mirror/manager
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ check_tmoe_command() {
else
TMOE_TIPS_01="tmoe"
fi
TMOE_TIPS_00="Welcome to tmoe linux manager v1.4538,type ${TMOE_TIPS_01} to start it."
TMOE_TIPS_00="Welcome to tmoe linux manager v1.4539,type ${TMOE_TIPS_01} to start it."
}
#########################
tmoe_manager_env() {
Expand Down
2 changes: 1 addition & 1 deletion .mirror/tool
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ check_tmoe_command() {
else
TMOE_TIPS_01="tmoe t"
fi
TMOE_TIPS_00="Welcome to tmoe linux tool v1.4538,type ${TMOE_TIPS_01} to start this tool."
TMOE_TIPS_00="Welcome to tmoe linux tool v1.4539,type ${TMOE_TIPS_01} to start this tool."
#勿改00变量
}
#########
Expand Down
2 changes: 1 addition & 1 deletion manager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ check_tmoe_command() {
else
TMOE_TIPS_01="tmoe"
fi
TMOE_TIPS_00="Welcome to tmoe linux manager v1.4538,type ${TMOE_TIPS_01} to start it."
TMOE_TIPS_00="Welcome to tmoe linux manager v1.4539,type ${TMOE_TIPS_01} to start it."
}
#########################
tmoe_manager_env() {
Expand Down
90 changes: 73 additions & 17 deletions share/app/tmoe
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bash
#############################################
# set -x
unset LINK_SOURCE_DIR TEMPORARY_SCRIPT_FILE TEMPORARY_SCRIPT_FILE_01 TEMPORARY_SCRIPT_FILE_02 TEMPORARY_SCRIPT_FILE_03
tmoe_main() {
case "$1" in
p* | proot)
Expand Down Expand Up @@ -144,8 +145,12 @@ tmoe_main() {
bash) TMOE_SHELL='/bin/bash' ;;
z | zsh) TMOE_SHELL='/bin/zsh' ;;
ash) TMOE_SHELL='/bin/ash' ;;
ln)
CREATE_A_SOFT_LINK=true
LINK_SOURCE_DIR="/etc/profile.d/permanent"
;;
"") ;;
*/*) TEMPORARY_SCRIPT_FILE="${4}" ;;
./* | ../* | /*) TEMPORARY_SCRIPT_FILE_01="${4}" ;;
*) EXEC_PROGRAM="${4}" ;;
esac

Expand All @@ -157,10 +162,35 @@ tmoe_main() {
bash) TMOE_SHELL='/bin/bash' ;;
z | zsh) TMOE_SHELL='/bin/zsh' ;;
ash) TMOE_SHELL='/bin/ash' ;;
ln)
CREATE_A_SOFT_LINK=true
LINK_SOURCE_DIR="/etc/profile.d/permanent"
;;
"") ;;
*/*) TEMPORARY_SCRIPT_FILE="${5}" ;;
./* | ../* | /*)
case ${CREATE_A_SOFT_LINK} in
true) LINK_SOURCE_DIR="${5}" ;;
*) TEMPORARY_SCRIPT_FILE_02="${5}" ;;
esac
;;
*) EXEC_PROGRAM="${5}" ;;
esac

case "$6" in
"") ;;
./* | ../* | /*)
case ${CREATE_A_SOFT_LINK} in
true) LINK_SOURCE_DIR="${6}" ;;
*) TEMPORARY_SCRIPT_FILE_03="${6}" ;;
esac
;;
esac

if ((${#} >= 7)); then
printf "%s\n" "ERROR, number of parameters exceeded."
printf "%s\n" "Please retype the commands:"
printf "%s\n" "tmoe ${1} ${2} ${3} ${4} ${5}"
fi
TMOE_PARAMETERS="$*"
start_tmoe_container
}
Expand Down Expand Up @@ -206,11 +236,16 @@ source_tmoe_env() {
}
########
check_container_login_file_01() {
if [ -r "${TMOE_GNU_LINUX_CONTAINER_ENV_DIR}/login" ]; then
if ! grep -q 'tmp/.tmoe_container_temporary' "${TMOE_GNU_LINUX_CONTAINER_ENV_DIR}/login"; then
${TMOE_PREFIX} rm -fv "${TMOE_GNU_LINUX_CONTAINER_ENV_DIR}/login" ${DEBIAN_CHROOT}/etc/profile.d/001_login.sh
fi
fi
if [ ! -e "${TMOE_GNU_LINUX_CONTAINER_ENV_DIR}/login" ]; then
${TMOE_PREFIX} mkdir -pv ${TMOE_GNU_LINUX_CONTAINER_ENV_DIR} ${DEBIAN_CHROOT}/etc/profile.d/permanent ${DEBIAN_CHROOT}/etc/profile.d/temporary
${TMOE_PREFIX} mkdir -pv ${TMOE_GNU_LINUX_CONTAINER_ENV_DIR} ${DEBIAN_CHROOT}/etc/profile.d/permanent
${TMOE_PREFIX} cp -fv ${TMOE_SHARE_DIR}/container/profile/login ${TMOE_GNU_LINUX_CONTAINER_ENV_DIR}
${TMOE_PREFIX} cp -fv ${TMOE_SHARE_DIR}/container/profile/login ${DEBIAN_CHROOT}/etc/profile.d/001_login.sh
${TMOE_PREFIX} chmod -Rv 755 ${TMOE_GNU_LINUX_CONTAINER_ENV_DIR} ${DEBIAN_CHROOT}/etc/profile.d/001_login.sh ${DEBIAN_CHROOT}/etc/profile.d/permanent ${DEBIAN_CHROOT}/etc/profile.d/temporary
${TMOE_PREFIX} chmod -Rv 755 ${TMOE_GNU_LINUX_CONTAINER_ENV_DIR} ${DEBIAN_CHROOT}/etc/profile.d/001_login.sh ${DEBIAN_CHROOT}/etc/profile.d/permanent
fi
if [[ ! -e ${CONTAINER_ZLOGIN_FILE} ]]; then
${TMOE_PREFIX} mkdir -pv ${CONTAINER_ZSH_DIR}
Expand All @@ -222,24 +257,33 @@ check_container_login_file_01() {
}
########
chmod_777_temporary_file_01() {
cat ${TMPDIR}/.tmoe_temporary_autostartup_script
${TMOE_PREFIX} chmod 777 ${TMPDIR}/.tmoe_temporary_autostartup_script
${TMOE_PREFIX} mv -f ${TMPDIR}/.tmoe_temporary_autostartup_script "${TMOE_GNU_LINUX_CONTAINER_TEMPORARY_STARTUP_DIR}/start_01_${RANDOM}"
cat ${TEMP_FILE}
${TMOE_PREFIX} chmod 777 ${TEMP_FILE}
${TMOE_PREFIX} mv -f ${TEMP_FILE} "${TMOE_GNU_LINUX_CONTAINER_TEMPORARY_STARTUP_DIR}/"
}
set_temp_file_env() {
TEMP_FILE="${TMPDIR}/start_01_${RANDOM}"
mkdir -p ${TMOE_GNU_LINUX_CONTAINER_TEMPORARY_STARTUP_DIR} || ${TMOE_PREFIX} mkdir -pv ${TMOE_GNU_LINUX_CONTAINER_TEMPORARY_STARTUP_DIR}
${TMOE_PREFIX} chmod 777 -R ${TMOE_GNU_LINUX_CONTAINER_TEMPORARY_STARTUP_DIR}
}
create_and_move_temporary_script_01() {
printf "%s\n%s\n%s\n" '#!/usr/bin/env bash' "cd /etc/profile.d/temporary" "${EXEC_PROGRAM}" >${TMPDIR}/.tmoe_temporary_autostartup_script
set_temp_file_env
printf "%s\n%s\n%s\n" '#!/usr/bin/env bash' "cd /tmp/.tmoe_container_temporary" "${EXEC_PROGRAM}" >${TEMP_FILE}
chmod_777_temporary_file_01
}
create_and_move_temporary_script_02() {
printf "%s\n%s\n%s\n" '#!/usr/bin/env bash' "cd /etc/profile.d/temporary" "${TEMPORARY_SCRIPT_FILE}" >${TMPDIR}/.tmoe_temporary_autostartup_script
set_temp_file_env
printf "%s\n%s\n%s\n" '#!/usr/bin/env bash' "cd /tmp/.tmoe_container_temporary" "${TEMPORARY_SCRIPT_FILE}" >${TEMP_FILE}
chmod_777_temporary_file_01
}
create_and_move_temporary_script_03() {
printf "%s\n%s\n" '#!/usr/bin/env bash' "${EXEC_PROGRAM}" >${TMPDIR}/.tmoe_temporary_autostartup_script
set_temp_file_env
printf "%s\n%s\n" '#!/usr/bin/env bash' "${EXEC_PROGRAM}" >${TEMP_FILE}
chmod_777_temporary_file_01
}
create_and_move_temporary_script_04() {
printf "%s\n%s\n" '#!/usr/bin/env bash' "${TEMPORARY_SCRIPT_FILE}" >${TMPDIR}/.tmoe_temporary_autostartup_script
set_temp_file_env
printf "%s\n%s\n" '#!/usr/bin/env bash' "${TEMPORARY_SCRIPT_FILE}" >${TEMP_FILE}
chmod_777_temporary_file_01
}
start_tmoe_container() {
Expand All @@ -266,6 +310,12 @@ start_tmoe_container() {
else
sed -n p ${DEBIAN_CHROOT}/etc/issue 2>/dev/null
fi

if [[ ${CREATE_A_SOFT_LINK} = true ]]; then
ln -svf ${DEBIAN_CHROOT}${LINK_SOURCE_DIR} ./container_link_${RANDOM}
exit 0
fi

case ${TMOE_CONTAINER_HITOKOTO} in
true) [[ ! ${TMOE_LANG} =~ zh_.*UTF-8 ]] || get_hitokito_cn ;;
esac
Expand Down Expand Up @@ -309,19 +359,25 @@ start_tmoe_container() {
exit 1
fi
fi
if [[ -n ${TEMPORARY_SCRIPT_FILE} ]]; then

if [[ -n ${TEMPORARY_SCRIPT_FILE_01} || -n ${TEMPORARY_SCRIPT_FILE_02} || -n ${TEMPORARY_SCRIPT_FILE_03} ]]; then
if [[ -e ${DEBIAN_CHROOT} ]]; then
check_container_login_file_01
else
printf "%s\n" "${RED}ERROR${RESET}, please install the ${BLUE}${DEBIAN_FOLDER}${RESET} container first."
exit 1
fi
if [[ -e ${TEMPORARY_SCRIPT_FILE} ]]; then
${TMOE_PREFIX} cp -rvf "${TEMPORARY_SCRIPT_FILE}" "${TMOE_GNU_LINUX_CONTAINER_TEMPORARY_STARTUP_DIR}/"
else
create_and_move_temporary_script_02
fi
for i in ${TEMPORARY_SCRIPT_FILE_01} ${TEMPORARY_SCRIPT_FILE_02} ${TEMPORARY_SCRIPT_FILE_03}; do
if [[ -e ${i} ]]; then
${TMOE_PREFIX} cp -rvf "${i}" "${TMOE_GNU_LINUX_CONTAINER_TEMPORARY_STARTUP_DIR}/"
${TMOE_PREFIX} chmod a+x "${TMOE_GNU_LINUX_CONTAINER_TEMPORARY_STARTUP_DIR}/${i}"
else
TEMPORARY_SCRIPT_FILE="${i}"
create_and_move_temporary_script_02
fi
done
fi

case ${SYSTEMD_NSPAWN} in
true)
case $(uname -o) in
Expand Down
1 change: 1 addition & 0 deletions share/completion/_tmoe
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ function _tmoe() {
"bash:GNU Bourne-Again SHell"
"zsh:z shell(Default)"
"ash:command interpreter (shell)"
"ln:create a symbolic link"
)

_arguments -C \
Expand Down
43 changes: 37 additions & 6 deletions share/container/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Environment variables and automatic startup scripts

关于菜单内环境变量选项与自启动脚本的说明:
关于tmoe container 环境变量选项与自启动脚本的说明:
注:适用于v1.4539(2021-04-23)及更新的版本安装的proot容器。
v1.4539及更新的版本安装的chroot容器。

## Global environment variables

(全局环境变量)
注:适用于v1.4526及更新的版本安装的proot容器。

容器在启动前,会加载环境变量文件,并将其自动生成为容器的启动参数,因此修改环境变量文件理论上对大部分login shell都能生效。
选择该选项,本质上是修改容器内部的 **/usr/local/etc/tmoe-linux/environment/container.env**

Expand Down Expand Up @@ -64,6 +66,8 @@ export PATH="/go/bin:/usr/local/go/bin${PATH:+:${PATH}}"

## Temporary scripts

注:临时脚本的文件夹位于容器内部的 **/tmp/.tmoe_container_temporary**

### create

如果需要创建临时自启动脚本,那么建议您使用 `tmoe` 命令。
Expand Down Expand Up @@ -104,14 +108,15 @@ t p u 下北澤紅茶 "./hello.rb"
关于使用多参数来执行多个脚本或多命令的说明。

临时脚本可以是一个,也可以是多个。
当个数在两个以内,直接使用参数即可。
当个数在两个以内时,直接使用参数即可。
注:不支持数组形式。
当个数大于两个时,那么最后一个参数请使用文件夹。
请使用命令或脚本来调用文件夹。

tmoe对于本地文件的优先级要高于容器内部文件。
若本地存在 **~/hello.rb**,而容器内部存在同名文件,则优先检测本地文件,若本地不存在才会执行容器内部的文件。

本地的脚本或者二进制文件可以没有可执行权限,但是容器内部的文件必须要有可执行权限,除非您使用了命令去调用另一个脚本。
本地的脚本或者二进制文件可以没有可执行权限,但是容器内部的文件必须要有可执行权限,除非您使用了命令去调用另一个脚本。

假设本地存在一个文件夹,名为“My-Rust-Project”

Expand All @@ -129,11 +134,37 @@ t p u 下北澤紅茶 "ls -lah My-Rust-Project" "./My-Rust-Project"
t p u 下北澤紅茶 "cd My-Rust-Project; cargo build --release" "./My-Rust-Project"
```

由于temporary文件夹会在执行完成后自动清空,因此不适用于编译型的任务。
接下来,开发者可能会优化Permanent scripts选项。
由于temporary文件夹会在执行完成后自动清空,因此不适用于编译型的任务。
请查看Permanent scripts的说明。

## Permanent scripts & ln parameter

注:永久性脚本的文件夹位于容器内部的 **/etc/profile.d/permanent**

建议将该功能配合ln参数使用。

当ln位于最后一个参数时,后面没有接任何路径,此时将创建容器内部的 **/etc/profile.d/permanent** 的软链接到当前路径的 "container_link_随机数值"

```shell
t p u 下北澤紅茶 ln
```

将本地脚本copy到软链接里去,这时候启动容器时会自动调用 **/etc/profile.d/permanent** 里的脚本或二进制文件。

如果您需要软链接容器目录下的/tmp到当前目录,那么可以这样做。

```shell
t p u 下北澤紅茶 ln /tmp
```

假设随机数值为**114514**,那么当前目录下就会生成一个软链接文件:**container_link_114514**
该链接指向容器内部的/tmp

如需删除该文件,您可以输入`unlink container_link_114514`,或者是`rm container_link_114514`,而不要输 `rm container_link_114514/*`

## Entrypoint

选择该选项,本质上是修改容器内部的 **/usr/local/etc/tmoe-linux/environment/entrypoint**
(入口点)

本选项与Permanent scripts存在相似且重合的功能。
Expand Down
12 changes: 6 additions & 6 deletions share/container/list
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ choose_which_gnu_linux_distro() {
choose_which_gnu_linux_distro_ja() {
SELECTED_GNU_LINUX=$(whiptail --title "GNU/Linux distros" --menu "GNU/Linuxディストリビューションを選択してください" 0 50 0 \
"1" "🍭 Beta版:Weekly builds(GUI included)" \
"2" "Alpha版:A service is a container(go,rust,nodejs,openjdk)" \
"2" "Beta版:A service is a container(go,rust,nodejs,openjdk)" \
"3" "🍥 Debian は オペレーティングシステムのフリーなディストリビューションです" \
"4" "🍛 Ubuntu:開発目標は「誰にでも使いやすい最新かつ安定したOS」を提供することである" \
"5" "🌉 Arch:「シンプリシティ」、「ミニマリズム」、「エレガンス」さ、コードの正しさに焦点を当てて開発されている" \
Expand All @@ -29,7 +29,7 @@ choose_which_gnu_linux_distro() {
choose_which_gnu_linux_distro_zh() {
SELECTED_GNU_LINUX=$(whiptail --title "GNU/Linux distros" --menu "您想要安装哪个GNU/Linux发行版?" 0 50 0 \
"1" "🍭 Beta公测:Weekly builds(每周构建GUI版)" \
"2" "Alpha内测:服务即容器(go,rust,nodejs,openjdk)" \
"2" "Beta公测:服务即容器(go,rust,nodejs,openjdk)" \
"3" "🍥 Debian:致力于自由" \
"4" "🍛 Ubuntu:我的存在是因為大家的存在" \
"5" "🌉 Arch:系统设计以KISS为总体指导原则" \
Expand All @@ -45,7 +45,7 @@ choose_which_gnu_linux_distro() {
choose_which_gnu_linux_distro_en() {
SELECTED_GNU_LINUX=$(whiptail --title "GNU/Linux distros" --menu "Which distribution do you want to install?" 0 50 0 \
"1" "🍭 Beta version:Weekly builds(GUI included)" \
"2" "Alpha:A service is a container(go,rust,nodejs,openjdk)" \
"2" "Beta:A service is a container(go,rust,nodejs,openjdk)" \
"3" "🍥 Debian is a distro composed of free and open-source software" \
"4" "🍛 Ubuntu is the modern, open source OS." \
"5" "🌉 Arch:simplicity,modernity,pragmatism,user centrality and versatility" \
Expand Down Expand Up @@ -130,12 +130,12 @@ install_tmoe_moonthly_build_edition() {
choose_service_en() {
INSTALLDESKTOP=$(whiptail --title "CONTAINERS" --menu \
"Except docker, tmoe/azure server will automatically build and release the latest container every 2 weeks" 0 0 0 \
"1" "Go (Makes it easy to build simple&reliable app)" \
"1" "🐹 Go (Makes it easy to build simple&reliable app)" \
"2" "Rust(Blazingly fast and memory-efficient)" \
"3" "Visual Studio Code online server" \
"4" "OpenJDK(An open-source Java Platform)" \
"5" "Docker(Run docker via qemu)" \
"6" "Node.js(A back-end JavaScript runtime environment)" \
"5" "🐳 Docker(Run docker via qemu)" \
"6" "🟩 Node.js(A back-end JavaScript runtime environment)" \
"7" "Ruby(An interpreted,general-purpose lang)" \
"0" "🌚 back = ̄ω ̄=" \
3>&1 1>&2 2>&3)
Expand Down
2 changes: 1 addition & 1 deletion share/container/profile/export_env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mkdir -p /etc/zsh /etc/profile.d/permanent /etc/profile.d/temporary
mkdir -p /etc/zsh /etc/profile.d/permanent
touch /usr/local/etc/tmoe-linux/environment/container.env
chmod 755 /usr/local/etc/tmoe-linux/environment/container.env

Expand Down
4 changes: 2 additions & 2 deletions share/container/profile/login
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ if [ "$(ls /etc/profile.d/permanent/ 2>/dev/null)" ]; then
fi
done
fi
if [ "$(ls /etc/profile.d/temporary/ 2>/dev/null)" ]; then
for i in /etc/profile.d/temporary/*; do
if [ "$(ls /tmp/.tmoe_container_temporary/ 2>/dev/null)" ]; then
for i in /tmp/.tmoe_container_temporary/*; do
if [ -e "${i}" ]; then
chmod 4755 -R ${i}
${i}
Expand Down
Loading

0 comments on commit 935ecaa

Please sign in to comment.