nas chroot debian ipTIME NAS4dual

chroot를 설치하는 방법입니다.

chroot의 의미는 wikipedia에 아래와 같이 설명되어 있습니다.

유닉스 운영 체제에서 chroot는 현재 실행 중인 프로세스와 차일드 프로세스{.new} 그룹에서 루트 디렉터리{.mw-redirect}를 변경하는 작업이다.

이렇게 수정된 환경에서 실행되는 프로그램은 지정된 디렉터리 트리 밖의 파일들의 이름을 지정할 수 없으므로(즉, 일반적으로는 접근이 불가능하므로) chroot 감옥으로 부른다.

나스 같은 환경에서는 특정 디렉토리에 리눅스(주로 데비안) 시스템 파일을 올려두고 그 디렉토리를 루트 디렉토리로 변경하여 기본 환경에 없는 각종 프로그램을 이용하는 목적으로 많이 사용합니다.

아래는  Arm 프로세서를 사용하는 라우트(RT-AC56U, RT-AC68U, RT-AC87U, RT-AC88U, RT-AC3200, RT-AC5300…)용으로 chroot를 설치하는 방법을 nas4dual에 맞게 수정한 것입니다.

원본은 아래 링크에서 확인할 수 있습니다.

https://www.hqt.ro/how-to-install-debian-jessie-arm/

준비 작업

아래 링크를 참고하여 entware를 설치합니다.

https://isulnara.com/archives/1307

자 이제 설치에 들어갑니다.

localhost> opkg install binutils
Installing binutils (2.26.1-1) to root...
Downloading http://pkg.entware.net/binaries/armv7/binutils_2.26.1-1_armv7soft.ip                                         k.
Installing objdump (2.26.1-1) to root...
Downloading http://pkg.entware.net/binaries/armv7/objdump_2.26.1-1_armv7soft.ipk                                         .
Installing libopcodes (2.26.1-1) to root...
Downloading http://pkg.entware.net/binaries/armv7/libopcodes_2.26.1-1_armv7soft.                                         ipk.
Installing libbfd (2.26.1-1) to root...
Downloading http://pkg.entware.net/binaries/armv7/libbfd_2.26.1-1_armv7soft.ipk.
Installing libiconv-full (1.11.1-3) to root...
Downloading http://pkg.entware.net/binaries/armv7/libiconv-full_1.11.1-3_armv7so                                         ft.ipk.
Installing libintl-full (0.19.8.1-1) to root...
Downloading http://pkg.entware.net/binaries/armv7/libintl-full_0.19.8.1-1_armv7soft.ipk.
Installing ar (2.26.1-1) to root...
Downloading http://pkg.entware.net/binaries/armv7/ar_2.26.1-1_armv7soft.ipk.
Configuring libiconv-full.
Configuring libintl-full.
Configuring libbfd.
Configuring libopcodes.
Configuring objdump.
Configuring ar.
Configuring binutils.

localhost> opkg install coreutils-chroot
Installing coreutils-chroot (8.23-2) to root...
Downloading http://pkg.entware.net/binaries/armv7/coreutils-chroot_8.23-2_armv7soft.ipk.
Configuring coreutils-chroot.

localhost> cd /opt
localhost> /opt/bin/wget -c -O debian_jessie8.9-arm_clean.tgz https://nas.fft.kr/nasXdual/chroot/?download=debian_jessie8.9-arm_clean.tgz
localhost> tar -xvzf ./debian_jessie8.9-arm_clean.tgz
localhost> rm -rf debian_jessie8.9-arm_clean.tgz

아래 명령 또는 텍스터 에디터로

vi /opt/etc/init.d/S99debian

아래와 같은 내용으로 /opt/etc/init.d/S99debian 파일을 생성합니다.

#!/bin/sh
PATH=/opt/bin:/opt/sbin:/sbin:/bin:/usr/sbin:/usr/bin
# Folder with Debian Jessie
CHROOT_DIR=/opt/debian
# Some folder outside of sandbox,
# will be mounted to /mnt folder in Debian
# Uncommented next line if you need to mount a folder inside debian
EXT_DIR=/mnt/HDD1/@chroot/media/
CHROOT_SERVICES_LIST=/opt/etc/chroot-services.list
if [ ! -e "$CHROOT_SERVICES_LIST" ]; then
        echo "Please, define Debian services to start in
$CHROOT_SERVICES_LIST first!"
        echo "One service per line. Hint: this is a script names from
Debian's /etc/init.d/"
        exit 1
fi
MountedDirCount="$(mount | grep $CHROOT_DIR | wc -l)"
start() {
        if [ $MountedDirCount -gt 0 ]; then
                echo "Chroot'ed services seems to be already started,
exiting..."
                exit 1
        fi
        echo "Starting chroot'ed Debian services..."
        for dir in dev proc sys; do
                mount -o bind /$dir $CHROOT_DIR/$dir
        done
        [ -z "$EXT_DIR" ] || mount -o bind $EXT_DIR $CHROOT_DIR/mnt
        for item in $(cat $CHROOT_SERVICES_LIST); do
                chroot $CHROOT_DIR /etc/init.d/$item start
        done
        }
stop() {
        if [ $MountedDirCount -eq 0 ]; then
                echo "Chroot'ed services seems to be already stopped,
exiting..."
                exit 1
        fi
        echo "Stopping chroot'ed Debian services..."
        for item in $(cat $CHROOT_SERVICES_LIST); do
                chroot $CHROOT_DIR /etc/init.d/$item stop
                sleep 2
        done
        mount | grep $CHROOT_DIR | awk '{print $3}' | xargs umount -l
        }
restart() {
        if [ $MountedDirCount -eq 0 ]; then
                echo "Chroot'ed services seems to be already stopped"
                start
                else
                echo "Stopping chroot'ed Debian services..."
        for item in $(cat $CHROOT_SERVICES_LIST); do
                chroot $CHROOT_DIR /etc/init.d/$item stop
                sleep 2
        done
        mount | grep $CHROOT_DIR | awk '{print $3}' | xargs umount -l
                echo "Restarting chroot'ed Debian services..."
        for dir in dev proc sys; do
                mount -o bind /$dir $CHROOT_DIR/$dir
        done
        [ -z "$EXT_DIR" ] || mount -o bind $EXT_DIR $CHROOT_DIR/mnt
        for item in $(cat $CHROOT_SERVICES_LIST); do
                chroot $CHROOT_DIR /etc/init.d/$item start
        done
        fi
        }
enter() {
        [ -z "$EXT_DIR" ] || mount -o bind $EXT_DIR $CHROOT_DIR/mnt
        mount -o bind /dev/ /opt/debian/dev/
        mount -o bind /dev/pts /opt/debian/dev/pts
        mount -o bind /proc/ /opt/debian/proc/
        mount -o bind /sys/ /opt/debian/sys/
        chroot /opt/debian /bin/bash
        }
status() {
        if [ $MountedDirCount -gt 0 ]; then
                echo "Chroot'ed services running..."
        else
                echo "Chroot'ed services not running!"
        fi
        }
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                restart
                ;;
        enter)
                enter
                ;;
        status) status
                ;;
        *)
                echo "Usage: (start|stop|restart|enter|status)"
                exit 1
                ;;
esac
echo Done.
exit 0

위 내용 중 **EXT_DIR=/mnt/HDD1/@chroot/media/**는 자신의 환경에 맞게 수정합니다.

root 권한이 필요한 부분이 있어서 아래 명령으로 root 사용자로 전환하고, 아래 명령들을 내립니다.

localhost> su -

localhost> chmod 755 /opt/etc/init.d/S99debian

localhost> touch /opt/etc/chroot-services.list
localhost> ln -s /opt/etc/init.d/S99debian /opt/bin/debian

localhost> cp /etc/hosts /opt/debian/etc/
localhost> mkdir -p /mnt/HDD1/@chroot/media

위의 마지막 명령에서 /mnt/HDD1/@chroot/media의 경로는 이전 단계 /opt/etc/init.d/S99debian 파일에 있는 EXT_DIR에 설정된 값으로 수정합니다.

이제 모든 설치와 설정이 끝났습니다.

아래 명령으로 chroot 환경의 데비안으로 접속합니다.(이후부터는 su – 이후 아래 명령만 내리면 됩니다.)

localhost> debian enter
mount: mount point /opt/debian/dev/pts does not exist
root@NAS4dual:/#

프롬프트가 위와 같이 변경되면 성공입니다. (pts 관련 에러가 발생하지만 무시해도 됩니다)

아래 명령으로 각종 패키지를 최신 버전으로 업데이트합니다.

apt update && apt upgrade -y

아래 명령으로 시간을 설정합니다.

dpkg-reconfigure tzdata

아래 명령으로 패키지를 하나 설치해봅니다. (ncftp)

root@NAS4dual:/# apt-get install ncftp
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  ncftp
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 531 kB of archives.
After this operation, 1032 kB of additional disk space will be used.
Get:1 http://httpredir.debian.org/debian/ stable/main ncftp armel 2:3.2.5-1.1 [531 kB]
Fetched 531 kB in 1s (431 kB/s)
E: Can not write log (Is /dev/pts mounted?) - posix_openpt (28: No space left on device)
Selecting previously unselected package ncftp.
(Reading database ... 8963 files and directories currently installed.)
Preparing to unpack .../ncftp_2%3a3.2.5-1.1_armel.deb ...
Unpacking ncftp (2:3.2.5-1.1) ...
Setting up ncftp (2:3.2.5-1.1) ...
update-alternatives: using /usr/bin/ncftp3 to provide /usr/bin/ncftp (ncftp) in auto mode

chroot 환경을 벗어나려면 아래 명령을 내립니다.

root@NAS4dual:/# exit
exit
Done.

2019-10-09  추가

chroot 환경에 설치된 서비스 시스템 리부팅 시 자동 시작

터미널에 root 권한으로 접속하여 /opt/etc/chroot-services.list 파일에 chroot 환경에서 설치된 서비스명을 한 줄에 하나씩 추가합니다. 서비스명은 chroot 환경 기준으로 _/etc/init.d_에 있는 파일명입니다(chroot 환경이 아닌 메인 시스템 기준으로는 _/opt/debian/etc/init.d_에 있는 파일명입니다).

localhost> su -
localhost> vi /opt/etc/chroot-services.list

참고로 위 파일의 내용 샘플입니다. chroot 환경 기준으로 /etc/init.d/dnsmasq, /etc/init.d/S90-abc.sh 파일을 자동 실행시킬 경우 아래처럼 작성합니다.

dnsmasq
S90-abc.sh

이제 시스템을 재시작하면 dnsmasq와 S90-abc.sh가 자동 실행됩니다.

참고 사이트: https://www.hqt.ro/how-to-install-debian-jessie-arm/