-
Notifications
You must be signed in to change notification settings - Fork 2
/
fun_plug
executable file
·118 lines (93 loc) · 3.88 KB
/
fun_plug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/sh -ex
# Enable telnet daemon for debugging only (1 or 0)
TELNETD=0
# Boot on USB Key (1 or 0)
USB_BOOT=1
DISTRIB=squeeze-armel
HD_MNT=/mnt/HD/HD_a2
USB_MNT=/mnt/USB/HD_c1
# Tar file with debian bootstrap
SOURCE=$HD_MNT/$DISTRIB.tar
# Directory where decompress debian
[ "$USB_BOOT" = "1" ] && DEST=$USB_MNT || DEST=$HD_MNT
# Final chroot directory (tar file should have $DISTRIB directory)
CHROOT=$DEST/$DISTRIB
# usbmount script to be patched (1 or 0)
PATCH_USBCHMOD=1
CONFIG_DIR=/usr/local/config
RCINIT_FILE=$CONFIG_DIR/rc.init.sh
if [ -z "$LOGGING" ]; then
# write a log, in case sth goes wrong
LOGGING=1 /bin/sh -e "$0" "$@" > /mnt/HD/HD_a2/ffp.log 2>&1
exit 0
fi
echo "`date '+[%d/%m/%Y %R]'` ------ Stating DNS-320 ------"
# switch to safe working directory on ramdisk
cd /
if [ "$TELNETD" = "1" ]; then
echo "Starting telnet daemon..."
utelnetd -l /bin/sh -d
fi
if [ "$PATCH_USBCHMOD" = "1" ]; then
echo "Patching USB mount..."
cat <<EOF > $CONFIG_DIR/chmod
#!/bin/sh
{ echo chmod \$@; echo From PID \$PPID: ; ps | egrep "^ \$PPID[ ]"; } >> $CONFIG_DIR/chmod.log
[[ "\$*" == "777 -R $USB_MNT" ]] && echo Ignored by $CONFIG_DIR/chmod && echo Ignored >> $CONFIG_DIR/chmod.log && exit
/bin/busybox chmod \$@
EOF
chmod a+x $CONFIG_DIR/chmod
grep "$CONFIG_DIR/chmod" $RCINIT_FILE \
&& echo "$RCINIT_FILE already patched" \
|| echo "[ -x $CONFIG_DIR/chmod ] && ln -nfs $CONFIG_DIR/chmod /bin/chmod > $CONFIG_DIR/chmod.log" >> $RCINIT_FILE
# cat $RCINIT_FILE
# cat $CONFIG_DIR/chmod
# cat $CONFIG_DIR/chmod.log || true
echo "USB mount patched..."
else
echo "Restore backup from /usr/local/modules/default/rc.init.sh"
cp -pf /usr/local/modules/default/rc.init.sh $CONFIG_DIR
[ -f $CONFIG_DIR/chmod ] && echo "Remove $CONFIG_DIR/chmod" && rm $CONFIG_DIR/chmod
fi
if [ "$USB_BOOT" = "1" ]; then
echo "Waiting USB Key $USB_MNT to be mounted"
local i=1; until df | grep $USB_MNT || [[ $i -gt 30 ]] ; do i=$((i*2)); echo sleep $i; sleep $i; done
df | grep $USB_MNT > /dev/null && echo "USB Key $USB_MNT mounted" || (echo "Failed to mount USB Key $USB_MNT" && exit)
# ls -la $USB_MNT
fi
# Remove to reinstall
[ -f $CHROOT.reinstall ] && echo "Remove $CHROOT to reinstall" && rm -rf $CHROOT $CHROOT.reinstall
if [ ! -d $CHROOT ]; then
echo "Installing on $DEST..."
tar xf $SOURCE -C $DEST
$CHROOT/usr/sbin/chroot $CHROOT bash -c 'echo "root:dlink" | chpasswd'
# ls -la $CHROOT
fi
# Correction devices/droits/groupes/repertoires
[ ! -e /dev/random ] && mknod -m 0444 /dev/random c 1 8
[ ! -e /dev/ptmx ] && mknod -m 0666 /dev/ptmx c 5 2
mkdir -p /var/run /var/log /dev/pts
chmod 0666 /dev/tty /dev/null ; chmod 1777 /tmp
! grep -wq utmp /etc/group && echo 'utmp:!:22:' >> /etc/group
! mount | grep -wq devpts && mount -t devpts devpts /dev/pts
touch /var/run/utmp /var/log/btmp /var/log/lastlog /var/log/wtmp
# Mount repertoires systemes dans environement chroote
mkdir -p $CHROOT/mnt/root $CHROOT/mnt/root/usr/local/config $CHROOT/mnt/root/usr/local/modules
mount -n --bind / $CHROOT/mnt/root
mount -n --bind /usr/local/config $CHROOT/mnt/root/usr/local/config
mount -n --bind /usr/local/modules $CHROOT/mnt/root/usr/local/modules
mount -n --bind /dev $CHROOT/dev
mount -n --bind /sys $CHROOT/sys
mount -n --bind /proc $CHROOT/proc
for DISK in `mount|awk '$3~"/mnt/"{print $3}'`; do
if [ -d $DISK ] ; then mkdir -p $CHROOT/$DISK ; mount -n --bind $DISK $CHROOT/$DISK ; fi
done
# Copie parametres specifiques du NAS dans environnement chroote
cp -f /proc/mounts $CHROOT/etc/mtab ; rm -f $CHROOT/etc/fstab
touch $CHROOT/etc/fstab ; cp -f /etc/resolv.conf $CHROOT/etc
hostname > $CHROOT/etc/hostname ; cp -f /etc/hosts $CHROOT/etc
# Lancement environnement chroote
echo "Starting on: $CHROOT"
# chmod -R 600 $CHROOT/etc/ssh/ $CHROOT/var/run/sshd
$CHROOT/usr/sbin/chroot $CHROOT service ssh start
echo "`date '+[%d/%m/%Y %R]'` ------ DNS-320 started ------"