source: lab/vendor/lxc/0.8.0~rc1-4ubuntu37/templates/lxc-sshd @ 175

Last change on this file since 175 was 175, checked in by mitty, 12 years ago
  • /usr/lib/lxc/templates of lxc 0.8.0~rc1-4ubuntu37 on Ubuntu 12.10 (beta)
  • Property svn:executable set to *
File size: 5.2 KB
Line 
1#!/bin/bash
2
3#
4# lxc: linux Container library
5
6# Authors:
7# Daniel Lezcano <daniel.lezcano@free.fr>
8
9# This library is free software; you can redistribute it and/or
10# modify it under the terms of the GNU Lesser General Public
11# License as published by the Free Software Foundation; either
12# version 2.1 of the License, or (at your option) any later version.
13
14# This library is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17# Lesser General Public License for more details.
18
19# You should have received a copy of the GNU Lesser General Public
20# License along with this library; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23install_sshd()
24{
25    rootfs=$1
26
27    tree="\
28$rootfs/var/run/sshd \
29$rootfs/var/empty/sshd \
30$rootfs/var/lib/empty/sshd \
31$rootfs/etc/ssh \
32$rootfs/dev/shm \
33$rootfs/run/shm \
34$rootfs/proc \
35$rootfs/bin \
36$rootfs/sbin \
37$rootfs/usr \
38$rootfs/tmp \
39$rootfs/home \
40$rootfs/root \
41$rootfs/lib \
42$rootfs/lib64"
43
44    mkdir -p $tree
45    if [ $? -ne 0 ]; then
46        return 1
47    fi
48
49    return 0
50}
51
52configure_sshd()
53{
54    rootfs=$1
55
56    cat <<EOF > $rootfs/etc/passwd
57root:x:0:0:root:/root:/bin/bash
58sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
59EOF
60
61    cat <<EOF > $rootfs/etc/group
62root:x:0:root
63sshd:x:74:
64EOF
65
66ssh-keygen -t rsa -f $rootfs/etc/ssh/ssh_host_rsa_key
67ssh-keygen -t dsa -f $rootfs/etc/ssh/ssh_host_dsa_key
68
69    # by default setup root password with no password
70    cat <<EOF > $rootfs/etc/ssh/sshd_config
71Port 22
72Protocol 2
73HostKey /etc/ssh/ssh_host_rsa_key
74HostKey /etc/ssh/ssh_host_dsa_key
75UsePrivilegeSeparation yes
76KeyRegenerationInterval 3600
77ServerKeyBits 768
78SyslogFacility AUTH
79LogLevel INFO
80LoginGraceTime 120
81PermitRootLogin yes
82StrictModes yes
83RSAAuthentication yes
84PubkeyAuthentication yes
85IgnoreRhosts yes
86RhostsRSAAuthentication no
87HostbasedAuthentication no
88PermitEmptyPasswords yes
89ChallengeResponseAuthentication no
90EOF
91
92    if [ -n "$auth_key" -a -f "$auth_key" ]; then
93        u_path="/root/.ssh"
94        root_u_path="$rootfs/$u_path"
95        mkdir -p $root_u_path
96        cp $auth_key "$root_u_path/authorized_keys"
97        chown -R 0:0 "$rootfs/$u_path"
98    chmod 700 "$rootfs/$u_path"
99
100        echo "Inserted SSH public key from $auth_key into /home/ubuntu/.ssh/authorized_keys"
101    fi
102
103    return 0
104}
105
106copy_configuration()
107{
108    path=$1
109    rootfs=$2
110    name=$3
111
112    grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
113    cat <<EOF >> $path/config
114lxc.utsname = $name
115lxc.pts = 1024
116# uncomment the next line to run the container unconfined:
117#lxc.aa_profile = unconfined
118lxc.mount.entry=/dev dev none ro,bind 0 0
119lxc.mount.entry=/lib lib none ro,bind 0 0
120lxc.mount.entry=/bin bin none ro,bind 0 0
121lxc.mount.entry=/usr usr none ro,bind 0 0
122lxc.mount.entry=/sbin sbin none ro,bind 0 0
123lxc.mount.entry=tmpfs var/run/sshd tmpfs mode=0644 0 0
124lxc.mount.entry=/usr/share/lxc/templates/lxc-sshd sbin/init none bind 0 0
125lxc.mount.entry=proc proc proc nodev,noexec,nosuid 0 0
126EOF
127
128    # if no .ipv4 section in config, then have the container run dhcp
129    grep -q "^lxc.network.ipv4" $path/config || touch $rootfs/run-dhcp
130
131    if [ "$(uname -m)" = "x86_64" ]; then
132        cat <<EOF >> $path/config
133lxc.mount.entry=/lib64 lib64 none ro,bind 0 0
134EOF
135    fi
136}
137
138usage()
139{
140    cat <<EOF
141$1 -h|--help -p|--path=<path>
142EOF
143    return 0
144}
145
146options=$(getopt -o hp:n:S: -l help,path:,name:,auth-key: -- "$@")
147if [ $? -ne 0 ]; then
148        usage $(basename $0)
149    exit 1
150fi
151eval set -- "$options"
152
153while true
154do
155    case "$1" in
156        -h|--help)      usage $0 && exit 0;;
157        -p|--path)      path=$2; shift 2;;
158        -n|--name)      name=$2; shift 2;;
159        -S|--auth-key)  auth_key=$2; shift 2;;
160        --)             shift 1; break ;;
161        *)              break ;;
162    esac
163done
164
165if [ "$(id -u)" != "0" ]; then
166    echo "This script should be run as 'root'"
167    exit 1
168fi
169
170if [ $0 == "/sbin/init" ]; then
171
172    type ${libexecdir}/lxc-init
173    if [ $? -ne 0 ]; then
174        echo "'lxc-init is not accessible on the system"
175        exit 1
176    fi
177
178    type sshd
179    if [ $? -ne 0 ]; then
180        echo "'sshd' is not accessible on the system "
181        exit 1
182    fi
183
184    # run dhcp?
185    if [ -f /run-dhcp ]; then
186        type dhclient
187        if [ $? -ne 0 ]; then
188            echo "can't find dhclient"
189            exit 1
190        fi
191    touch /etc/fstab
192        rm -f /dhclient.conf
193        cat > /dhclient.conf << EOF
194send host-name "<hostname>";
195EOF
196    ifconfig eth0 up
197        dhclient eth0 -cf /dhclient.conf
198    fi
199
200    exec ${libexecdir}/lxc-init -- /usr/sbin/sshd
201    exit 1
202fi
203
204if [ -z "$path" ]; then
205    echo "'path' parameter is required"
206    exit 1
207fi
208
209# detect rootfs
210config="$path/config"
211if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
212    rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
213else
214    rootfs=$path/rootfs
215fi
216
217install_sshd $rootfs
218if [ $? -ne 0 ]; then
219    echo "failed to install sshd's rootfs"
220    exit 1
221fi
222
223configure_sshd $rootfs
224if [ $? -ne 0 ]; then
225    echo "failed to configure sshd template"
226    exit 1
227fi
228
229copy_configuration $path $rootfs $name
230if [ $? -ne 0 ]; then
231    echo "failed to write configuration file"
232    exit 1
233fi
Note: See TracBrowser for help on using the repository browser.