PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/templates/lxc-openmandriva.in

https://gitlab.com/Red54/lxc
Autoconf | 490 lines | 375 code | 67 blank | 48 comment | 57 complexity | e78f136ce7c512d69534e303b21db839 MD5 | raw file
  1. #!/bin/bash
  2. #
  3. # template script for generating openmandriva container for LXC
  4. #
  5. #
  6. # lxc: linux Container library
  7. # Authors:
  8. # Alexander Khryukin <alexander@mezon.ru>
  9. # Vokhmin Alexey V <avokhmin@gmail.com>
  10. # This library is free software; you can redistribute it and/or
  11. # modify it under the terms of the GNU Lesser General Public
  12. # License as published by the Free Software Foundation; either
  13. # 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.
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. #
  22. # Detect use under userns (unsupported)
  23. for arg in "$@"; do
  24. [ "$arg" = "--" ] && break
  25. if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
  26. echo "This template can't be used for unprivileged containers." 1>&2
  27. echo "You may want to try the \"download\" template instead." 1>&2
  28. exit 1
  29. fi
  30. done
  31. # Make sure the usual locations are in PATH
  32. export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
  33. #Configurations
  34. #distro=cooker
  35. hostarch=$(uname -m)
  36. cache_base=@LOCALSTATEDIR@/cache/lxc/openmandriva/$arch
  37. default_path=@LXCPATH@
  38. default_profile=default
  39. root_password=root
  40. lxc_network_type=veth
  41. lxc_network_link=br0
  42. # is this openmandriva?
  43. [ -f /etc/mandriva-release ] && is_openmandriva=true
  44. configure_openmandriva()
  45. {
  46. mkdir -p ${rootfs_path}/etc/sysconfig/network-scripts/
  47. # configure the network using the dhcp
  48. cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-eth0
  49. DEVICE=eth0
  50. ONBOOT=yes
  51. BOOTPROTO=dhcp
  52. NM_CONTROLLED=no
  53. HOSTNAME=${utsname}
  54. EOF
  55. # set the hostname
  56. cat <<EOF > ${rootfs_path}/etc/sysconfig/network
  57. NETWORKING=yes
  58. HOSTNAME=${utsname}
  59. EOF
  60. echo "${utsname}" > ${rootfs_path}/etc/hostname
  61. # set minimal hosts
  62. cat <<EOF > $rootfs_path/etc/hosts
  63. 127.0.0.1 localhost.localdomain localhost $utsname
  64. ::1 localhost6.localdomain6 localhost6
  65. EOF
  66. }
  67. populate_dev()
  68. {
  69. echo -n "Create devices in /dev/"
  70. dev_path="${rootfs_path}/dev"
  71. rm -rf $dev_path
  72. mkdir -p $dev_path
  73. mknod -m 666 ${dev_path}/null c 1 3
  74. mknod -m 666 ${dev_path}/zero c 1 5
  75. mknod -m 666 ${dev_path}/random c 1 8
  76. mknod -m 666 ${dev_path}/urandom c 1 9
  77. mkdir -m 755 ${dev_path}/pts
  78. mkdir -m 1777 ${dev_path}/shm
  79. mknod -m 666 ${dev_path}/tty c 5 0
  80. mknod -m 666 ${dev_path}/tty0 c 4 0
  81. mknod -m 666 ${dev_path}/tty1 c 4 1
  82. mknod -m 666 ${dev_path}/tty2 c 4 2
  83. mknod -m 666 ${dev_path}/tty3 c 4 3
  84. mknod -m 666 ${dev_path}/tty4 c 4 4
  85. mknod -m 600 ${dev_path}/console c 5 1
  86. mknod -m 666 ${dev_path}/full c 1 7
  87. mknod -m 600 ${dev_path}/initctl p
  88. mknod -m 666 ${dev_path}/ptmx c 5 2
  89. mkdir -m 755 ${dev_path}/net
  90. mknod -m 666 ${dev_path}/net/tun c 10 200
  91. }
  92. set_guest_root_password()
  93. {
  94. [ -z "$root_password" ] && return # pass is empty, abort
  95. echo " - setting guest root password.."
  96. echo "root passwd is: $root_password"
  97. echo "root:$root_password" | chroot "$rootfs_path" chpasswd
  98. echo "done."
  99. }
  100. create_chroot_openmandriva()
  101. {
  102. # check the mini openmandriva was not already downloaded
  103. INSTALL_ROOT=$cache/cache
  104. mkdir -p $INSTALL_ROOT
  105. if [ $? -ne 0 ]; then
  106. echo "Failed to create '$INSTALL_ROOT' directory"
  107. return 1
  108. fi
  109. # package list to install
  110. PKG_LIST="basesystem-minimal locales locales-en initscripts urpmi cronie dhcp-client kbd"
  111. # download a mini openmandriva into a cache
  112. echo "Downloading openmandriva minimal ..."
  113. URPMI="/usr/sbin/urpmi.addmedia --urpmi-root $INSTALL_ROOT main http://abf.rosalinux.ru/downloads/$release/repository/$arch/main/release"
  114. echo $URPMI
  115. URPMI_BASE="/usr/sbin/urpmi --no-suggests --no-verify-rpm --ignorearch --root $INSTALL_ROOT --urpmi-root $INSTALL_ROOT --auto $PKG_LIST"
  116. $URPMI
  117. $URPMI_BASE
  118. # We're splitting the old loop into two loops plus a directory retrival.
  119. # First loop... Try and retrive a mirror list with retries and a slight
  120. # delay between attempts...
  121. if [ $? -ne 0 ]; then
  122. echo "Failed to download the rootfs, aborting."
  123. return 1
  124. fi
  125. mv "$INSTALL_ROOT" "$cache/rootfs"
  126. echo "Download complete."
  127. return 0
  128. }
  129. copy_openmandriva()
  130. {
  131. echo -n "Copying rootfs to $rootfs_path ..."
  132. mkdir -p $rootfs_path
  133. rsync -Ha $cache/rootfs/ $rootfs_path/
  134. return 0
  135. }
  136. update_openmandriva()
  137. {
  138. echo "automated update in progress..."
  139. urpmi --root $cache/rootfs --urpmi-root $cache/rootfs --auto --auto-update --ignorearch
  140. }
  141. configure_openmandriva_systemd()
  142. {
  143. chroot ${rootfs_path} ln -s /dev/null /etc/systemd/system/proc-sys-fs-binfmt_misc.automount
  144. chroot ${rootfs_path} ln -s /dev/null /etc/systemd/system/systemd-udevd.service
  145. chroot ${rootfs_path} ln -s /dev/null /etc/systemd/system/systemd-udevd-control.socket
  146. chroot ${rootfs_path} ln -s /dev/null /etc/systemd/system/systemd-udevd-kernel.socket
  147. # remove numlock service
  148. # KDGKBLED: Inappropriate ioctl for device
  149. rm -f ${rootfs_path}/etc/systemd/system/getty@.service.d/enable-numlock.conf
  150. unlink ${rootfs_path}/etc/systemd/system/default.target
  151. chroot ${rootfs_path} ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
  152. sed -i 's!ConditionPathExists=/dev/tty0!ConditionPathExists=|/dev/tty0\nConditionVirtualization=|lxc!' \
  153. ${rootfs_path}/lib/systemd/system/getty\@.service
  154. }
  155. install_openmandriva()
  156. {
  157. mkdir -p @LOCALSTATEDIR@/lock/subsys/
  158. (
  159. flock -x 9
  160. if [ $? -ne 0 ]; then
  161. echo "Cache repository is busy."
  162. return 1
  163. fi
  164. echo "Checking cache download in $cache/rootfs ... "
  165. if [ ! -e "$cache/rootfs" ]; then
  166. echo $cache/rootfs
  167. create_chroot_openmandriva
  168. if [ $? -ne 0 ]; then
  169. echo "Failed to download 'openmandriva basesystem-minimal'"
  170. return 1
  171. fi
  172. else
  173. echo "Cache found. Updating..."
  174. update_openmandriva
  175. if [ $? -ne 0 ]; then
  176. echo "Failed to update 'openmandriva base', continuing with last known good cache"
  177. else
  178. echo "Update finished"
  179. fi
  180. fi
  181. echo "Copy $cache/rootfs to $rootfs_path ... "
  182. copy_openmandriva
  183. if [ $? -ne 0 ]; then
  184. echo "Failed to copy rootfs"
  185. return 1
  186. fi
  187. return 0
  188. ) 9>@LOCALSTATEDIR@/lock/subsys/lxc-openmandriva
  189. return $?
  190. }
  191. copy_configuration()
  192. {
  193. mkdir -p $config_path
  194. grep -q "^lxc.rootfs" $config_path/config 2>/dev/null || echo "lxc.rootfs = $rootfs_path" >> $config_path/config
  195. cat <<EOF >> $config_path/config
  196. lxc.utsname = $name
  197. lxc.autodev = 1
  198. lxc.tty = 4
  199. lxc.pts = 1024
  200. lxc.mount = $config_path/fstab
  201. lxc.cap.drop = sys_module mac_admin mac_override sys_time
  202. # When using LXC with apparmor, uncomment the next line to run unconfined:
  203. #lxc.aa_profile = unconfined
  204. #networking
  205. lxc.network.type = $lxc_network_type
  206. lxc.network.flags = up
  207. lxc.network.link = $lxc_network_link
  208. lxc.network.name = eth0
  209. lxc.network.mtu = 1500
  210. EOF
  211. if [ ! -z ${ipv4} ]; then
  212. cat <<EOF >> $config_path/config
  213. lxc.network.ipv4 = $ipv4
  214. EOF
  215. fi
  216. if [ ! -z ${gw} ]; then
  217. cat <<EOF >> $config_path/config
  218. lxc.network.ipv4.gateway = $gw
  219. EOF
  220. fi
  221. if [ ! -z ${ipv6} ]; then
  222. cat <<EOF >> $config_path/config
  223. lxc.network.ipv6 = $ipv6
  224. EOF
  225. fi
  226. if [ ! -z ${gw6} ]; then
  227. cat <<EOF >> $config_path/config
  228. lxc.network.ipv6.gateway = $gw6
  229. EOF
  230. fi
  231. cat <<EOF >> $config_path/config
  232. #cgroups
  233. lxc.cgroup.devices.deny = a
  234. # /dev/null and zero
  235. lxc.cgroup.devices.allow = c 1:3 rwm
  236. lxc.cgroup.devices.allow = c 1:5 rwm
  237. # consoles
  238. lxc.cgroup.devices.allow = c 5:1 rwm
  239. lxc.cgroup.devices.allow = c 5:0 rwm
  240. lxc.cgroup.devices.allow = c 4:0 rwm
  241. lxc.cgroup.devices.allow = c 4:1 rwm
  242. # /dev/{,u}random
  243. lxc.cgroup.devices.allow = c 1:9 rwm
  244. lxc.cgroup.devices.allow = c 1:8 rwm
  245. lxc.cgroup.devices.allow = c 136:* rwm
  246. lxc.cgroup.devices.allow = c 5:2 rwm
  247. # rtc
  248. lxc.cgroup.devices.allow = c 10:135 rwm
  249. EOF
  250. cat <<EOF > $config_path/fstab
  251. proc $rootfs_path/proc proc nodev,noexec,nosuid 0 0
  252. sysfs $rootfs_path/sys sysfs defaults 0 0
  253. EOF
  254. if [ $? -ne 0 ]; then
  255. echo "Failed to add configuration"
  256. return 1
  257. fi
  258. return 0
  259. }
  260. clean()
  261. {
  262. if [ ! -e $cache ]; then
  263. exit 0
  264. fi
  265. # lock, so we won't purge while someone is creating a repository
  266. (
  267. flock -x 9
  268. if [ $? != 0 ]; then
  269. echo "Cache repository is busy."
  270. exit 1
  271. fi
  272. echo -n "Purging the download cache for OpenMandriva-$release..."
  273. rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
  274. exit 0
  275. ) 9>@LOCALSTATEDIR@/lock/subsys/lxc-openmandriva
  276. }
  277. usage()
  278. {
  279. cat <<EOF
  280. usage:
  281. $1 -n|--name=<container_name>
  282. [-p|--path=<path>] [-c|--clean] [-R|--release=<openmandriva2013.0/rosa2012.1/cooker/ release>]
  283. [-4|--ipv4=<ipv4 address>] [-6|--ipv6=<ipv6 address>]
  284. [-g|--gw=<gw address>] [-d|--dns=<dns address>]
  285. [-P|--profile=<name of the profile>] [--rootfs=<path>]
  286. [-A|--arch=<arch of the container>]
  287. [-h|--help]
  288. Mandatory args:
  289. -n,--name container name, used to as an identifier for that container from now on
  290. Optional args:
  291. -p,--path path to where the container rootfs will be created, defaults to @LXCPATH@. The container config will go under @LXCPATH@ in that case
  292. -c,--clean clean the cache
  293. -R,--release openmandriva2013.0/cooker/rosa2012.1 release for the new container. if the host is OpenMandriva, then it will default to the host's release.
  294. -4,--ipv4 specify the ipv4 address to assign to the virtualized interface, eg. 192.168.1.123/24
  295. -6,--ipv6 specify the ipv6 address to assign to the virtualized interface, eg. 2003:db8:1:0:214:1234:fe0b:3596/64
  296. -g,--gw specify the default gw, eg. 192.168.1.1
  297. -G,--gw6 specify the default gw, eg. 2003:db8:1:0:214:1234:fe0b:3596
  298. -d,--dns specify the DNS server, eg. 192.168.1.2
  299. -P,--profile Profile name is the file name in /etc/lxc/profiles contained packages name for install to cache.
  300. -A,--arch Define what arch the container will be [i586,x86_64,armv7l,armv7hl]
  301. ---rootfs rootfs path
  302. -h,--help print this help
  303. EOF
  304. return 0
  305. }
  306. options=$(getopt -o hp:n:P:cR:4:6:g:d:A -l help,rootfs:,path:,name:,profile:,clean:,release:,ipv4:,ipv6:,gw:,dns:,arch: -- "$@")
  307. if [ $? -ne 0 ]; then
  308. usage $(basename $0)
  309. exit 1
  310. fi
  311. eval set -- "$options"
  312. release=${release:-"cooker"}
  313. if [ -f /etc/lsb-release ]; then
  314. . /etc/lsb-release
  315. if [ "$DISTRIB_ID" = "OpenMandrivaLinux" ]; then
  316. release=openmandriva2013.0
  317. elif [ "$DISTRIB_ID" = "RosaDesktop.Fresh" ]; then
  318. release=rosa2012.1
  319. else
  320. echo "This is not an OpenMandriva or ROSA release"
  321. exit 1
  322. fi
  323. fi
  324. while true
  325. do
  326. case "$1" in
  327. -h|--help) usage $0 && exit 0;;
  328. -p|--path) path=$2; shift 2;;
  329. --rootfs) rootfs_path=$2; shift 2;;
  330. -n|--name) name=$2; shift 2;;
  331. -P|--profile) profile=$2; shift 2;;
  332. -c|--clean) clean=$2; shift 2;;
  333. -R|--release) release=$2; shift 2;;
  334. -a|--arch) arch=$2; shift 2;;
  335. -4|--ipv4) ipv4=$2; shift 2;;
  336. -6|--ipv6) ipv6=$2; shift 2;;
  337. -g|--gw) gw=$2; shift 2;;
  338. -d|--dns) dns=$2; shift 2;;
  339. --) shift 1; break ;;
  340. *) break ;;
  341. esac
  342. done
  343. arch=${arch:-$hostarch}
  344. if [ ! -z "$clean" -a -z "$path" ]; then
  345. clean || exit 1
  346. exit 0
  347. fi
  348. if [ -z "${utsname}" ]; then
  349. utsname=${name}
  350. fi
  351. type urpmi >/dev/null 2>&1
  352. if [ $? -ne 0 ]; then
  353. echo "'urpmi' command is missing"
  354. exit 1
  355. fi
  356. if [ -z "$path" ]; then
  357. path=$default_path
  358. fi
  359. if [ -z "$profile" ]; then
  360. profile=$default_profile
  361. fi
  362. if [ $hostarch = "i586" -a $arch = "x86_64" ]; then
  363. echo "can't create x86_64 container on i586"
  364. exit 1
  365. fi
  366. if [ -z "$ipv4" -a -z "$ipv6" ]; then
  367. BOOTPROTO="dhcp"
  368. else
  369. BOOTPROTO="static"
  370. fi
  371. if [ "$(id -u)" != "0" ]; then
  372. echo "This script should be run as 'root'"
  373. exit 1
  374. fi
  375. # check for 'lxc.rootfs' passed in through default config by lxc-create
  376. if [ -z "$rootfs_path" ]; then
  377. if grep -q '^lxc.rootfs' $path/config 2>/dev/null ; then
  378. rootfs_path=$(awk -F= '/^lxc.rootfs =/{ print $2 }' $path/config)
  379. else
  380. rootfs_path=$path/$name/rootfs
  381. fi
  382. fi
  383. config_path=$default_path/$name
  384. cache=$cache_base/$release/$arch/$profile
  385. if [ ! -f $config_path/config ]; then
  386. echo "A container with that name exists, chose a different name"
  387. exit 1
  388. fi
  389. install_openmandriva
  390. if [ $? -ne 0 ]; then
  391. echo "failed to install openmandriva"
  392. exit 1
  393. fi
  394. configure_openmandriva
  395. if [ $? -ne 0 ]; then
  396. echo "failed to configure openmandriva for a container"
  397. exit 1
  398. fi
  399. # If the systemd configuration directory exists - set it up for what we need.
  400. if [ -d ${rootfs_path}/etc/systemd/system ]
  401. then
  402. configure_openmandriva_systemd
  403. fi
  404. populate_dev
  405. if [ $? -ne 0 ]; then
  406. echo "failed to populated /dev/ devices"
  407. exit 1
  408. fi
  409. set_guest_root_password
  410. if [ $? -ne 0 ]; then
  411. echo "failed to configure password for chroot"
  412. exit 1
  413. fi
  414. copy_configuration
  415. if [ $? -ne 0 ]; then
  416. echo "failed write configuration file"
  417. exit 1
  418. fi
  419. if [ ! -z $clean ]; then
  420. clean || exit 1
  421. exit 0
  422. fi
  423. echo "container rootfs and config created"