about summary refs log tree commit diff stats
path: root/core/scripts/setup-core.sh
blob: 8a85d4a7aed7bd2aac63e72eca626e65a3f422ad (plain) (blame)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash

# Absolute path to this script,
# e.g. /home/user/c9-doc/core/scripts/foo.sh
SCRIPT=$(readlink -f "$0")
# Absolute path this script is in,
# e.g. /home/user/c9-doc/core/scripts
SCRIPTPATH=$(dirname "$SCRIPT")
# Absolute path to doc section,
# e.g /home/user/c9-doc/core
DIR_DATA=$(dirname "$SCRIPTPATH");

CHROOT="/mnt"
DATA_CNF="${DIR_DATA}/conf"
ADMIN_USER="c9admin"

ConfirmOrExit()
{
    while true
    do
	echo -n "Please confirm (y or n) :"
	read CONFIRM
	case $CONFIRM in
	    y|Y|YES|yes|Yes) break ;;
	    n|N|no|NO|No)
		echo "Aborting - you entered $CONFIRM"
		exit
		;;
	    *) echo "Please enter only y or n"
	esac
    done
    echo "You entered $CONFIRM. Continuing ..."
}

setup_core() {

    echo "1.1. Copying configuration data;\n"
    if [ -f "${DATA_CNF}/etc.tar.gz" ]; then
	echo "1.1. Extracting configuration data;\n"
	tar --xattrs -xpvf $DATA_CNF/etc.tar.gz --directory=$CHROOT/etc
	tar --xattrs -xpvf $DATA_CNF/usr_etc.tar.gz --directory=$CHROOT/usr/etc
    else
	echo "1.1.7. dns resolver, copy resolv.conf;\n"
	cp /etc/resolv.conf $CHROOT/etc

	echo "1.1.9. Install Skeletons\n"
	cp -r $DATA_CNF/skel $CHROOT/etc/

	cp $DATA_CNF/sudoers $CHROOT/etc/

	echo "1.2.1. Set hostname and hosts;"
	cp $DATA_CNF/hosts $CHROOT/etc/

	echo "1.2.5. File system table;"
	cp $DATA_CNF/fstab $CHROOT/etc/
	chroot $CHROOT /bin/bash -c "mount >> /etc/fstab"

	echo "1.2.6. Initialization Scripts;"
	cp $DATA_CNF/rc.d/* $CHROOT/etc/rc.d/
	cp $DATA_CNF/rc.conf $CHROOT/etc/

	echo "1.3 Install Ports;"
	rm -fr $CHROOT/etc/ports
	cp -r $DATA_CNF/ports $CHROOT/etc/ports
	cp $DATA_CNF/ports.conf $CHROOT/etc/ports.conf

	echo "1.3.3 Configure pkgmk;"
	cp $DATA_CNF/pkgmk.conf $CHROOT/etc/pkgmk.conf
	cp $DATA_CNF/pkgmk.conf.harden $CHROOT/etc/pkgmk.conf.harden

	echo "1.3.4 Configure prt-get;"
	cp $DATA_CNF/prt-get.conf $CHROOT/etc/

    fi

    echo "1.2.2. Set timezone;"
    chroot $CHROOT /bin/bash -c tzselect

    echo "1.2.3. Set locale;"
    chroot $CHROOT /bin/bash -c "localedef -i en_US -f UTF-8 en_US.UTF-8"
}

setup_users(){
    echo "1.2.4.2. Create Administrator $ADMIN_USER;"

    chroot $CHROOT /usr/bin/env -i \
	HOME=/root TERM="$TERM" PS1='\u:\w\$ ' \
	PATH=/bin:/usr/bin:/sbin:/usr/sbin \
	/bin/bash -c "useradd -U -m -k /etc/skel -s /bin/bash $ADMIN_USER"

    echo "1.2.4.3. Add Administrator $ADMIN_USER to wheel group;"
    chroot $CHROOT /bin/bash -c "usermod -a -G wheel $ADMIN_USER"
    echo "1.2.4.3. Uncomment to allow members of group wheel to execute any command\n
    #    %wheel ALL=(ALL) ALL"

    echo "1.3.1. Ports Layout;"

    chroot --userspec=pkgmk:pkgmk $CHROOT /bin/bash -c "mkdir /usr/ports/{work,distfiles,packages,work,pkgbuild}"

    echo "1.3.2. Build as unprivileged user;"


    chroot $CHROOT /usr/bin/env -i \
	HOME=/root TERM="$TERM" PS1='\u:\w\$ ' \
	PATH=/bin:/usr/bin:/sbin:/usr/sbin \
	/bin/bash -c "useradd -U -M -d /usr/ports -s /bin/false pkgmk"

    chroot $CHROOT /usr/bin/env -i \
	HOME=/root TERM="$TERM" PS1='\u:\w\$ ' \
	PATH=/bin:/usr/bin:/sbin:/usr/sbin \
	/bin/bash -c "usermod -a -G pkgmk $ADMIN_USER"

    chroot $CHROOT /usr/bin/env -i \
	HOME=/root TERM="$TERM" PS1='\u:\w\$ ' \
	PATH=/bin:/usr/bin:/sbin:/usr/sbin \
	/bin/bash -c "chown pkgmk /usr/ports/{distfiles,packages,work,pkgbuild}"

    chroot $CHROOT /usr/bin/env -i \
	HOME=/root TERM="$TERM" PS1='\u:\w\$ ' \
	PATH=/bin:/usr/bin:/sbin:/usr/sbin \
	/bin/bash -c "chown pkgmk:pkgmk /usr/ports/pkgbuild"

    chroot $CHROOT /usr/bin/env -i \
	HOME=/root TERM="$TERM" PS1='\u:\w\$ ' \
	PATH=/bin:/usr/bin:/sbin:/usr/sbin \
	/bin/bash -c "chmod g+w /usr/ports/pkgbuild"

    PKGMK_WRK="pkgmk   /usr/ports/work tmpfs size=30G,gid=$(id -g pkgmk),uid=$(id -u pkgmk),defaults,mode=0750 0    0 >> /etc/fstab"

    chroot $CHROOT /usr/bin/env -i \
	HOME=/root TERM="$TERM" PS1='\u:\w\$ ' \
	PATH=/bin:/usr/bin:/sbin:/usr/sbin \
	/bin/bash -c "echo ${PKGMK_WRK} >> /etc/fstab"
}

setup_config(){
    vim $CHROOT/etc/rc.conf
    vim $CHROOT/etc/hosts
    vim $CHROOT/etc/resolv.conf
    vim $CHROOT/etc/fstab

    vim $CHROOT/etc/pkgmk.conf
    vim $CHROOT/etc/prt-get.conf
}

echo "ADMIN_USER=${ADMIN_USER}";
echo "CHROOT=${CHROOT}";
echo "DATA_CNF=${DATA_CNF}";

ConfirmOrExit

setup_core
setup_users
setup_config