4 # lxc: linux Container library
7 # Paul Robinson <takigama@gmail.com>
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.
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.
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
29 echo "Creating default configuration, $rootfs/config - EDIT THIS CONFIG"
33 # there arent many config variables here
34 # just ip address, hostname and gateway
36 ip4address=eth0:10.0.3.100/24
37 ip4address=lo:127.0.0.1/8
48 cat > node_base/config <<EOF
49 # this is where a list of apps to startup are stored
51 # appname:directory:commandline[:opts]
52 myapp:/node_code/somethingorother:dontrestart
74 $rootfs/node_modules \
76 $rootfs/node_base/control \
77 $rootfs/node_base/logs \
81 mkdir -p $tree || return 1
82 chmod 755 $tree || return 1
84 pushd $rootfs/dev > /dev/null || return 1
86 # minimal devices needed for busybox
87 mknod tty c 5 0 || res=1
88 mknod console c 5 1 || res=1
89 chmod 666 tty console || res=1
90 mknod tty0 c 4 0 || res=1
91 mknod tty1 c 4 0 || res=1
92 mknod tty5 c 4 0 || res=1
93 chmod 666 tty0 || res=1
94 mknod ram0 b 1 0 || res=1
95 chmod 600 ram0 || res=1
96 mknod null c 1 3 || res=1
97 chmod 666 null || res=1
100 echo "Downloading latest nodejs binaries for linux x64"
101 #fname=/tmp/nodejs.$RANDOM.`date +%s`tar.gz
102 # TODO: DONT FORGET TO REMOVE THIS
103 fname=/tmp/nodejs.11298.tar.gz
104 #wget -O $fname http://nodejs.org/dist/v0.8.17/node-v0.8.17-linux-x64.tar.gz
108 ln -s node*linux*64* node
112 echo "Installing ip cmd from upper OS"
118 cat >> sbin/init <<EOF
122 var fs = require("fs");
123 var mnt = require("/node_modules/mount");
125 mnt.mount("proc", "/proc", "proc");
127 console.log("starting scheduler");
128 setTimeout(scheduler, 2000);
129 setTimeout(startnode, 4000);
134 console.log("scheduler called: ", process.getuid());
136 //fs.readdir("/proc", function(err, files) {
137 //console.log("files: ", files);
140 setTimeout(scheduler, 2000);
143 function startnode() {
144 var opts = { uid: uid, gid: uid };
145 var nodespawn = require("child_process").spawn,
146 nodeproc = nodespawn("/node/bin/node", ["/node_code/nc.js"], opts);
147 nodeproc.stdout.on("data", function(data) {
148 console.log("data from nodespawn: " + data);
150 nodeproc.stderr.on("data", function(data) {
151 console.log("stderrdata from nodespawn: " + data);
153 nodeproc.on("exit", function(data) {
154 console.log("nodespawn died");
163 echo "Installing mount via npm"
164 ./node/bin/npm install mount
178 cat <<EOF >> $path/config
183 # uncomment the next line to run the container unconfined:
184 #lxc.aa_profile = unconfined
187 if [ -d "$rootfs/lib" ]; then
188 cat <<EOF >> $path/config
189 lxc.mount.entry=/lib $rootfs/lib none ro,bind 0 0
190 lxc.mount.entry=/usr/lib $rootfs/usr/lib none ro,bind 0 0
194 if [ -d "/lib64" ] && [ -d "$rootfs/lib64" ]; then
195 cat <<EOF >> $path/config
196 lxc.mount.entry=/lib64 $rootfs/lib64 none ro,bind 0 0
200 if [ -d "/usr/lib64" ] && [ -d "$rootfs/usr/lib64" ]; then
201 cat <<EOF >> $path/config
202 lxc.mount.entry=/usr/lib64 $rootfs/usr/lib64 none ro,bind 0 0
210 $1 -h|--help -p|--path=<path> -u|--user=<uid> -i|--ip=<ip>
216 ip_last=$(($RANDOM%100+100))
217 ip=10.0.3.$ip_last/24
219 options=$(getopt -o hp:u:n:i:g: -l help,path:,user:,name:,ip:,gateway: -- "$@")
220 if [ $? -ne 0 ]; then
224 eval set -- "$options"
229 -h|--help) usage $0 && exit 0;;
230 -u|--user) user=$2; shift 2;;
231 -i|--ip) ip=$2; shift 2;;
232 -g|--gateway) gw=$2; shift 2;;
233 -p|--path) path=$2; shift 2;;
234 -n|--name) name=$2; shift 2;;
235 --) shift 1; break ;;
240 if [ "$(id -u)" != "0" ]; then
241 echo "This script should be run as 'root'"
245 if [ -z "$path" ]; then
246 echo "'path' parameter is required"
252 install_node $rootfs $name $user
253 if [ $? -ne 0 ]; then
254 echo "failed to install the nodejs rootfs"
258 setup_node $rootfs $name $user
259 if [ $? -ne 0 ]; then
260 echo "Failed to setup node"
264 echo "sleeping for check - 20s"
267 echo Params are, $user, $path, $name
270 copy_configuration $path $rootfs $name
271 if [ $? -ne 0 ]; then
272 echo "failed to write configuration file"