Advertisement
Guest User

OpenVZ post-copy

a guest
Aug 28th, 2011
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.23 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. #  Changes after copying server/VM to configure containers
  5. #
  6.  
  7.  
  8. [[ ! ("$#" == 1 && -n "$1") ]] && echo "Only 1 argument required, $# provided" && exit 1;
  9.  
  10. [[ ! $1 =~ ^[0-9]+$ ]] && echo "Container ID has to be numeric, '$1' provided. Please provide a numeric argument and try again" && exit 1;
  11.  
  12.  
  13. if [ `whoami` = root ]
  14. then
  15.     echo Configuring Container $1....
  16.     # ttys/pttys and mounts
  17.     sed -i -e 's/^[0-9].*getty.*tty/#&/g'  /vz/private/$1/etc/inittab
  18.     echo "none /dev/pts devpts rw 0 0" > /vz/private/$1/etc/fstab
  19.     ln -sf /proc/mounts /vz/private/$1/etc/mtab
  20.  
  21.     # Start afresh
  22.     rm -rf /vz/private/$1/dev /vz/private/$1/mnt /vz/private/$1/proc /vz/private/$1/sys /vz/private/$1/tmp /vz/private/$1/dev/pts /vz/private/$1/etc/udev/devices /vz/private/$1/var/tmp
  23.  
  24.     # create dev tmp sys proc. Note: if  /vz/private/$1/var/tmp doesn't exist network inside CT wont start
  25.     mkdir  -p /vz/private/$1/dev /vz/private/$1/mnt /vz/private/$1/proc /vz/private/$1/sys /vz/private/$1/tmp /vz/private/$1/dev/pts /vz/private/$1/etc/udev/devices /vz/private/$1/var/tmp
  26.    
  27.     # udev
  28.     /sbin/MAKEDEV -d /vz/private/$1/dev -x {p,t}ty{a,p}{0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f} console core full kmem kmsg mem null port ptmx random urandom zero ram0
  29.     /sbin/MAKEDEV -d /vz/private/$1/etc/udev/devices -x {p,t}ty{a,p}{0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f} console core full kmem kmsg mem null port ptmx random urandom zero ram0
  30.     [[ `egrep -e "sbin/MAKEDEV pty|sbin/MAKEDEV tty|sbin/MAKEDEV urandom" /vz/private/$1/etc/rc.d/rc.local | wc -l` -ne 3  ]] &&  echo -e "\n/sbin/MAKEDEV pty\n/sbin/MAKEDEV tty\n/sbin/MAKEDEV urandom" >> /vz/private/$1/etc/rc.d/rc.local
  31.  
  32.     # tmp folders
  33.     chmod 1777 /vz/private/$1/tmp
  34.     chmod 1777 /vz/private/$1/var/tmp
  35.  
  36.     # disable iptables module. If a modules is needed first load on host and then inside CT
  37.     sed -i -e 's/^IPTABLES_MODULES="\(.*\)"/IPTABLES_MODULES=""/g' /vz/private/$1/etc/sysconfig/iptables-config
  38.    
  39.     # disable SSH root login
  40.     sed -i -e 's/^PermitRootLogin yes/#PermitRootLogin yes/g'  -e 's/^#PermitRootLogin no/PermitRootLogin no/g'  /vz/private/$1/etc/ssh/sshd_config
  41.    
  42.     # disable ipv6
  43.     sed -i -e 's/^NETWORKING_IPV6=yes/NETWORKING_IPV6=no/g'  -e 's/^NETWORKING_IPV6="yes"/NETWORKING_IPV6="no"/g'  /vz/private/$1/etc/sysconfig/network
  44.     [[ `egrep -e "blacklist ipv6|blacklist net-pf-10" /vz/private/$1/etc/modprobe.d/blacklist | wc -l` -ne 2  && -e /vz/private/$1/etc/modprobe.d/blacklist ]] &&  echo -e "blacklist ipv6\nblacklist net-pf-10" >> /vz/private/$1/etc/modprobe.d/blacklist
  45.    
  46.     # disable ethx (if using venet0)
  47.     # ls /vz/private/$1/etc/sysconfig/network-scripts/ifcfg-eth*| xargs sed -ie 's/ONBOOT=yes/ONBOOT=no/g'
  48.     [[ `ls /vz/private/$1/etc/sysconfig/network-scripts/ | egrep if'(down|up)'-venet  | wc -l` -ge 1 ]] && ls /vz/private/$1/etc/sysconfig/network-scripts/ | egrep if'(down|up)'-venet | xargs rm
  49.  
  50.     # add eth0 interface to the CT
  51.     vzctl set $1 --netif_add eth0 --save
  52.     echo -e "DEVICE=veth$1.0\nONBOOT=yes\nBRIDGE=br0" > /etc/sysconfig/network-scripts/ifcfg-veth$1.0
  53.    
  54.     # Start CT, enable veth and allow dns traffic inside CT
  55.     vzctl start $1
  56.     ifconfig veth$i.0 0
  57.     vzctl exec  $1 iptables -I RH-Firewall-1-INPUT -p udp -m udp --dport 53 -j ACCEPT
  58. else
  59.     echo 'Need to be "root" to execute this script'
  60. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement