Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
lxc
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
lxc
Commits
5d325fcf
Commit
5d325fcf
authored
Feb 09, 2012
by
Ubuntu
Committed by
Daniel Lezcano
Feb 26, 2012
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lxc-clone: support btrfs and clean up safely
btrfs support from Scott Moser. Signed-off-by:
Serge Hallyn
<
serge.hallyn@canonical.com
>
Signed-off-by:
Daniel Lezcano
<
dlezcano@fr.ibm.com
>
parent
8b7071ec
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
58 additions
and
51 deletions
+58
-51
lxc-clone.in
src/lxc/lxc-clone.in
+58
-51
No files found.
src/lxc/lxc-clone.in
View file @
5d325fcf
...
@@ -21,6 +21,8 @@
...
@@ -21,6 +21,8 @@
# License along with this library; if not, write to the Free Software
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
set
-e
usage
()
{
usage
()
{
echo
"usage: lxc-clone -o <orig> -n <new> [-s] [-h] [-L fssize] [-v vgname] [-p lxc_lv_prefix] [-t fstype]"
echo
"usage: lxc-clone -o <orig> -n <new> [-s] [-h] [-L fssize] [-v vgname] [-p lxc_lv_prefix] [-t fstype]"
}
}
...
@@ -97,7 +99,8 @@ while true; do
...
@@ -97,7 +99,8 @@ while true; do
;;
;;
--
)
--
)
shift
shift
break
;;
break
;;
*
)
*
)
echo
$1
echo
$1
usage
usage
...
@@ -148,10 +151,27 @@ if [ -d "$lxc_path/$lxc_new" ]; then
...
@@ -148,10 +151,27 @@ if [ -d "$lxc_path/$lxc_new" ]; then
exit
1
exit
1
fi
fi
trap
"
${
bindir
}
/lxc-destroy -n
$lxc_new
; echo aborted; exit 1"
SIGHUP SIGINT SIGTERM
mounted
=
0
frozen
=
0
oldroot
=
`
grep
lxc.rootfs
$lxc_path
/
$lxc_orig
/config |
awk
-F
=
'{ print $2 '
}
`
mkdir
-p
$lxc_path
/
$lxc_new
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 SIGHUP SIGINT SIGTERM
mkdir
-p
$lxc_path
/
$lxc_new
hostname
=
$lxc_new
hostname
=
$lxc_new
echo
"Tweaking configuration"
echo
"Tweaking configuration"
...
@@ -168,85 +188,72 @@ fi
...
@@ -168,85 +188,72 @@ fi
echo
"Copying rootfs..."
echo
"Copying rootfs..."
rootfs
=
$lxc_path
/
$lxc_new
/rootfs
rootfs
=
$lxc_path
/
$lxc_new
/rootfs
# First figure out if the old is a device. For now we only support
# lvm devices.
container_running
=
True
mounted
=
0
lxc-info
-s
-n
$lxc_orig
|grep RUNNING
>
/dev/null 2>&1
||
container_running
=
False
#is container running
lxc-info
-s
-n
$lxc_orig
|grep RUNNING
>
/dev/null 2>&1
if
[
$?
-ne
0
]
;
then
container_running
=
True
else
container_running
=
False
fi
sed
-i
'/lxc.rootfs/d'
$lxc_path
/
$lxc_new
/config
sed
-i
'/lxc.rootfs/d'
$lxc_path
/
$lxc_new
/config
oldroot
=
`
grep
lxc.rootfs
$lxc_path
/
$lxc_orig
/config |
awk
-F
=
'{ print $2 '
}
`
oldroot
=
`
grep
lxc.rootfs
$lxc_path
/
$lxc_orig
/config |
awk
-F
=
'{ print $2 '
}
`
if
[
-b
$oldroot
]
;
then
if
[
-b
$oldroot
]
;
then
# this is a device. If we don't want to snapshot, then mkfs, mount
type
vgscan
||
{
echo
"Please install lvm"
;
false
;
}
# and rsync. Trivial but not yet implemented
lvdisplay
$oldroot
>
/dev/null 2>&1
||
{
echo
"non-lvm blockdev cloning not supported"
;
false
;
}
#if [ $snapshot == "no" ]; then
# echo "non-snapshot and non-lvm clone of block device not yet implemented"
# exit 1
#fi
lvdisplay
$oldroot
>
/dev/null 2>&1
if
[
$?
-ne
0
]
;
then
lvm
=
False
echo
"non-lvm block device cloning not yet implemented"
exit
1
else
lvm
=
TRUE
lvm
=
TRUE
fi
# ok, create a snapshot of the lvm device
# ok, create a snapshot of the lvm device
if
[
$container_running
=
=
"True"
]
;
then
if
[
$container_running
=
"True"
]
;
then
lxc-freeze
-n
$lxc_orig
lxc-freeze
-n
$lxc_orig
frozen
=
1
fi
fi
lvcreate
-s
-L
$lxc_size
-n
${
lxc_lv_prefix
}${
lxc_new
}
_snapshot
$oldroot
lvcreate
-s
-L
$lxc_size
-n
${
lxc_lv_prefix
}${
lxc_new
}
_snapshot
$oldroot
RETVAL
=
$?
if
[
$container_running
=
"True"
]
;
then
if
[
$container_running
==
"True"
]
;
then
lxc-unfreeze
-n
$lxc_orig
lxc-unfreeze
-n
$lxc_orig
frozen
=
0
fi
fi
if
[
$RETVAL
-ne
0
]
;
then
if
[
$snapshot
=
"no"
]
;
then
echo
"snapshot creation failed"
exit
1
fi
if
[
$snapshot
==
"no"
]
;
then
#mount snapshot
#mount snapshot
mkdir
-p
${
rootfs
}
_snapshot
mkdir
-p
${
rootfs
}
_snapshot
mount /dev/
$lxc_vg
/
${
lxc_lv_prefix
}${
lxc_new
}
_snapshot
${
rootfs
}
_snapshot
||
{
echo
"failed to mount new rootfs_snapshot"
;
exit
1
;
}
mount /dev/
$lxc_vg
/
${
lxc_lv_prefix
}${
lxc_new
}
_snapshot
${
rootfs
}
_snapshot
||
{
echo
"failed to mount new rootfs_snapshot"
;
false
;
}
#create a new lv
#create a new lv
lvcreate
-L
$lxc_size
$lxc_vg
-n
${
lxc_lv_prefix
}
$lxc_new
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
echo
"lxc.rootfs = /dev/
$lxc_vg
/
${
lxc_lv_prefix
}
$lxc_new
"
>>
$lxc_path
/
$lxc_new
/config
# and mount it so we can tweak it
# and mount it so we can tweak it
mkdir
-p
$lxc_path
/
$lxc_new
/rootfs
mkdir
-p
$lxc_path
/
$lxc_new
/rootfs
mkfs
-t
$fstype
/dev/
$lxc_vg
/
${
lxc_lv_prefix
}
$lxc_new
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"
;
exit
1
;
}
mount /dev/
$lxc_vg
/
${
lxc_lv_prefix
}
$lxc_new
$rootfs
||
{
echo
"failed to mount new rootfs"
;
false
;
}
mounted
=
1
mounted
=
1
rsync
-ax
${
rootfs
}
_snapshot/
${
rootfs
}
/
||
{
echo
"copy of data to new lv failed"
;
exit
1
;
}
rsync
-ax
${
rootfs
}
_snapshot/
${
rootfs
}
/
||
{
echo
"copy of data to new lv failed"
;
false
;
}
umount
${
rootfs
}
_snapshot
||
{
echo
"failed to unmount new rootfs_snapshot"
;
exit
1
;
}
umount
${
rootfs
}
_snapshot
rm
-rf
${
rootfs
}
_snapshot
rmdir
${
rootfs
}
_snapshot
lvremove
-f
$lxc_vg
/
${
lxc_lv_prefix
}
$lxc_new
||
echo
"failed to remove the snapshot"
lvremove
-f
$lxc_vg
/
${
lxc_lv_prefix
}${
lxc_new
}
_snapshot
else
else
lvrename
$lxc_vg
/
${
lxc_lv_prefix
}
}
${
lxc_new
}
_snapshot
$lxc_vg
/
${
lxc_lv_prefix
}
$lxc_new
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
echo
"lxc.rootfs = /dev/
$lxc_vg
/
${
lxc_lv_prefix
}
$lxc_new
"
>>
$lxc_path
/
$lxc_new
/config
# and mount it so we can tweak it
# and mount it so we can tweak it
mkdir
-p
$lxc_path
/
$lxc_new
/rootfs
mkdir
-p
$lxc_path
/
$lxc_new
/rootfs
mount /dev/
$lxc_vg
/
${
lxc_lv_prefix
}
$lxc_new
$rootfs
||
{
echo
"failed to mount new rootfs"
;
exit
1
;
}
mount /dev/
$lxc_vg
/
${
lxc_lv_prefix
}
$lxc_new
$rootfs
||
{
echo
"failed to mount new rootfs"
;
false
;
}
mounted
=
1
mounted
=
1
fi
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
else
if
[
$container_running
==
True
]
;
then
if
[
$snapshot
=
"yes"
]
;
then
echo
"Can't snapshot a directory"
cleanup
fi
if
[
$container_running
=
"True"
]
;
then
lxc-freeze
-n
$lxc_orig
lxc-freeze
-n
$lxc_orig
frozen
=
1
fi
fi
rsync
-ax
$lxc_path
/
$lxc_orig
/rootfs
$lxc_path
/
$lxc_new
/rootfs
mkdir
-p
$lxc_path
/
$lxc_new
/rootfs/
RETVAL
=
$?
rsync
-ax
$lxc_path
/
$lxc_orig
/rootfs/
$lxc_path
/
$lxc_new
/rootfs/
if
[
$container_running
==
True
]
;
then
echo
"lxc.rootfs =
$rootfs
"
>>
$lxc_path
/
$lxc_new
/config
if
[
$container_running
=
"True"
]
;
then
lxc-unfreeze
-n
$lxc_orig
lxc-unfreeze
-n
$lxc_orig
frozen
=
0
fi
fi
if
[
RETVAL
-ne
0
]
;
then
echo
"copying rootfs failed"
exit
1
fi
echo
"lxc.rootfs =
$rootfs
"
>>
$lxc_path
/
$lxc_new
/config
fi
fi
echo
"Updating rootfs..."
echo
"Updating rootfs..."
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment