Advertisement
Guest User

create_slackware_openvz_template_14.2.sh

a guest
Mar 1st, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.15 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # $Id: create_slackware_openvz_template.sh,v 1.5 2015/05/20 09:48:11 root Exp root $
  4. # Copyright 2014, 2015  Eric Hameleers, Eindhoven, NL
  5. # All rights reserved.
  6. #
  7. #   Permission to use, copy, modify, and distribute this software for
  8. #   any purpose with or without fee is hereby granted, provided that
  9. #   the above copyright notice and this permission notice appear in all
  10. #   copies.
  11. #
  12. #   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  13. #   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. #   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. #   IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
  16. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  19. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  21. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. #   SUCH DAMAGE.
  24. # -----------------------------------------------------------------------------
  25. #
  26.  
  27. # This script creates a template for a Slackware OS, to be deployed with openvz.
  28.  
  29. # Stamp today in the filename:
  30. THEDATE=$(date +%Y%m%d)
  31.  
  32. # Slackware version to install:
  33. #1 SL_VERSION=${SL_VERSION:-"14.1"}
  34. SL_VERSION=${SL_VERSION:-"14.2"}
  35.  
  36. # Slackware architecture to install:
  37. SL_ARCH=${SL_ARCH:-"x86_64"}
  38.  
  39. # Directory suffix, arch dependent:
  40. if [ "$SL_ARCH" = "x86_64" ]; then
  41.   DIRSUFFIX="64"
  42. else
  43.   DIRSUFFIX=""
  44. fi
  45.  
  46. # Root directory of a Slackware local mirror tree:
  47. #2 SL_REPO=${SL_REPO:-"/home/ftp/pub/Linux/Slackware"}
  48. SL_REPO=${SL_REPO:-"/home/someuser/openvz_template"}
  49.  
  50. # Package root directory:
  51. SL_PKGROOT=${SL_REPO}/slackware${DIRSUFFIX}-${SL_VERSION}/slackware${DIRSUFFIX}
  52.  
  53. # Patches root directory:
  54. SL_PATCHROOT=${SL_REPO}/slackware${DIRSUFFIX}-${SL_VERSION}/patches/packages
  55.  
  56. # Directory where we will install the root filesystem:
  57. #3 VZ_ROOTDIR=${VZ_ROOTDIR:-"/mnt/openzvz_temp"}
  58. VZ_ROOTDIR=${VZ_ROOTDIR:-"/home/someuser/openvz_template/openvz_temp"}
  59.  
  60. # Directory where the template archive will be written:
  61. #4 OUTPUT=${OUTPUT:-"/tmp"}
  62. OUTPUT=${OUTPUT:-"/home/someuser/openvz_template/output"}
  63.  
  64. # Slackware minimal package list:
  65. SL_PKGLIST="
  66. aaa_base
  67. aaa_elflibs
  68. aaa_terminfo
  69. acl
  70. attr
  71. bash
  72. bin
  73. binutils
  74. bison
  75. bzip2
  76. coreutils
  77. cpio
  78. dcron
  79. dev86
  80. devs
  81. dhcpcd
  82. dialog
  83. diffutils
  84. dmidecode
  85. e2fsprogs
  86. elvis
  87. etc
  88. eudev
  89. file
  90. findutils
  91. flex
  92. floppy
  93. gawk
  94. gcc
  95. glibc-solibs
  96. glibc-zoneinfo
  97. gnupg
  98. gnutls
  99. gptfdisk
  100. grep
  101. groff
  102. gzip
  103. infozip
  104. iptables
  105. iputils
  106. kernel-firmware
  107. kernel-headers
  108. kmod
  109. less
  110. libgudev
  111. libmpc
  112. libtermcap
  113. lilo
  114. links
  115. logrotate
  116. lvm2
  117. make
  118. man
  119. man-pages
  120. mkinitrd
  121. mpfr
  122. mtr
  123. nano
  124. ncurses
  125. net-tools
  126. network-scripts
  127. openssh
  128. openssl-solibs
  129. parted
  130. pkgtools
  131. polkit
  132. procps-ng
  133. quota
  134. rsync
  135. screen
  136. sed
  137. shadow
  138. sharutils
  139. slackpkg
  140. slocate
  141. strace
  142. sudo
  143. sysklogd
  144. syslinux
  145. sysvinit
  146. sysvinit-functions
  147. sysvinit-scripts
  148. tar
  149. usbutils
  150. utempter
  151. util-linux
  152. wget
  153. which
  154. whois
  155. xz
  156. "
  157. # Action!
  158.  
  159. # Some sanity checks first.
  160. if [ ! -d ${SL_REPO} ]; then
  161.   echo "-- Slackware repository root '${SL_REPO}' does not exist! Exiting."
  162.   exit 1
  163. fi
  164.  
  165. # Create output directory for template file:
  166. mkdir -p ${OUTPUT}
  167. if [ $? -ne 0 ]; then
  168.   echo "-- Creation of output directory '${OUTPUT}' failed! Exiting."
  169.   exit 1
  170. fi
  171.  
  172. # Create working directory:
  173. if [ ! -d ${VZ_ROOTDIR} ]; then
  174.   mkdir -p ${VZ_ROOTDIR}
  175.   if [ $? -ne 0 ]; then
  176.     echo "-- Creation of working directory '${VZ_ROOTDIR}' failed! Exiting."
  177.     exit 1
  178.   fi
  179. else
  180.   echo "-- Found an existing openvz root filesystem at '${VZ_ROOTDIR}'".
  181.   echo "-- After 10 seconds we will proceed and wipe it."
  182.   echo "-- If you do not want to delete this, please press Ctrl-C now!"
  183.   read -t 10 -p "-- Continue [Y/n] ?"
  184.   if [ "x$REPLY" = "xn" -o "x$REPLY" = "xN" ]; then
  185.     echo "-- OK: exiting now."
  186.   fi
  187. fi
  188. echo
  189. rm -rf --one-file-system ${VZ_ROOTDIR}/*
  190. chmod 775 ${VZ_ROOTDIR}
  191.  
  192. # Find packages and install them into the temporary root:
  193. for PKG in $(echo $SL_PKGLIST); do
  194.   FULLPKG=$(find ${SL_PATCHROOT} -name "${PKG}-*.t?z" | grep -E "${PKG}-[^-]+-[^-]+-[^-]+.t?z")
  195.   if [ "x${FULLPKG}" = "x" ]; then
  196.     FULLPKG=$(find ${SL_PKGROOT} -name "${PKG}-*.t?z" | grep -E "${PKG}-[^-]+-[^-]+-[^-]+.t?z")
  197.   else
  198.     echo "-- $PKG found in patches"
  199.   fi
  200.   if [ "x${FULLPKG}" = "x" ]; then
  201.     echo "-- Package $PKG was not found in Slackware ${SL_VERSION} !"
  202.   else
  203.     installpkg --terse --root ${VZ_ROOTDIR} ${FULLPKG}
  204.   fi
  205. done
  206.  
  207. # Next step, prepare the root filesystem for use as an openvz container.
  208.  
  209. # Clean out the unneeded stuff:
  210. rm -f ${VZ_ROOTDIR}/boot/*
  211. rm -f ${VZ_ROOTDIR}/tmp/[A-Za-z]*
  212. rm -f ${VZ_ROOTDIR}/var/mail/*
  213.  
  214. # Disable unneeded services:
  215. [ -f ${VZ_ROOTDIR}/etc/rc.d/rc.acpid ] && chmod -x ${VZ_ROOTDIR}/etc/rc.d/rc.acpid
  216. [ -f ${VZ_ROOTDIR}/etc/rc.d/rc.pcmcia ] && chmod -x ${VZ_ROOTDIR}/etc/rc.d/rc.pcmcia
  217. [ -f ${VZ_ROOTDIR}/etc/rc.d/rc.udev ] && chmod -x ${VZ_ROOTDIR}/etc/rc.d/rc.udev
  218.  
  219. # Remove ssh server keys - new unique keys will be generated
  220. # at first boot of a container:
  221. rm -f ${VZ_ROOTDIR}/etc/ssh/*key*
  222.  
  223. # Delete /etc/mtab and make it a symlink to /proc/mounts:
  224. rm -f ${VZ_ROOTDIR}/etc/mtab
  225. ln -s /proc/mounts ${VZ_ROOTDIR}/etc/mtab
  226.  
  227. # Change /etc/rc.d/rc.S so that Slackware does not remove our /etc/mtab symlink:
  228. sed -i -e '/ \/etc\/mtab/s/^/# /' ${VZ_ROOTDIR}/etc/rc.d/rc.S
  229.  
  230. # No hardware clock present:
  231. sed -i -e '/^if \[ -x \/sbin\/hwclock/,/^fi$/s/^/#/' ${VZ_ROOTDIR}/etc/rc.d/rc.S
  232.  
  233. # We need this to skip the WRITE check at next boot, which would drop us in a
  234. # recovery shell and halt the boot process:
  235. sed -i -e '/^if touch \/fsrwtestfile/,/^fi$/s/^/#/' ${VZ_ROOTDIR}/etc/rc.d/rc.S
  236.  
  237. # Better to disable the root filesystem check altogether:
  238. sed -i -e '/^if \[ ! \$READWRITE = yes/,/^fi # Done checking root filesystem/s/^/#/' ${VZ_ROOTDIR}/etc/rc.d/rc.S
  239.  
  240. # Setterm is not useful here:
  241. sed -i -e '/\/bin\/setterm/s/^/# /' ${VZ_ROOTDIR}/etc/rc.d/rc.M
  242.  
  243. # We can not write to the hardware clock:
  244. sed -i -e '/systohc/s/^/# /' ${VZ_ROOTDIR}/etc/rc.d/rc.6
  245.  
  246. # Skip all filesystem checks at boot:
  247. touch ${VZ_ROOTDIR}/etc/fastboot
  248.  
  249. # Sanitize /etc/fstab :
  250. cat << EOT > ${VZ_ROOTDIR}/etc/fstab
  251. devpts           /dev/pts         devpts      gid=5,mode=620   0   0
  252. tmpfs            /dev/shm         tmpfs   defaults,nodev,nosuid,mode=1777  0   0
  253. EOT
  254.  
  255. # Edit /etc/inittab so that console login processes are not spawned:
  256. sed -i -e "/agetty/s/^c/#c/" ${VZ_ROOTDIR}/etc/inittab
  257.  
  258. # Reduce the number of local consoles, two should be enough:
  259. sed -i -e '/^c3\|^c4\|^c5\|^c6/s/^/# /' ${VZ_ROOTDIR}/etc/inittab
  260.  
  261. # Edit /etc/shadow and invalidate the root password.
  262. # The openvz tools will set the password instead:
  263. sed -i -e '/^root/s/^root::/root:!:/' ${VZ_ROOTDIR}/etc/shadow
  264.  
  265. # Make sure we can access DNS:
  266. cat << EOT >> ${VZ_ROOTDIR}/etc/resolv.conf
  267. nameserver 8.8.4.4
  268. nameserver 8.8.8.8
  269.  
  270. EOT
  271.  
  272. # Enable a Slackware mirror for slackpkg:
  273. cat <<EOT >> ${VZ_ROOTDIR}/etc/slackpkg/mirrors
  274. http://mirrors.slackware.com/slackware/slackware${DIRSUFFIX}-${SL_VERSION}/
  275. EOT
  276.  
  277. # Blacklist the l10n packages;
  278. cat << EOT >> ${VZ_ROOTDIR}/etc/slackpkg/blacklist
  279.  
  280. # Blacklist the l10n packages;
  281. calligra-l10n-
  282. kde-l10n-
  283.  
  284. EOT
  285.  
  286. # Update the cache for slackpkg:
  287. echo "-- Creating slackpkg cache, takes a few seconds..."
  288. chroot ${VZ_ROOTDIR} /usr/sbin/slackpkg update gpg 2>/dev/null
  289. chroot ${VZ_ROOTDIR} /usr/sbin/slackpkg update 2>/dev/null
  290.  
  291. # Create a locate cache:
  292. echo "-- Creating locate cache, takes a few seconds..."
  293. chroot ${VZ_ROOTDIR} /etc/cron.daily/slocate 2>/dev/null
  294.  
  295. # Compress the tree into an OpenVZ template:
  296. echo "-- Creating the template..."
  297. cd ${VZ_ROOTDIR}
  298.   tar -Jcvf ${OUTPUT}/slackware-${SL_VERSION}-${SL_ARCH}-minimal-${THEDATE}.tar.xz .
  299.   if [ $? -eq 0 ]; then
  300.     echo "-- Created ${OUTPUT}/slackware-${SL_VERSION}-${SL_ARCH}-minimal-${THEDATE}.tar.xz"
  301.   else
  302.     echo "-- Non-zero exitcode, something went wrong."
  303.   fi
  304. cd -
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement