~ubuntu-branches/ubuntu/quantal/lxc/quantal-201208232243

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/bash

#
# lxc: linux Container library

# Authors:
# Serge Hallyn <serge.hallyn@ubuntu.com>
# Daniel Lezcano <daniel.lezcano@free.fr>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

set -e

usage() {
    echo "usage: lxc-clone -o <orig> -n <new> [-s] [-h] [-L fssize] [-v vgname] [-p lxc_lv_prefix] [-t fstype]"
}

help() {
    usage
    echo
    echo "creates a lxc system object."
    echo
    echo "Options:"
    echo "orig        : name of the original container"
    echo "new         : name of the new container"
    echo "-s          : make the new rootfs a snapshot of the original"
    echo "fssize      : size if creating a new fs.  By default, 2G"
    echo "vgname      : lvm volume group name, lxc by default"
    echo "lvprefix    : lvm volume name prefix, none by default, e.g. --lvprefix=lxc_ then new lxc lv name will be lxc_newname"
    echo "fstype      : new container file system type, ext3 by default (only works for non-snapshot lvm)"
}

shortoptions='ho:n:sL:v:p:t:'
longoptions='help,orig:,name:,snapshot,fssize:,vgname:,lvprefix:,fstype:'
localstatedir=@LOCALSTATEDIR@
lxc_path=@LXCPATH@
bindir=@BINDIR@
snapshot=no
lxc_defsize=2G
lxc_size=_unset
lxc_vg=lxc
lxc_lv_prefix=""
fstype=ext3

getopt=$(getopt -o $shortoptions --longoptions  $longoptions -- "$@")
if [ $? != 0 ]; then
    usage
    exit 1;
fi

eval set -- "$getopt"

while true; do
        case "$1" in
            -h|--help)
            help
            exit 1
            ;;
        -s|--snapshot)
            shift
            snapshot=yes
            snapshot_opt="-s"
            ;;
        -o|--orig)
            shift
            lxc_orig=$1
            shift
            ;;
        -L|--fssize)
            shift
            lxc_size=$1
            shift
            ;;
        -v|--vgname)
            shift
            lxc_vg=$1
            shift
            ;;
        -n|--new)
            shift
            lxc_new=$1
            shift
            ;;
        -p|--lvprefix)
            shift
            lxc_lv_prefix=$1
            shift
            ;;
        --)
            shift
            break
            ;;
        *)
            echo $1
            usage
            exit 1
            ;;
        esac
done

if [ -z "$lxc_path" ]; then
    echo "no configuration path defined !"
    exit 1
fi

if [ ! -r $lxc_path ]; then
    echo "configuration path '$lxc_path' not found"
    exit 1
fi

if [ -z "$lxc_orig" ]; then
    echo "no original container name specified"
    usage
    exit 1
fi

if [ -z "$lxc_new" ]; then
    echo "no new container name specified"
    usage
    exit 1
fi

if [ "$(id -u)" != "0" ]; then
   echo "This command has to be run as root"
   exit 1
fi

if [ ! -r $lxc_path ]; then
    echo "no configuration path defined !"
    exit 1
fi

if [ ! -d "$lxc_path/$lxc_orig" ]; then
    echo "'$lxc_orig' does not exist"
    exit 1
fi

if [ -d "$lxc_path/$lxc_new" ]; then
    echo "'$lxc_new' already exists"
    exit 1
fi

mounted=0
frozen=0
oldroot=`grep lxc.rootfs $lxc_path/$lxc_orig/config | awk -F= '{ print $2 '}`

cleanup() {
    if [ -b $oldroot ]; then
        if [ $mounted -eq 1 ]; then
            umount $rootfs || true
        fi
        lvremove -f $rootdev || true
    fi
    ${bindir}/lxc-destroy -n $lxc_new || true
    if [ $frozen -eq 1 ]; then
        lxc-unfreeze -n $lxc_orig
    fi
    echo aborted
    exit 1
}
trap cleanup HUP INT TERM

mkdir -p $lxc_path/$lxc_new
hostname=$lxc_new

echo "Tweaking configuration"
cp $lxc_path/$lxc_orig/config $lxc_path/$lxc_new/config
sed -i '/lxc.utsname/d' $lxc_path/$lxc_new/config
echo "lxc.utsname = $hostname" >> $lxc_path/$lxc_new/config

grep "lxc.mount[ \t]" $lxc_path/$lxc_new/config >/dev/null 2>&1 && { sed -i '/lxc.mount[ \t]/d' $lxc_path/$lxc_new/config; echo "lxc.mount = $lxc_path/$lxc_new/fstab" >> $lxc_path/$lxc_new/config; }

if [ -e  $lxc_path/$lxc_orig/fstab ];then
    cp $lxc_path/$lxc_orig/fstab $lxc_path/$lxc_new/fstab
    sed -i "s@$lxc_path/$lxc_orig@$lxc_path/$lxc_new@" $lxc_path/$lxc_new/fstab
fi

echo "Copying rootfs..."
rootfs=$lxc_path/$lxc_new/rootfs

container_running=True
lxc-info -s -n $lxc_orig|grep RUNNING >/dev/null 2>&1 || container_running=False

sed -i '/lxc.rootfs/d' $lxc_path/$lxc_new/config
oldroot=`grep lxc.rootfs $lxc_path/$lxc_orig/config | awk -F= '{ print $2 '}`
if [ -b $oldroot ]; then
    type vgscan || { echo "Please install lvm"; false; }
    lvdisplay $oldroot > /dev/null 2>&1 || { echo "non-lvm blockdev cloning not supported"; false; }
    lvm=TRUE
    # ok, create a snapshot of the lvm device
    if [ $container_running = "True" ]; then
        lxc-freeze -n $lxc_orig
        frozen=1
    fi
    if [ $lxc_size = "_unset" ]; then
        lxc_size=`lvdisplay $oldroot | grep Size | awk '{ print $3 $4 }'`
    fi
    newlv="${lxc_lv_prefix}${lxc_new}_snapshot"
    lvcreate -s -L $lxc_size -n $newlv $oldroot
    type xfs_admin > /dev/null 2>&1 && {
        # change filesystem UUID if it is an xfs filesystem
        xfs_admin -u /dev/$lxc_vg/$newlv && xfs_admin -U generate /dev/$lxc_vg/$newlv
    }
	     
    if [ $container_running = "True" ]; then
        lxc-unfreeze -n $lxc_orig
        frozen=0
    fi
    if [ $snapshot = "no" ]; then
        #mount snapshot
        mkdir -p ${rootfs}_snapshot
        mount /dev/$lxc_vg/${lxc_lv_prefix}${lxc_new}_snapshot ${rootfs}_snapshot || { echo "failed to mount new rootfs_snapshot"; false; }
        #create a new lv
        lvcreate -L $lxc_size $lxc_vg -n ${lxc_lv_prefix}$lxc_new
        echo "lxc.rootfs = /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new" >> $lxc_path/$lxc_new/config
        # and mount it so we can tweak it
        mkdir -p $lxc_path/$lxc_new/rootfs
        mkfs -t $fstype /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new
        mount /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new $rootfs || { echo "failed to mount new rootfs"; false; }
        mounted=1
        rsync -ax ${rootfs}_snapshot/ ${rootfs}/ || { echo "copy of data to new lv failed"; false; }
        umount ${rootfs}_snapshot
        rmdir ${rootfs}_snapshot
        lvremove -f $lxc_vg/${lxc_lv_prefix}${lxc_new}_snapshot
    else
        lvrename $lxc_vg/${lxc_lv_prefix}${lxc_new}_snapshot $lxc_vg/${lxc_lv_prefix}$lxc_new
        echo "lxc.rootfs = /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new" >> $lxc_path/$lxc_new/config
        # and mount it so we can tweak it
        mkdir -p $lxc_path/$lxc_new/rootfs
        mount /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new $rootfs || { echo "failed to mount new rootfs"; false; }
        mounted=1
    fi

elif out=$(btrfs subvolume list "$lxc_path/$lxc_orig/rootfs" 2>&1); then

    out=$(btrfs subvolume snapshot "$lxc_path/$lxc_orig/rootfs" "$rootfs" 2>&1) || { echo "failed btrfs snapshot"; false; }
    echo "lxc.rootfs = $rootfs" >> "$lxc_path/$lxc_new/config"

else
    if [ $snapshot = "yes" ]; then
        echo "Can't snapshot a directory"
        cleanup
    fi
    if [ $container_running = "True" ]; then
        lxc-freeze -n $lxc_orig
        frozen=1
    fi
    mkdir -p $lxc_path/$lxc_new/rootfs/
    rsync -ax $lxc_path/$lxc_orig/rootfs/ $lxc_path/$lxc_new/rootfs/
    echo "lxc.rootfs = $rootfs" >> $lxc_path/$lxc_new/config
    if [ $container_running = "True" ]; then
        lxc-unfreeze -n $lxc_orig
        frozen=0
    fi
fi

echo "Updating rootfs..."

# so you can 'ssh $hostname.' or 'ssh $hostname.local'
if [ -f $rootfs/etc/dhcp/dhclient.conf ] && ! grep -q "^send host-name.*hostname" $rootfs/etc/dhcp/dhclient.conf; then
    sed -i "s/send host-name.*$/send host-name \"$hostname\";/" $rootfs/etc/dhcp/dhclient.conf
fi

c=$lxc_path/$lxc_new/config
# change hwaddrs
mv ${c} ${c}.old
(
while read line; do
	if [ "${line:0:18}" = "lxc.network.hwaddr" ]; then
		echo "lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')"
	else
		echo "$line"
	fi
done
) < ${c}.old > ${c}
rm -f ${c}.old

# set the hostname
cat <<EOF > $rootfs/etc/hostname
$hostname
EOF
# set minimal hosts
cat <<EOF > $rootfs/etc/hosts
127.0.0.1 localhost $hostname
EOF

# if this was a block device, then umount it now
if [ $mounted -eq 1 ]; then
    umount $rootfs
fi

echo "'$lxc_new' created"