zone cloning without shutdown

por | 17 agosto, 2016

In my presentation at Oracle Openworld, I showed a demo of how to easily clone a zone to make a copy for development or testing purposes. These were the steps, given that the zone oow01 was already set up and running.

zonecfg -z oow01 export > oow43.cfg -- dump the existing config to a file
# edit the file oow43 for a new ip address and a new zonepath
zonecfg -z oow43 -f oow43.cfg -- import the new config
zoneadm -z oow01 halt -- this is neccessary for the 'clone' command
zoneadm -z oow43 clone oow01 -- this will take care of the rest

One of the questions I received from the audience after the presentation was: “Is it possible to clone a running zone?”. Sure, this is possible, but unfortunately not with the zoneadm command, so you will have to do some manual work. I did not have time to do it right there but promised to follow-up on my blog. But first a word of caution: Though I have done this with running zones multiple times with success, this does not mean it is guaranteed to work. You freeze the ZFS filesystem with a running zone midflight which is similar to turning off the power off an on and hoping that your computer works fine afterwards. From my experience, it does work fine almost all the time (and Oracle itself should be protected by instance recovery), just don’t count on it or blame me if the clone won’t start up or behave unexpectedly.
So basically, what ‘zoneadm clone’ will do for you is create a ZFS clone from the source zone’s filesystem and then modify a text file while ignoring a ‘DO NOT EDIT THIS FILE’-hint.

# the first two steps are exactly the same as above
zonecfg -z oow01 export > oow43.cfg -- dump the existing config to a file
# edit the file oow43 for a new ip address and a new zonepath
zonecfg -z oow43 -f oow43.cfg -- import the new config
zfs snapshot rpool/zones/oow42@clone_oow43 -- snapshot running zone
zfs clone rpool/zones/oow42@clone_oow43 rpool/zones/oow43 -- make a clone
# now, edit the file /etc/zones/index
# change the status of your zone from configured to installed
# and set a unique id

When I choose a new id for the zone, I just copy an existing one and modify a few digits. I don’t know if this is the ‘right’ way to do it but it works for me. Now, you can boot up this newly cloned zone and that is it. The hostname inside the zone is still the same as the original, but I usually don’t care or simply correct this.

thoughts on “zone cloning without shutdown”

  1. Just wanted to say thanks, this trick saved my ass yesterday after a zone clone failed and we didn’t notice until after the source zone had been brought back up. I actually created a temporary clone using this method, then cloned that the normal way to get a legitimate zone URN. (The zoneadm clone command has a -s option to use an existing zone snapshot of course, but that still requires the source zone to be halted, which seems nonsensical.)

  2. When I try the snapshot/clone trick with a zone filesystem, all I get in the clone are empty ‘dev’ and ‘root’ filesystems. A snapshot/clone of any other filesystem behaves as expected. Any insights? This is on oi_151a4.

    Thanks!

  3. nice .
    But importent is also to say, that the following files are not being copied:
    /etc/hosts
    /etc/nsswitch.conf
    /etc/resolv.conf
    /etc/netmasks

  4. Solaris 11 zones added quite a lot of complexity to the process:
    -take a recursive snapshot (ie: zfs snapshot -r rpool/zones/oow42@clone_oow43 )
    -clone all components individually (check zfs list -t filesystem -r rpool/zones/oow42 for a complete list):
    -rpool/zones/oow42/rpool
    -rpool/zones/oow42/rpool /rpool/ROOT
    -rpool/zones/oow42/rpool/ROOT/system-0 (or whatever Bootenvironment name you have)
    -rpool/zones/oow42/rpool/ROOT/system-0/var

    -you need to set a few filesystem attributes:
    zfs set zoned=on rpool/zones/oow43
    zfs set com.oracle.libbe:nbe_handle=on rpool/zones/oow43
    zfs set org.opensolaris.libbe:active=on rpool/zones/oow43/rpool/ROOT/solaris-0

    -Instead of playing with /etc/zones/index, you can also create a /rpool/zones/oow43/SUNWdetached.xml file (use edited contents from zoneadm -z oow42 detach -n). Then you can attach the zone with zoneadm -z oow43 attach