~smoser/ubuntu/precise/lxc/btrfs-clone-support

« 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: 2011-09-14 15:07:25 UTC
  • Revision ID: package-import@ubuntu.com-20110914150725-sgai0vjn5s618szs
Tags: 0.7.5-0ubuntu8
* debian/patches/0007-fix-lxc-clone-hostname.patch: make sure $hostname
  is defined before it is first used.  Reported by Benjamin Saller.
  (LP: #850205)
* add missing ; at end of 'send hostname' in dhclient.conf (LP: #851274)

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"