4
# lxc: linux Container library
7
# Serge Hallyn <serge.hallyn@ubuntu.com>
8
# Daniel Lezcano <daniel.lezcano@free.fr>
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.
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.
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
25
echo "usage: lxc-clone -o <orig> -n <new> [-s] [-h] [-L fssize] [-v vgname]"
31
echo "creates a lxc system object."
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"
41
shortoptions='ho:n:sL:v:'
42
longoptions='help,orig:,name:,snapshot,fssize,vgname'
49
getopt=$(getopt -o $shortoptions --longoptions $longoptions -- "$@")
98
if [ -z "$lxc_path" ]; then
99
echo "no configuration path defined !"
103
if [ ! -r $lxc_path ]; then
104
echo "configuration path '$lxc_path' not found"
108
if [ -z "$lxc_orig" ]; then
109
echo "no original container name specified"
114
if [ -z "$lxc_new" ]; then
115
echo "no new container name specified"
120
if [ "$(id -u)" != "0" ]; then
121
echo "This command has to be run as root"
125
if [ ! -r $lxc_path ]; then
126
echo "no configuration path defined !"
130
if [ ! -d "$lxc_path/$lxc_orig" ]; then
131
echo "'$lxc_orig' does not exist"
135
if [ -d "$lxc_path/$lxc_new" ]; then
136
echo "'$lxc_new' already exists"
140
trap "${bindir}/lxc-destroy -n $lxc_new; echo aborted; exit 1" SIGHUP SIGINT SIGTERM
142
mkdir -p $lxc_path/$lxc_new
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
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
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
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
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"
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"
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; }
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
186
echo "Updating rootfs..."
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
195
cat <<EOF > $rootfs/etc/hostname
199
cat <<EOF > $rootfs/etc/hosts
200
127.0.0.1 localhost $hostname
203
# if this was a block device, then umount it now
204
if [ $mounted -eq 1 ]; then
208
echo "'$lxc_new' created"