~ubuntu-branches/ubuntu/quantal/lxc/quantal-201205292108

« back to all changes in this revision

Viewing changes to .pc/0007-fix-lxc-clone-hostname.patch/src/lxc/lxc-clone.in

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2012-04-26 15:18:35 UTC
  • mfrom: (3.1.41 sid)
  • Revision ID: package-import@ubuntu.com-20120426151835-3vz6kb4m90gb26js
Tags: 0.8.0~rc1-4ubuntu1
* Merge from unstable.  Remaining changes:
  - control:
    - update maintainer
    - Build-Depends: add dh-apparmor and libapparmor-dev
    - lxc Depends: add bridge-utils, dnsmasq-base, iptables, rsync
    - lxc Recommends: add cgroup-lite | cgroup-bin, openssl
    - lxc Suggests: add btrfs-tools, lvm2, qemu-user-static
    - lxc Conflicts: remove (cgroup-bin)
  - Add lxc-start-ephemeral and lxc-wait to debian/local
  - apparmor:
    - add lxc.apparmor, lxc-containers.apparmor,
      lxc-default.apparmor, and new lxc.apparmor.in
  - add debian/lxc.conf (default container creation config file)
  - debian/lxc.install.in:
    * add lxc-start-ephemeral
    * add debian/lxc.conf
    * skip lxc-debconf*
    * skip lxc-ls (Use upstream's)
  - debian/lxc*.install.in: use '*', not @DEB_HOST_MULTIARCH@
  - Use our own completely different lxc.postinst and lxc.postrm
  - remove lxc.templates
  - debian/rules:
    * add DEB_DH_INSTALLINIT_ARGS = --upstart-only
    * don't do debconf stuff
    * add debian/*.apparmor.in to files processed under
      override_dh_auto_clean
    * don't comment out ubuntu or busybox templates
    * do apparmor stuff and install our own lxc-wait under override_dh_install
    * install our upstart scripts in override_dh_installinit
  - add lxc.default, lxc.lxc-net.upstart, lxc.upstart under
    debian/

* patches kept:
  - 0013-lxc-create-use-default-config.patch (needed manual rebase)
  - 0030-ubuntu-template-fail.patch
  - 0031-ubuntu-template-resolvconf.patch
  - 0044-lxc-destroy-rm-autos
  - debian/patches/0045-fix-other-templates
  - debian/patches/0046-lxc-clone-change-hwaddr
  - debian/patches/0047-bindhome-check-shell
  - debian/patches/0049-ubuntu-template-sudo-and-cleanup
  - debian/patches/0050-clone-lvm-sizes
  - debian/patches/0052-ubuntu-bind-user-conflict
  - debian/patches/0053-lxc-start-pin-rootfs
  - debian/patches/0054-ubuntu-debug
  - debian/patches/0055-ubuntu-handle-badgrp
  - debian/patches/0056-dont-watch-utmp
  - debian/patches/0057-update-manpages
  - debian/patches/0058-fixup-ubuntu-cloud
  - debian/patches/0059-reenable-daily-cloudimg
  - debian/patches/0060-lxc-shutdown
  - debian/patches/0061-lxc-start-apparmor
  - debian/patches/0062-templates-relative-paths
  - debian/patches/0063-check-apparmor-enabled
  - debian/patches/0064-apparmor-mount-proc
  - debian/patches/0065-fix-bindhome-relpath
  - debian/patches/0066-confile-typo
  - debian/patches/0067-templates-lxc-profile
  - debian/patches/0068-fix-lxc-config-layout 
  - debian/patches/0069-ubuntu-cloud-fix
  - debian/patches/0070-templates-rmdir-dev-shm
  - debian/patches/0071-ubuntu-cloud-fix-image-extraction
  - debian/patches/0072-lxc-shutdown-help
  - debian/patches/0073-lxc-destroy-waits-before-destroy
  - mark all patches which have been forwarded as such, refresh all
* 0074-lxc-execute-find-init: lxc-init had moved.  Introduce a function in
  lxc-execute to go find it.  Otherwise lxc-execute for any older releases
  will fail.
* 0075-lxc-ls-bash: lxc-ls needs bash, not sh
* add debian/lxc.apparmor.in so DEB_HOST_MULTIARCH can be expanded
* 0076-fix-sprintfs:  - check return values for all sprintfs and snprintfs
  which could overflow (LP: #988918)
* 0077-execute-without-rootfs: let lxc-execute succeed with no rootfs
  (LP: #981955)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/bash
2
 
 
3
 
#
4
 
# lxc: linux Container library
5
 
 
6
 
# Authors:
7
 
# Serge Hallyn <serge.hallyn@ubuntu.com>
8
 
# Daniel Lezcano <daniel.lezcano@free.fr>
9
 
 
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
 
 
15
 
# This library is distributed in the hope that it will be useful,
16
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 
# Lesser General Public License for more details.
19
 
 
20
 
# You should have received a copy of the GNU Lesser General Public
21
 
# License along with this library; if not, write to the Free Software
22
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
 
 
24
 
usage() {
25
 
    echo "usage: lxc-clone -o <orig> -n <new> [-s] [-h] [-L fssize] [-v vgname]"
26
 
}
27
 
 
28
 
help() {
29
 
    usage
30
 
    echo
31
 
    echo "creates a lxc system object."
32
 
    echo
33
 
    echo "Options:"
34
 
    echo "orig        : name of the original container"
35
 
    echo "new         : name of the new container"
36
 
    echo "-s          : make the new rootfs a snapshot of the original"
37
 
    echo "fssize      : size if creating a new fs.  By default, 2G"
38
 
    echo "vgname      : lvm volume group name, lxc by default"
39
 
}
40
 
 
41
 
shortoptions='ho:n:sL:v:'
42
 
longoptions='help,orig:,name:,snapshot,fssize,vgname'
43
 
lxc_path=/var/lib/lxc
44
 
bindir=/usr/bin
45
 
snapshot=no
46
 
lxc_size=2G
47
 
lxc_vg=lxc
48
 
 
49
 
getopt=$(getopt -o $shortoptions --longoptions  $longoptions -- "$@")
50
 
if [ $? != 0 ]; then
51
 
    usage
52
 
    exit 1;
53
 
fi
54
 
 
55
 
eval set -- "$getopt"
56
 
 
57
 
while true; do
58
 
        case "$1" in
59
 
            -h|--help)
60
 
                help
61
 
                exit 1
62
 
                ;;
63
 
            -s|--snapshot)
64
 
                shift
65
 
                snapshot=yes
66
 
                ;;
67
 
            -o|--orig)
68
 
                shift
69
 
                lxc_orig=$1
70
 
                shift
71
 
                ;;
72
 
            -L|--fssize)
73
 
                shift
74
 
                lxc_size=$1
75
 
                shift
76
 
                ;;
77
 
            -v|--vgname)
78
 
                shift
79
 
                lxc_vg=$1
80
 
                shift
81
 
                ;;
82
 
            -n|--new)
83
 
                shift
84
 
                lxc_new=$1
85
 
                shift
86
 
                ;;
87
 
            --)
88
 
                shift
89
 
                break;;
90
 
            *)
91
 
                echo $1
92
 
                usage
93
 
                exit 1
94
 
                ;;
95
 
        esac
96
 
done
97
 
 
98
 
if [ -z "$lxc_path" ]; then
99
 
    echo "no configuration path defined !"
100
 
    exit 1
101
 
fi
102
 
 
103
 
if [ ! -r $lxc_path ]; then
104
 
    echo "configuration path '$lxc_path' not found"
105
 
    exit 1
106
 
fi
107
 
 
108
 
if [ -z "$lxc_orig" ]; then
109
 
    echo "no original container name specified"
110
 
    usage
111
 
    exit 1
112
 
fi
113
 
 
114
 
if [ -z "$lxc_new" ]; then
115
 
    echo "no new container name specified"
116
 
    usage
117
 
    exit 1
118
 
fi
119
 
 
120
 
if [ "$(id -u)" != "0" ]; then
121
 
   echo "This command has to be run as root"
122
 
   exit 1
123
 
fi
124
 
 
125
 
if [ ! -r $lxc_path ]; then
126
 
    echo "no configuration path defined !"
127
 
    exit 1
128
 
fi
129
 
 
130
 
if [ ! -d "$lxc_path/$lxc_orig" ]; then
131
 
    echo "'$lxc_orig' does not exist"
132
 
    exit 1
133
 
fi
134
 
 
135
 
if [ -d "$lxc_path/$lxc_new" ]; then
136
 
    echo "'$lxc_new' already exists"
137
 
    exit 1
138
 
fi
139
 
 
140
 
trap "${bindir}/lxc-destroy -n $lxc_new; echo aborted; exit 1" SIGHUP SIGINT SIGTERM
141
 
 
142
 
mkdir -p $lxc_path/$lxc_new
143
 
 
144
 
echo "Tweaking configuration"
145
 
cp $lxc_path/$lxc_orig/config $lxc_path/$lxc_new/config
146
 
sed -i '/lxc.utsname/d' $lxc_path/$lxc_new/config
147
 
echo "lxc.utsname = $hostname" >> $lxc_path/$lxc_new/config
148
 
 
149
 
sed -i '/lxc.mount/d' $lxc_path/$lxc_new/config
150
 
echo "lxc.mount = $lxc_path/$lxc_new/fstab" >> $lxc_path/$lxc_new/config
151
 
 
152
 
cp $lxc_path/$lxc_orig/fstab $lxc_path/$lxc_new/fstab
153
 
sed -i "s@$lxc_path/$lxc_orig@$lxc_path/$lxc_new@" $lxc_path/$lxc_new/fstab
154
 
 
155
 
echo "Copying rootfs..."
156
 
rootfs=$lxc_path/$lxc_new/rootfs
157
 
# First figure out if the old is a device.  For now we only support
158
 
# lvm devices.
159
 
mounted=0
160
 
sed -i '/lxc.rootfs/d' $lxc_path/$lxc_new/config
161
 
oldroot=`grep lxc.rootfs $lxc_path/$lxc_orig/config | awk -F= '{ print $2 '}`
162
 
if [ -b $oldroot ]; then
163
 
        # this is a device.  If we don't want to snapshot, then mkfs, mount
164
 
        # and rsync.  Trivial but not yet implemented
165
 
        if [ $snapshot == "no" ]; then
166
 
                echo "non-snapshot and non-lvm clone of block device not yet implemented"
167
 
                exit 1
168
 
        fi
169
 
        lvdisplay $oldroot > /dev/null 2>&1
170
 
        if [ $? -ne 0 ]; then
171
 
                echo "non-snapshot and non-lvm clone of block device not yet implemented"
172
 
                exit 1
173
 
        fi
174
 
        # ok, create a snapshot of the lvm device
175
 
        lvcreate -s -L $lxc_size -n $lxc_new /dev/$lxc_vg/$lxc_orig || exit 1
176
 
        echo "lxc.rootfs = /dev/$lxc_vg/$lxc_new" >> $lxc_path/$lxc_new/config
177
 
        # and mount it so we can tweak it
178
 
        mkdir -p $lxc_path/$lxc_new/rootfs
179
 
        mount /dev/$lxc_vg/$lxc_new $rootfs || { echo "failed to mount new rootfs"; exit 1; }
180
 
        mounted=1
181
 
else
182
 
        cp -a $lxc_path/$lxc_orig/rootfs $lxc_path/$lxc_new/rootfs || return 1
183
 
        echo "lxc.rootfs = $rootfs" >> $lxc_path/$lxc_new/config
184
 
fi
185
 
 
186
 
echo "Updating rootfs..."
187
 
hostname=$lxc_new
188
 
 
189
 
# so you can 'ssh $hostname.' or 'ssh $hostname.local'
190
 
if [ -f $rootfs/etc/dhcp/dhclient.conf ]; then
191
 
        sed -i "s/send host-name.*$/send host-name $hostname/" $rootfs/etc/dhcp/dhclient.conf
192
 
fi
193
 
 
194
 
# set the hostname
195
 
cat <<EOF > $rootfs/etc/hostname
196
 
$hostname
197
 
EOF
198
 
# set minimal hosts
199
 
cat <<EOF > $rootfs/etc/hosts
200
 
127.0.0.1 localhost $hostname
201
 
EOF
202
 
 
203
 
# if this was a block device, then umount it now
204
 
if [ $mounted -eq 1 ]; then
205
 
        umount $rootfs
206
 
fi
207
 
 
208
 
echo "'$lxc_new' created"