| 1 | #!/bin/bash |
|---|
| 2 | |
|---|
| 3 | # |
|---|
| 4 | # template script for generating fedora container for LXC |
|---|
| 5 | # |
|---|
| 6 | |
|---|
| 7 | # |
|---|
| 8 | # lxc: linux Container library |
|---|
| 9 | |
|---|
| 10 | # Authors: |
|---|
| 11 | # Daniel Lezcano <daniel.lezcano@free.fr> |
|---|
| 12 | # Ramez Hanna <rhanna@informatiq.org> |
|---|
| 13 | |
|---|
| 14 | # This library is free software; you can redistribute it and/or |
|---|
| 15 | # modify it under the terms of the GNU Lesser General Public |
|---|
| 16 | # License as published by the Free Software Foundation; either |
|---|
| 17 | # version 2.1 of the License, or (at your option) any later version. |
|---|
| 18 | |
|---|
| 19 | # This library is distributed in the hope that it will be useful, |
|---|
| 20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 22 | # Lesser General Public License for more details. |
|---|
| 23 | |
|---|
| 24 | # You should have received a copy of the GNU Lesser General Public |
|---|
| 25 | # License along with this library; if not, write to the Free Software |
|---|
| 26 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 27 | |
|---|
| 28 | #Configurations |
|---|
| 29 | arch=$(arch) |
|---|
| 30 | cache_base=/var/cache/lxc/fedora/$arch |
|---|
| 31 | default_path=/var/lib/lxc |
|---|
| 32 | root_password=root |
|---|
| 33 | |
|---|
| 34 | # is this fedora? |
|---|
| 35 | [ -f /etc/fedora-release ] && is_fedora=true |
|---|
| 36 | |
|---|
| 37 | if [ "$arch" = "i686" ]; then |
|---|
| 38 | arch=i386 |
|---|
| 39 | fi |
|---|
| 40 | |
|---|
| 41 | configure_fedora() |
|---|
| 42 | { |
|---|
| 43 | |
|---|
| 44 | # disable selinux in fedora |
|---|
| 45 | mkdir -p $rootfs_path/selinux |
|---|
| 46 | echo 0 > $rootfs_path/selinux/enforce |
|---|
| 47 | |
|---|
| 48 | # configure the network using the dhcp |
|---|
| 49 | cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-eth0 |
|---|
| 50 | DEVICE=eth0 |
|---|
| 51 | BOOTPROTO=dhcp |
|---|
| 52 | ONBOOT=yes |
|---|
| 53 | HOSTNAME=${name} |
|---|
| 54 | NM_CONTROLLED=no |
|---|
| 55 | TYPE=Ethernet |
|---|
| 56 | MTU=${MTU} |
|---|
| 57 | EOF |
|---|
| 58 | |
|---|
| 59 | # set the hostname |
|---|
| 60 | cat <<EOF > ${rootfs_path}/etc/sysconfig/network |
|---|
| 61 | NETWORKING=yes |
|---|
| 62 | HOSTNAME=${name} |
|---|
| 63 | EOF |
|---|
| 64 | |
|---|
| 65 | # set minimal hosts |
|---|
| 66 | cat <<EOF > $rootfs_path/etc/hosts |
|---|
| 67 | 127.0.0.1 localhost $name |
|---|
| 68 | EOF |
|---|
| 69 | |
|---|
| 70 | sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.sysinit |
|---|
| 71 | sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.d/rc.sysinit |
|---|
| 72 | # don't mount devpts, for pete's sake |
|---|
| 73 | sed -i 's/^.*dev.pts.*$/#\0/' ${rootfs_path}/etc/rc.sysinit |
|---|
| 74 | sed -i 's/^.*dev.pts.*$/#\0/' ${rootfs_path}/etc/rc.d/rc.sysinit |
|---|
| 75 | |
|---|
| 76 | chroot ${rootfs_path} chkconfig udev-post off |
|---|
| 77 | chroot ${rootfs_path} chkconfig network on |
|---|
| 78 | |
|---|
| 79 | dev_path="${rootfs_path}/dev" |
|---|
| 80 | rm -rf $dev_path |
|---|
| 81 | mkdir -p $dev_path |
|---|
| 82 | mknod -m 666 ${dev_path}/null c 1 3 |
|---|
| 83 | mknod -m 666 ${dev_path}/zero c 1 5 |
|---|
| 84 | mknod -m 666 ${dev_path}/random c 1 8 |
|---|
| 85 | mknod -m 666 ${dev_path}/urandom c 1 9 |
|---|
| 86 | mkdir -m 755 ${dev_path}/pts |
|---|
| 87 | mkdir -m 1777 ${dev_path}/shm |
|---|
| 88 | mknod -m 666 ${dev_path}/tty c 5 0 |
|---|
| 89 | mknod -m 666 ${dev_path}/tty0 c 4 0 |
|---|
| 90 | mknod -m 666 ${dev_path}/tty1 c 4 1 |
|---|
| 91 | mknod -m 666 ${dev_path}/tty2 c 4 2 |
|---|
| 92 | mknod -m 666 ${dev_path}/tty3 c 4 3 |
|---|
| 93 | mknod -m 666 ${dev_path}/tty4 c 4 4 |
|---|
| 94 | mknod -m 600 ${dev_path}/console c 5 1 |
|---|
| 95 | mknod -m 666 ${dev_path}/full c 1 7 |
|---|
| 96 | mknod -m 600 ${dev_path}/initctl p |
|---|
| 97 | mknod -m 666 ${dev_path}/ptmx c 5 2 |
|---|
| 98 | |
|---|
| 99 | echo "setting root passwd to $root_password" |
|---|
| 100 | echo "root:$root_password" | chroot $rootfs_path chpasswd |
|---|
| 101 | |
|---|
| 102 | # specifying this in the initial packages doesn't always work. |
|---|
| 103 | echo "installing fedora-release package" |
|---|
| 104 | chroot ${rootfs_path} yum --releasever=${release} -y install fedora-release |
|---|
| 105 | |
|---|
| 106 | # silence some needless startup errors |
|---|
| 107 | touch ${rootfs_path}/etc/fstab |
|---|
| 108 | |
|---|
| 109 | # give us a console on /dev/console |
|---|
| 110 | sed -i 's/ACTIVE_CONSOLES=.*$/ACTIVE_CONSOLES="\/dev\/console \/dev\/tty[1-4]"/' \ |
|---|
| 111 | ${rootfs_path}/etc/sysconfig/init |
|---|
| 112 | |
|---|
| 113 | return 0 |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | download_fedora() |
|---|
| 117 | { |
|---|
| 118 | |
|---|
| 119 | # check the mini fedora was not already downloaded |
|---|
| 120 | INSTALL_ROOT=$cache/partial |
|---|
| 121 | mkdir -p $INSTALL_ROOT |
|---|
| 122 | if [ $? -ne 0 ]; then |
|---|
| 123 | echo "Failed to create '$INSTALL_ROOT' directory" |
|---|
| 124 | return 1 |
|---|
| 125 | fi |
|---|
| 126 | |
|---|
| 127 | # download a mini fedora into a cache |
|---|
| 128 | echo "Downloading fedora minimal ..." |
|---|
| 129 | YUM="yum --installroot $INSTALL_ROOT -y --nogpgcheck" |
|---|
| 130 | PKG_LIST="yum initscripts passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils fedora-release" |
|---|
| 131 | MIRRORLIST_URL="http://mirrors.fedoraproject.org/mirrorlist?repo=fedora-$release&arch=$arch" |
|---|
| 132 | |
|---|
| 133 | DOWNLOAD_OK=no |
|---|
| 134 | for trynumber in 1 2 3; do |
|---|
| 135 | [ $trynumber != 1 ] && echo "Trying again..." |
|---|
| 136 | MIRROR_URL=$(curl -s -S -f "$MIRRORLIST_URL" | head -n2 | tail -n1) |
|---|
| 137 | if [ $? -ne 0 ] || [ -z "$MIRROR_URL" ]; then |
|---|
| 138 | echo "Failed to get a mirror" |
|---|
| 139 | continue |
|---|
| 140 | fi |
|---|
| 141 | RELEASE_URL="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm" |
|---|
| 142 | echo "Fetching from $RELEASE_URL" |
|---|
| 143 | curl -f "$RELEASE_URL" > $INSTALL_ROOT/fedora-release-$release.noarch.rpm |
|---|
| 144 | if [ $? -ne 0 ]; then |
|---|
| 145 | echo "Failed to download fedora release rpm" |
|---|
| 146 | continue |
|---|
| 147 | fi |
|---|
| 148 | DOWNLOAD_OK=yes |
|---|
| 149 | break |
|---|
| 150 | done |
|---|
| 151 | if [ $DOWNLOAD_OK != yes ]; then |
|---|
| 152 | echo "Aborting" |
|---|
| 153 | return 1 |
|---|
| 154 | fi |
|---|
| 155 | |
|---|
| 156 | mkdir -p $INSTALL_ROOT/var/lib/rpm |
|---|
| 157 | rpm --root $INSTALL_ROOT --initdb |
|---|
| 158 | rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm |
|---|
| 159 | $YUM install $PKG_LIST |
|---|
| 160 | |
|---|
| 161 | if [ $? -ne 0 ]; then |
|---|
| 162 | echo "Failed to download the rootfs, aborting." |
|---|
| 163 | return 1 |
|---|
| 164 | fi |
|---|
| 165 | |
|---|
| 166 | mv "$INSTALL_ROOT" "$cache/rootfs" |
|---|
| 167 | echo "Download complete." |
|---|
| 168 | |
|---|
| 169 | return 0 |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | copy_fedora() |
|---|
| 173 | { |
|---|
| 174 | |
|---|
| 175 | # make a local copy of the minifedora |
|---|
| 176 | echo -n "Copying rootfs to $rootfs_path ..." |
|---|
| 177 | #cp -a $cache/rootfs-$arch $rootfs_path || return 1 |
|---|
| 178 | # i prefer rsync (no reason really) |
|---|
| 179 | mkdir -p $rootfs_path |
|---|
| 180 | rsync -a $cache/rootfs/ $rootfs_path/ |
|---|
| 181 | return 0 |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | update_fedora() |
|---|
| 185 | { |
|---|
| 186 | chroot $cache/rootfs yum -y update |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | install_fedora() |
|---|
| 190 | { |
|---|
| 191 | mkdir -p /var/lock/subsys/ |
|---|
| 192 | ( |
|---|
| 193 | flock -x 200 |
|---|
| 194 | if [ $? -ne 0 ]; then |
|---|
| 195 | echo "Cache repository is busy." |
|---|
| 196 | return 1 |
|---|
| 197 | fi |
|---|
| 198 | |
|---|
| 199 | echo "Checking cache download in $cache/rootfs ... " |
|---|
| 200 | if [ ! -e "$cache/rootfs" ]; then |
|---|
| 201 | download_fedora |
|---|
| 202 | if [ $? -ne 0 ]; then |
|---|
| 203 | echo "Failed to download 'fedora base'" |
|---|
| 204 | return 1 |
|---|
| 205 | fi |
|---|
| 206 | else |
|---|
| 207 | echo "Cache found. Updating..." |
|---|
| 208 | update_fedora |
|---|
| 209 | if [ $? -ne 0 ]; then |
|---|
| 210 | echo "Failed to update 'fedora base', continuing with last known good cache" |
|---|
| 211 | else |
|---|
| 212 | echo "Update finished" |
|---|
| 213 | fi |
|---|
| 214 | fi |
|---|
| 215 | |
|---|
| 216 | echo "Copy $cache/rootfs to $rootfs_path ... " |
|---|
| 217 | copy_fedora |
|---|
| 218 | if [ $? -ne 0 ]; then |
|---|
| 219 | echo "Failed to copy rootfs" |
|---|
| 220 | return 1 |
|---|
| 221 | fi |
|---|
| 222 | |
|---|
| 223 | return 0 |
|---|
| 224 | |
|---|
| 225 | ) 200>/var/lock/subsys/lxc |
|---|
| 226 | |
|---|
| 227 | return $? |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | copy_configuration() |
|---|
| 231 | { |
|---|
| 232 | |
|---|
| 233 | mkdir -p $config_path |
|---|
| 234 | cat <<EOF >> $config_path/config |
|---|
| 235 | lxc.utsname = $name |
|---|
| 236 | lxc.tty = 4 |
|---|
| 237 | lxc.pts = 1024 |
|---|
| 238 | lxc.rootfs = $rootfs_path |
|---|
| 239 | lxc.mount = $config_path/fstab |
|---|
| 240 | |
|---|
| 241 | # uncomment the next line to run the container unconfined: |
|---|
| 242 | #lxc.aa_profile = unconfined |
|---|
| 243 | |
|---|
| 244 | #cgroups |
|---|
| 245 | lxc.cgroup.devices.deny = a |
|---|
| 246 | # /dev/null and zero |
|---|
| 247 | lxc.cgroup.devices.allow = c 1:3 rwm |
|---|
| 248 | lxc.cgroup.devices.allow = c 1:5 rwm |
|---|
| 249 | # consoles |
|---|
| 250 | lxc.cgroup.devices.allow = c 5:1 rwm |
|---|
| 251 | lxc.cgroup.devices.allow = c 5:0 rwm |
|---|
| 252 | lxc.cgroup.devices.allow = c 4:0 rwm |
|---|
| 253 | lxc.cgroup.devices.allow = c 4:1 rwm |
|---|
| 254 | # /dev/{,u}random |
|---|
| 255 | lxc.cgroup.devices.allow = c 1:9 rwm |
|---|
| 256 | lxc.cgroup.devices.allow = c 1:8 rwm |
|---|
| 257 | lxc.cgroup.devices.allow = c 136:* rwm |
|---|
| 258 | lxc.cgroup.devices.allow = c 5:2 rwm |
|---|
| 259 | # rtc |
|---|
| 260 | lxc.cgroup.devices.allow = c 254:0 rwm |
|---|
| 261 | EOF |
|---|
| 262 | |
|---|
| 263 | cat <<EOF > $config_path/fstab |
|---|
| 264 | proc proc proc nodev,noexec,nosuid 0 0 |
|---|
| 265 | sysfs sys sysfs defaults 0 0 |
|---|
| 266 | EOF |
|---|
| 267 | if [ $? -ne 0 ]; then |
|---|
| 268 | echo "Failed to add configuration" |
|---|
| 269 | return 1 |
|---|
| 270 | fi |
|---|
| 271 | |
|---|
| 272 | return 0 |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | clean() |
|---|
| 276 | { |
|---|
| 277 | |
|---|
| 278 | if [ ! -e $cache ]; then |
|---|
| 279 | exit 0 |
|---|
| 280 | fi |
|---|
| 281 | |
|---|
| 282 | # lock, so we won't purge while someone is creating a repository |
|---|
| 283 | ( |
|---|
| 284 | flock -x 200 |
|---|
| 285 | if [ $? != 0 ]; then |
|---|
| 286 | echo "Cache repository is busy." |
|---|
| 287 | exit 1 |
|---|
| 288 | fi |
|---|
| 289 | |
|---|
| 290 | echo -n "Purging the download cache for Fedora-$release..." |
|---|
| 291 | rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1 |
|---|
| 292 | exit 0 |
|---|
| 293 | |
|---|
| 294 | ) 200>/var/lock/subsys/lxc |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | usage() |
|---|
| 298 | { |
|---|
| 299 | cat <<EOF |
|---|
| 300 | usage: |
|---|
| 301 | $1 -n|--name=<container_name> |
|---|
| 302 | [-p|--path=<path>] [-c|--clean] [-R|--release=<Fedora_release>] [-A|--arch=<arch of the container>] |
|---|
| 303 | [-h|--help] |
|---|
| 304 | Mandatory args: |
|---|
| 305 | -n,--name container name, used to as an identifier for that container from now on |
|---|
| 306 | Optional args: |
|---|
| 307 | -p,--path path to where the container rootfs will be created, defaults to /var/lib/lxc. The container config will go under /var/lib/lxc in that case |
|---|
| 308 | -c,--clean clean the cache |
|---|
| 309 | -R,--release Fedora release for the new container. if the host is Fedora, then it will defaultto the host's release. |
|---|
| 310 | -A,--arch NOT USED YET. Define what arch the container will be [i686,x86_64] |
|---|
| 311 | -h,--help print this help |
|---|
| 312 | EOF |
|---|
| 313 | return 0 |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | options=$(getopt -o hp:n:cR: -l help,path:,name:,clean,release: -- "$@") |
|---|
| 317 | if [ $? -ne 0 ]; then |
|---|
| 318 | usage $(basename $0) |
|---|
| 319 | exit 1 |
|---|
| 320 | fi |
|---|
| 321 | eval set -- "$options" |
|---|
| 322 | |
|---|
| 323 | while true |
|---|
| 324 | do |
|---|
| 325 | case "$1" in |
|---|
| 326 | -h|--help) usage $0 && exit 0;; |
|---|
| 327 | -p|--path) path=$2; shift 2;; |
|---|
| 328 | -n|--name) name=$2; shift 2;; |
|---|
| 329 | -c|--clean) clean=$2; shift 2;; |
|---|
| 330 | -R|--release) release=$2; shift 2;; |
|---|
| 331 | --) shift 1; break ;; |
|---|
| 332 | *) break ;; |
|---|
| 333 | esac |
|---|
| 334 | done |
|---|
| 335 | |
|---|
| 336 | if [ ! -z "$clean" -a -z "$path" ]; then |
|---|
| 337 | clean || exit 1 |
|---|
| 338 | exit 0 |
|---|
| 339 | fi |
|---|
| 340 | |
|---|
| 341 | needed_pkgs="" |
|---|
| 342 | type yum >/dev/null 2>&1 |
|---|
| 343 | if [ $? -ne 0 ]; then |
|---|
| 344 | needed_pkgs="yum $needed_pkgs" |
|---|
| 345 | fi |
|---|
| 346 | |
|---|
| 347 | type curl >/dev/null 2>&1 |
|---|
| 348 | if [ $? -ne 0 ]; then |
|---|
| 349 | needed_pkgs="curl $needed_pkgs" |
|---|
| 350 | fi |
|---|
| 351 | |
|---|
| 352 | if [ -n "$needed_pkgs" ]; then |
|---|
| 353 | echo "Missing commands: $needed_pkgs" |
|---|
| 354 | echo "Please install these using \"sudo apt-get install $needed_pkgs\"" |
|---|
| 355 | exit 1 |
|---|
| 356 | fi |
|---|
| 357 | |
|---|
| 358 | if [ -z "$path" ]; then |
|---|
| 359 | path=$default_path |
|---|
| 360 | fi |
|---|
| 361 | |
|---|
| 362 | if [ -z "$release" ]; then |
|---|
| 363 | if [ "$is_fedora" ]; then |
|---|
| 364 | release=$(cat /etc/fedora-release |awk '/^Fedora/ {print $3}') |
|---|
| 365 | else |
|---|
| 366 | echo "This is not a fedora host and release missing, defaulting to 14. use -R|--release to specify release" |
|---|
| 367 | release=14 |
|---|
| 368 | fi |
|---|
| 369 | fi |
|---|
| 370 | |
|---|
| 371 | if [ "$(id -u)" != "0" ]; then |
|---|
| 372 | echo "This script should be run as 'root'" |
|---|
| 373 | exit 1 |
|---|
| 374 | fi |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | rootfs_path=$path/rootfs |
|---|
| 378 | config_path=$default_path/$name |
|---|
| 379 | cache=$cache_base/$release |
|---|
| 380 | |
|---|
| 381 | revert() |
|---|
| 382 | { |
|---|
| 383 | echo "Interrupted, so cleaning up" |
|---|
| 384 | lxc-destroy -n $name |
|---|
| 385 | # maybe was interrupted before copy config |
|---|
| 386 | rm -rf $path/$name |
|---|
| 387 | rm -rf $default_path/$name |
|---|
| 388 | echo "exiting..." |
|---|
| 389 | exit 1 |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | trap revert SIGHUP SIGINT SIGTERM |
|---|
| 393 | |
|---|
| 394 | copy_configuration |
|---|
| 395 | if [ $? -ne 0 ]; then |
|---|
| 396 | echo "failed write configuration file" |
|---|
| 397 | exit 1 |
|---|
| 398 | fi |
|---|
| 399 | |
|---|
| 400 | install_fedora |
|---|
| 401 | if [ $? -ne 0 ]; then |
|---|
| 402 | echo "failed to install fedora" |
|---|
| 403 | exit 1 |
|---|
| 404 | fi |
|---|
| 405 | |
|---|
| 406 | configure_fedora |
|---|
| 407 | if [ $? -ne 0 ]; then |
|---|
| 408 | echo "failed to configure fedora for a container" |
|---|
| 409 | exit 1 |
|---|
| 410 | fi |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | if [ ! -z $clean ]; then |
|---|
| 414 | clean || exit 1 |
|---|
| 415 | exit 0 |
|---|
| 416 | fi |
|---|
| 417 | echo "container rootfs and config created" |
|---|