Skip to content

Instantly share code, notes, and snippets.

@glinton
Last active September 1, 2023 04:34
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save glinton/9153193 to your computer and use it in GitHub Desktop.
Save glinton/9153193 to your computer and use it in GitHub Desktop.
SmartOS zone manipulation. Snapshot, image, rollback, and migrate.

SmartOS Zone Manipulation

Notes on snapshots, images, and migrations

Create Zone/KVM

# Create zone
vmadm create <<EOL
{
 "brand": "joyent",
 .
 .
}
EOL
vmadm start 2e88885e-5050-4916-93ea-594bb48bce7a

# Create KVM with additional disk
vmadm create <<EOL
{
 .
  "disks": [
    {
      "path": "/dev/zvol/rdsk/zones/2e88885e-5050-4916-93ea-594bb48bce7b-disk0",
      "boot": true,
      .
    },
    {
      "path": "/dev/zvol/rdsk/zones/2e88885e-5050-4916-93ea-594bb48bce7b-disk1",
      "boot": false,
      .
    }
  ],
 .
}
EOL
vmadm start 2e88885e-5050-4916-93ea-594bb48bce7b

Snapshot Zone/KVM

# Zone
# Will/should capture delegated dataset as well.
zfs snapshot zones/2e88885e-5050-4916-93ea-594bb48bce7a@my-zone

# KVM
# Disk0 is the boot disk, disk1 is the additional disk, similar to the delegated dataset.
zfs snapshot zones/2e88885e-5050-4916-93ea-594bb48bce7b-disk0@boot-drive
zfs snapshot zones/2e88885e-5050-4916-93ea-594bb48bce7b-disk1@additional-drive

# Send snapshot to disk
# NOTE: Only necessary if converting to an image
zfs send zones/2e88885e-5050-4916-93ea-594bb48bce7a@my-zone | gzip > /opt/image/my-zone.zfs.gz

# For KVM, only create image from disk0, as it contains the OS
zfs send zones/2e88885e-5050-4916-93ea-594bb48bce7b-disk0@boot-drive | gzip > /opt/image/my-kvm.zfs.gz
# Then proceed to `zfs send zones/2e888...-disk1@additional-drive | zfs receive -F zones/<new_kvm_uuid>-disk1`

Convert Snapshot to Image

NOTE: This will only result in a reuseable image once the zone/KVM has been "prepared" prior to snapshotting

  • Create a manifest for the zone you snapshot to disk
  • Publish to image server
  • Import image when needed

Create manifest for zone

'creator' and 'vendor_uuid' should be consistent with creator and organization, respectively

cat > /opt/image/my-zone.dsmanifest << EOF
{
  "uuid": "`uuid -v4`",
  "name": "my-zone",
  "version": "0.14.2",
  "v": 1,
  "description": "My custom zone.",
  "os": "smartos",
  "published_at": "`date '+%Y-%m-%dT%H:%M:%SZ'`",
  "type": "zone-dataset",
  "platform_type": "smartos",
  "creator_name": "pagoda-ops",
  "creator_uuid": "28cf9d27-9891-45d9-8af6-0df133c3aca0",
  "vendor_uuid": "5acc1983-1cc2-48ae-9ef0-db3264a6b190",
  "files": [
    {
      "path": "my-zone.zfs.gz",
      "sha1": "`digest -a sha1 /opt/image/my-zone.zfs.gz`",
      "size": `ls -l /opt/image/my-zone.zfs.gz | awk '{print $5}'`,
      "compression": "gzip"
    }
  ]
}
EOF

Create manifest for KVM

cat > /opt/image/my-kvm.dsmanifest << EOF
{
  "uuid": "`uuid -v4`",
  "name": "my-kvm",
  "version": "0.10.12",
  "v": 1,
  "description": "My custom KVM zone.",
  "os": "linux",
  "type": "zvol",
  "published_at": "`date '+%Y-%m-%dT%H:%M:%SZ'`",
  "creator_name": "pagoda-ops",
  "creator_uuid": "28cf9d27-9891-45d9-8af6-0df133c3aca0",
  "vendor_uuid": "5acc1983-1cc2-48ae-9ef0-db3264a6b190",
  "files": [
    {
      "path": "mykvm.zfs.gz",
      "sha1": "`digest -a sha1 /opt/image/mykvm.zfs.gz`",
      "size": `ls -l /opt/image/mykvm.zfs.gz | awk '{print $5}'`,
      "compression": "gzip"
    }
  ],
  "requirements": {
    "networks": [
        {
            "name": "net0",
            "description": "public"
        }
    ],
    "ssh_key": true
  },
  "nic_driver": "virtio",
  "disk_driver": "virtio",
  "cpu_type": "qemu64",
  "image_size": `vmadm get <uuid_of_original_kvm> | json disks.0.size`
}
EOF

Publish to Image Server

imgadm publish -m /opt/image/my-zone.dsmanifest -f /opt/image/my-zone.zfs.gz images.pagodabox.com -q

# Or import image (local only, use image server instead)
imgadm install -m /opt/image/my-zone.dsmanifest -f /opt/image/my-zone.zfs.gz

Duplicate Zone/KVM

  • Snapshot zone/KVM
  • Create new zone/KVM
  • Send snapshot to new zone/KVM
# -F is necessary becuse the disk is already created.  
# WARNING: Will overwrite receiving dataset. Make sure it is empty.
# KVM
zfs send zones/2e88885e-5050-4916-93ea-594bb48bce7b-disk0@my-kvm-disk0 | zfs receive -F zones/375fd95a-fd32-474d-9f8d-eb3cbff882c7-disk0
zfs send zones/2e88885e-5050-4916-93ea-594bb48bce7b-disk1@my-kvm-disk1 | zfs receive -F zones/375fd95a-fd32-474d-9f8d-eb3cbff882c7-disk1

# Zone
zfs send zones/2e88885e-5050-4916-93ea-594bb48bce7a@my-zone | zfs receive -F zones/375fd95a-fd32-474d-9f8d-eb3cbff882c7

Image Snapshot Procedure

# Create new snapshot
zfs snapshot zones/53cdc280-3f4f-0131-560f-7742b098622d@working
# Verify snapshot
zfs list -t snapshot | grep 53cdc280-3f4f-0131-560f-7742b098622d
# Prepare zone for imaging (will shut it down)
zlogin 53cdc280-3f4f-0131-560f-7742b098622d sm-prepare-image -y
# Snapshot prepped zone
zfs snapshot zones/53cdc280-3f4f-0131-560f-7742b098622d@prepared
# Do stuff with your to-be-imaged snapshot
zfs send zones/53cdc280-3f4f-0131-560f-7742b098622d@prepared > /dev/null
# Rollback to working version of zone
# -r - Destroy any snapshots and bookmarks more recent than the one specified.
zfs rollback -r zones/53cdc280-3f4f-0131-560f-7742b098622d@working
# Start zone back up
vmadm start 53cdc280-3f4f-0131-560f-7742b098622d 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment