Advertisement
eostapets

lxc-clone

Oct 2nd, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.26 KB | None | 0 0
  1. --- lxc-clone.orig      2013-10-02 19:23:25.402039992 +0300
  2. +++ lxc-clone   2013-10-02 19:28:54.320818235 +0300
  3. @@ -191,6 +191,7 @@
  4.  
  5.  sed -i '/lxc.rootfs/d' $lxc_path/$lxc_new/config
  6.  if [ -b $oldroot ]; then
  7. +    tmproot=`mktemp -d`
  8.      type vgscan || { echo "$(basename $0): lvm is not installed" >&2; false; }
  9.      lvdisplay $oldroot > /dev/null 2>&1 || { echo "$(basename $0): non-lvm blockdev cloning is not supported" >&2; false; }
  10.      lvm=TRUE
  11. @@ -202,41 +203,28 @@
  12.      if [ $lxc_size = "_unset" ]; then
  13.          lxc_size=`lvdisplay $oldroot | grep Size | awk '{ print $3 $4 }'`
  14.      fi
  15. -    newlv="${lxc_lv_prefix}${lxc_new}_snapshot"
  16. -    lvcreate -s -L $lxc_size -n $newlv $oldroot
  17. -    type xfs_admin > /dev/null 2>&1 && {
  18. -        # change filesystem UUID if it is an xfs filesystem
  19. -        xfs_admin -u /dev/$lxc_vg/$newlv && xfs_admin -U generate /dev/$lxc_vg/$newlv
  20. -    }
  21.  
  22.      if [ $container_running = "True" ]; then
  23.          lxc-unfreeze -n $lxc_orig
  24.          frozen=0
  25.      fi
  26.      if [ $snapshot = "no" ]; then
  27. -        #mount snapshot
  28. -        mkdir -p ${rootfs}_snapshot
  29. -        mount /dev/$lxc_vg/${lxc_lv_prefix}${lxc_new}_snapshot ${rootfs}_snapshot || { echo "$(basename $0): failed to mount new rootfs_snapshot" >&2; false; }
  30. -        #create a new lv
  31. -        lvcreate -L $lxc_size $lxc_vg -n ${lxc_lv_prefix}$lxc_new
  32. -        echo "lxc.rootfs = /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new" >> $lxc_path/$lxc_new/config
  33. -        # and mount it so we can tweak it
  34. -        mkdir -p $rootfs
  35. -        mkfs -t $fstype /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new
  36. -        mount /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new $rootfs || { echo "$(basename $0): failed to mount new rootfs" >&2; false; }
  37. -        mounted=1
  38. -        rsync -ax ${rootfs}_snapshot/ ${rootfs}/ || { echo "$(basename $0): copying data to new lv failed" >&2; false; }
  39. -        umount ${rootfs}_snapshot
  40. -        rmdir ${rootfs}_snapshot
  41. -        lvremove -f $lxc_vg/${lxc_lv_prefix}${lxc_new}_snapshot
  42. +       lvconvert -m 1 $oldroot --alloc anywhere
  43. +       lvconvert --splitmirrors 1 --name $lxc_new $oldroot
  44.      else
  45. +       newlv="${lxc_lv_prefix}${lxc_new}_snapshot"
  46. +       lvcreate -s -L $lxc_size -n $newlv $oldroot
  47.          lvrename $lxc_vg/${lxc_lv_prefix}${lxc_new}_snapshot $lxc_vg/${lxc_lv_prefix}$lxc_new
  48. -        echo "lxc.rootfs = /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new" >> $lxc_path/$lxc_new/config
  49. -        # and mount it so we can tweak it
  50. -        mkdir -p $rootfs
  51. -        mount /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new $rootfs || { echo "$(basename $0): failed to mount new rootfs" >&2; false; }
  52. -        mounted=1
  53.      fi
  54. +    echo "lxc.rootfs = /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new" >> $lxc_path/$lxc_new/config
  55. +    type xfs_admin > /dev/null 2>&1 && {
  56. +               # change filesystem UUID if it is an xfs filesystem
  57. +               xfs_admin -u /dev/$lxc_vg/$newlv && xfs_admin -U generate /dev/$lxc_vg/$newlv
  58. +    }
  59. +    mkdir -p $tmproot
  60. +    mount /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new $tmproot || { echo "$(basename $0): failed to mount new rootfs" >&2; false; }
  61. +    mounted=1
  62. +    rootfs=$tmproot
  63.  
  64.  elif which btrfs >/dev/null 2>&1 && btrfs subvolume list $oldroot >/dev/null 2>&1; then
  65.      # if oldroot is a btrfs subvolume, assume they want a snapshot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement