cloudmin-kvm-redhat-install.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #!/bin/sh
  2. # cloudmin-kvm-redhat-install.sh
  3. # Copyright 2005-2011 Virtualmin, Inc.
  4. #
  5. # Installs Cloudmin GPL for KVM and all dependencies on a CentOS, RHEL or
  6. # Fedora system.
  7. VER=1.1
  8. # Define functions
  9. yesno () {
  10. while read line; do
  11. case $line in
  12. y|Y|Yes|YES|yes|yES|yEs|YeS|yeS) return 0
  13. ;;
  14. n|N|No|NO|no|nO) return 1
  15. ;;
  16. *)
  17. printf "\nPlease enter y or n: "
  18. ;;
  19. esac
  20. done
  21. }
  22. # Show help page
  23. if [ "$1" = "--help" ]; then
  24. echo $0 [--qemu] [--no-bridge | --bridge-interface ethN]
  25. exit 0
  26. fi
  27. # Ask the user first
  28. cat <<EOF
  29. *******************************************************************************
  30. * Welcome to the Cloudmin GPL for KVM installer, version $VER *
  31. *******************************************************************************
  32. Operating systems supported by this installer are:
  33. Fedora Core 3-12 on i386 and x86_64
  34. CentOS and RHEL 3-7 on i386 and x86_64
  35. If your OS is not listed above, this script will fail (and attempting
  36. to run it on an unsupported OS is not recommended, or...supported).
  37. EOF
  38. printf " Continue? (y/n) "
  39. if ! yesno
  40. then exit
  41. fi
  42. echo ""
  43. # Check for non-kernel mode flag
  44. if [ "$1" = "--qemu" ]; then
  45. shift
  46. qemu=1
  47. fi
  48. # Check for flag to disable bridge setup
  49. if [ "$1" = "--no-bridge" ]; then
  50. shift
  51. nobridge=1
  52. fi
  53. # Check for flag for network interface
  54. interface=eth0
  55. if [ "$1" = "--bridge-interface" ]; then
  56. shift
  57. interface=$1
  58. shift
  59. fi
  60. # Cleanup old repo files
  61. rm -f /etc/yum.repos.d/vm2* /etc/yum.repos.d/cloudmin*
  62. if [ "$qemu" != 1 ]; then
  63. # Check for KVM-capable CPU
  64. echo Checking for hardware support for KVM ..
  65. grep -e vmx -e svm /proc/cpuinfo >/dev/null
  66. if [ $? != 0 ]; then
  67. echo .. not found. Make sure your CPU has Intel VT-x or AMD-V support
  68. echo ""
  69. exit 1
  70. fi
  71. echo .. found OK
  72. echo ""
  73. fi
  74. # Check for the expected bridge interface
  75. if [ "$nobridge" = "" ]; then
  76. echo Checking for network interface $interface ..
  77. (ifconfig -a ; ip addr ; true) 2>/dev/null | grep $interface >/dev/null
  78. if [ "$?" != 0 ]; then
  79. echo ".. not found. Use the --bridge-interface flag to set the correct interface"
  80. exit 1
  81. fi
  82. echo .. found OK
  83. echo ""
  84. fi
  85. # Check for yum
  86. echo Checking for yum ..
  87. if [ ! -x /usr/bin/yum ]; then
  88. echo .. not installed. The Cloudmin installer requires YUM to download packages
  89. echo ""
  90. exit 1
  91. fi
  92. echo .. found OK
  93. echo ""
  94. # Make sure we have wget
  95. echo "Installing wget .."
  96. yum install -y wget
  97. echo ".. done"
  98. echo ""
  99. # Check for wget or curl
  100. echo "Checking for curl or wget..."
  101. if [ -x "/usr/bin/curl" ]; then
  102. download="/usr/bin/curl -s "
  103. elif [ -x "/usr/bin/wget" ]; then
  104. download="/usr/bin/wget -nv -O -"
  105. else
  106. echo "No web download program available: Please install curl or wget"
  107. echo "and try again."
  108. exit 1
  109. fi
  110. echo "found $download"
  111. echo ""
  112. # Create Cloudmin licence file
  113. echo Creating Cloudmin licence file
  114. cat >/etc/server-manager-license <<EOF
  115. SerialNumber=GPL
  116. LicenseKey=GPL
  117. EOF
  118. chmod 600 /etc/server-manager-license
  119. # Download GPG keys
  120. echo Downloading GPG keys for packages ..
  121. $download "http://software.virtualmin.com/lib/RPM-GPG-KEY-virtualmin" >/etc/pki/rpm-gpg/RPM-GPG-KEY-virtualmin
  122. if [ "$?" != 0 ]; then
  123. echo .. download failed
  124. exit 1
  125. fi
  126. $download "http://software.virtualmin.com/lib/RPM-GPG-KEY-webmin" >/etc/pki/rpm-gpg/RPM-GPG-KEY-webmin
  127. if [ "$?" != 0 ]; then
  128. echo .. download failed
  129. exit 1
  130. fi
  131. echo .. done
  132. echo ""
  133. # Import keys
  134. echo Importing GPG keys ..
  135. rpm -q gpg-pubkey-a0bdbcf9-42d1d837 || rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-virtualmin
  136. rpm -q gpg-pubkey-11f63c51-3c7dc11d || rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-webmin
  137. echo .. done
  138. echo ""
  139. # Setup the YUM repo file
  140. echo Creating YUM repository for Cloudmin packages ..
  141. cat >/etc/yum.repos.d/cloudmin.repo <<EOF
  142. [cloudmin-universal]
  143. name=Cloudmin Distribution Neutral
  144. baseurl=http://cloudmin.virtualmin.com/kvm/universal/
  145. enabled=1
  146. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-virtualmin
  147. gpgcheck=1
  148. EOF
  149. echo .. done
  150. echo ""
  151. # YUM install Perl, modules and other dependencies
  152. echo Installing required Perl modules using YUM ..
  153. yum install -y perl openssl perl-Net-SSLeay bind bind-utils kvm kvm-qemu-img qemu-kvm qemu-img qemu-kvm-tools kvm-tools bridge-utils lsof parted perl-Data-Dumper bzip2 psmisc
  154. if [ "$?" != 0 ]; then
  155. echo .. install failed
  156. exit 1
  157. fi
  158. yum install -y kmod-kvm
  159. yum install -y perl-JSON
  160. yum install -y dhcp
  161. yum install -y ebtables
  162. yum install -y openssh-clients
  163. yum install -y libcgroup-tools
  164. yum install -y vixie-cron || yum install -y cronie
  165. echo .. done
  166. echo ""
  167. # Activate cgroups
  168. echo Installing and activating cgroups ..
  169. yum install -y libcgroup
  170. if [ "$?" != 0 ]; then
  171. echo .. install failed - CPU limits will not be available
  172. else
  173. service cgconfig start
  174. chkconfig cgconfig on
  175. echo .. done
  176. fi
  177. echo ""
  178. # Activate KVM kernel module
  179. if [ "$qemu" != 1 ]; then
  180. echo Activating KVM kernel module ..
  181. modprobe kvm-intel || modprobe kvm-amd
  182. /sbin/lsmod | grep kvm >/dev/null
  183. if [ "$?" != 0 ]; then
  184. echo .. kernel module did not load successfully
  185. exit 1
  186. fi
  187. sleep 5
  188. if [ ! -e /dev/kvm ]; then
  189. echo .. KVM device file /dev/kvm is missing
  190. exit 1
  191. fi
  192. echo .. done
  193. echo ""
  194. fi
  195. # Create loop devices if missing
  196. echo Creating /dev/loop devices ..
  197. for loop in 0 1 2 3 4 5 6 7; do
  198. if [ ! -r "/dev/loop$loop" ]; then
  199. mknod "/dev/loop$loop" b 7 $loop
  200. fi
  201. done
  202. echo .. done
  203. echo ""
  204. # YUM install webmin, theme and Cloudmin
  205. echo Installing Cloudmin packages using YUM ..
  206. yum install -y webmin wbm-server-manager wbt-virtual-server-theme
  207. if [ "$?" != 0 ]; then
  208. echo .. install failed
  209. exit 1
  210. fi
  211. mkdir -p /kvm
  212. echo .. done
  213. echo ""
  214. # Configure Webmin to use theme
  215. echo Configuring Webmin ..
  216. grep -v "^preroot=" /etc/webmin/miniserv.conf >/tmp/miniserv.conf.$$
  217. echo preroot=authentic-theme >>/tmp/miniserv.conf.$$
  218. cat /tmp/miniserv.conf.$$ >/etc/webmin/miniserv.conf
  219. rm -f /tmp/miniserv.conf.$$
  220. grep -v "^theme=" /etc/webmin/config >/tmp/config.$$
  221. echo theme=authentic-theme >>/tmp/config.$$
  222. cat /tmp/config.$$ >/etc/webmin/config
  223. rm -f /tmp/config.$$
  224. /etc/webmin/restart
  225. echo .. done
  226. echo ""
  227. # Setup BIND zone for virtual systems
  228. basezone=`hostname -d`
  229. if [ "$basezone" = "" ]; then
  230. basezone=example.com
  231. fi
  232. zone="cloudmin.$basezone"
  233. echo Creating DNS zone $zone ..
  234. /usr/libexec/webmin/server-manager/setup-bind-zone.pl --zone $zone --auto-view
  235. if [ "$?" != 0 ]; then
  236. echo .. failed
  237. else
  238. echo kvm_zone=$zone >>/etc/webmin/server-manager/config
  239. echo kvm_zone=$zone >>/etc/webmin/server-manager/this
  240. echo .. done
  241. fi
  242. echo ""
  243. # Set Qemu mode flag
  244. if [ "$qemu" = 1 ]; then
  245. echo kvm_qemu=1 >>/etc/webmin/server-manager/config
  246. fi
  247. # Enable bridge
  248. if [ "$nobridge" = "" ]; then
  249. echo Creating network bridge ..
  250. /usr/libexec/webmin/server-manager/setup-kvm-bridge.pl $interface
  251. brex=$?
  252. if [ "$brex" = 0 ]; then
  253. echo .. already active
  254. else
  255. if [ "$brex" = 1 ]; then
  256. echo .. done
  257. else
  258. echo .. bridge creation failed
  259. fi
  260. fi
  261. echo ""
  262. fi
  263. # Open Webmin firewall port
  264. echo Opening port 10000 on IPtables firewall ..
  265. ports="10000 10001 10002 10003 10004 10005 843"
  266. /usr/libexec/webmin/firewall/open-ports.pl $ports
  267. if [ "$?" != 0 ]; then
  268. echo .. failed
  269. else
  270. echo .. done
  271. fi
  272. echo ""
  273. # Open firewalld ports
  274. if [ -x /usr/bin/firewall-cmd ]; then
  275. echo Opening port 10000 on Firewalld firewall ..
  276. for port in $ports; do
  277. /usr/bin/firewall-cmd --add-port=$port/tcp >/dev/null
  278. /usr/bin/firewall-cmd --permanent --add-port=$port/tcp >/dev/null
  279. done
  280. echo .. done
  281. echo ""
  282. fi
  283. # Tell user about need to reboot
  284. hostname=`hostname`
  285. if [ "$brex" = 1 ]; then
  286. echo Cloudmin GPL has been successfully installed. However, you will
  287. echo need to reboot to activate the network bridge before any KVM
  288. echo instances can be created.
  289. echo
  290. echo One this is done, you can login to Cloudmin at :
  291. echo https://$hostname:10000/
  292. else
  293. echo Cloudmin GPL has been successfully installed.
  294. echo
  295. echo You can login to Cloudmin at :
  296. echo https://$hostname:10000/
  297. fi
  298. # All done!