github.com/cloudfoundry-attic/garden-linux@v0.333.2-candidate/linux_backend/skeleton/setup.sh (about) 1 #!/bin/bash 2 3 set -o xtrace 4 set -o nounset 5 set -o errexit 6 shopt -s nullglob 7 8 cd $(dirname $0) 9 10 # Defaults for debugging the setup script 11 iface_name_prefix="${GARDEN_NETWORK_INTERFACE_PREFIX}" 12 max_id_len=$(expr 16 - ${#iface_name_prefix} - 2) 13 iface_name=$(tail -c ${max_id_len} <<< ${id}) 14 id=${id:-test} 15 network_cidr=${network_cidr:-10.0.0.0/30} 16 container_iface_mtu=${container_iface_mtu:-1500} 17 network_host_ip=${network_host_ip:-10.0.0.1} 18 network_host_iface="${iface_name_prefix}${iface_name}-0" 19 network_container_ip=${network_container_ip:-10.0.0.2} 20 network_container_iface="${iface_name_prefix}${iface_name}-1" 21 bridge_iface="${bridge_iface}" 22 network_cidr_suffix=${network_cidr_suffix:-30} 23 root_uid=${root_uid:-10000} 24 rootfs_path=$(readlink -f $rootfs_path) 25 26 if [ ! -d $rootfs_path/tmp ]; then 27 mkdir $rootfs_path/tmp 28 fi 29 chmod 1777 $rootfs_path/tmp 30 31 if [ ! -d $rootfs_path/etc ]; then 32 mkdir $rootfs_path/etc 33 chmod 0755 $rootfs_path/etc 34 fi 35 36 # Write configuration 37 cat > etc/config <<-EOS 38 id=$id 39 network_host_ip=$network_host_ip 40 network_host_iface=$network_host_iface 41 network_container_ip=$network_container_ip 42 network_container_iface=$network_container_iface 43 bridge_iface=$bridge_iface 44 network_cidr_suffix=$network_cidr_suffix 45 container_iface_mtu=$container_iface_mtu 46 network_cidr=$network_cidr 47 root_uid=$root_uid 48 rootfs_path=$rootfs_path 49 external_ip=$external_ip 50 EOS 51 52 if [ ! -d $rootfs_path/proc ]; then 53 mkdir -p $rootfs_path/proc 54 chown $root_uid:$root_uid $rootfs_path/proc 55 chmod 0755 $rootfs_path/proc 56 fi 57 58 if [ ! -d $rootfs_path/sys ]; then 59 mkdir -p $rootfs_path/sys 60 chown $root_uid:$root_uid $rootfs_path/sys 61 chmod 0755 $rootfs_path/sys 62 fi 63 64 #chown $root:0 $rootfs_path/proc 65 66 if [ ! -d $rootfs_path/dev ]; then 67 mkdir -p $rootfs_path/dev 68 chown $root_uid:$root_uid $rootfs_path/dev 69 chmod 0755 $rootfs_path/dev 70 fi 71 72 # Strip /dev down to the bare minimum 73 rm -rf $rootfs_path/dev/* 74 75 if [ ! -d $rootfs_path/dev/shm ]; then 76 mkdir $rootfs_path/dev/shm 77 chown $root_uid:$root_uid $rootfs_path/dev/shm 78 chmod 1777 $rootfs_path/dev/shm 79 fi 80 81 # add device: adddev <owner> <device-file-path> <mknod-1> <mknod-2> 82 function adddev() 83 { 84 local own=${1} 85 local file=${2} 86 local opts="c ${3} ${4}" 87 88 mknod -m 666 ${file} ${opts} 89 chown root:${own} ${file} 90 } 91 92 93 # /dev/tty 94 adddev tty $rootfs_path/dev/tty 5 0 95 # /dev/random, /dev/urandom 96 adddev root $rootfs_path/dev/random 1 8 97 adddev root $rootfs_path/dev/urandom 1 9 98 # /dev/null, /dev/zero, /dev/full 99 adddev root $rootfs_path/dev/null 1 3 100 adddev root $rootfs_path/dev/zero 1 5 101 adddev root $rootfs_path/dev/full 1 7 102 103 # /dev/fd, /dev/std{in,out,err} 104 pushd $rootfs_path/dev > /dev/null 105 ln -s /proc/self/fd 106 ln -s fd/0 stdin 107 ln -s fd/1 stdout 108 ln -s fd/2 stderr 109 popd > /dev/null 110 111 # Add fuse group and device, so fuse can work inside the container 112 mknod -m 666 $rootfs_path/dev/fuse c 10 229 113 chown $root_uid:$root_uid $rootfs_path/dev/fuse 114 chmod ugo+rw $rootfs_path/dev/fuse 115 116 cat > $rootfs_path/etc/hostname <<-EOS 117 $id 118 EOS 119 120 cat > $rootfs_path/etc/hosts <<-EOS 121 127.0.0.1 localhost 122 $network_container_ip $id 123 EOS 124 125 # By default, inherit the nameserver from the host container. 126 # 127 # Exception: When the host's nameserver is set to localhost (127.0.0.1), it is 128 # assumed to be running its own DNS server and listening on all interfaces. 129 # In this case, the container must use the network_host_ip address 130 # as the nameserver. 131 if [[ "$(cat /etc/resolv.conf)" == "nameserver 127.0.0.1" ]] 132 then 133 cat > $rootfs_path/etc/resolv.conf <<-EOS 134 nameserver $network_host_ip 135 EOS 136 else 137 # some images may have something set up here; the host's should be the source 138 # of truth 139 rm -f $rootfs_path/etc/resolv.conf 140 141 cp /etc/resolv.conf $rootfs_path/etc/ 142 fi 143 144 if [ -d "$rootfs_path/dev" ] && [ "$root_uid" -ne 0 ]; then 145 chown -R $root_uid:$root_uid "$rootfs_path/dev" 146 fi