From c263e5150c5d7bfb8ce3afd20d335fb6eb0920f6 Mon Sep 17 00:00:00 2001 From: Helight Date: Sun, 15 Nov 2026 15:34:27 +0100 Subject: [PATCH] feat(incus): resize pools, volumes and root disks, and set pool settings --- .../docs/(guide)/deploy/provisioners.mdx | 17 +- plugins/incus/bin/provision | 293 +++++++++++++++++- plugins/incus/guide.md | 74 ++++- plugins/incus/package.json | 50 ++- tests/test_incus_plugin.py | 276 ++++++++++++++++- 5 files changed, 676 insertions(+), 34 deletions(-) diff --git a/docs/content/docs/(guide)/deploy/provisioners.mdx b/docs/content/docs/(guide)/deploy/provisioners.mdx index 933b751..d4edc91 100644 --- a/docs/content/docs/(guide)/deploy/provisioners.mdx +++ b/docs/content/docs/(guide)/deploy/provisioners.mdx @@ -364,7 +364,22 @@ A provisioner of the type `incus/vm` makes a virtual machine, and one of `incus/ berth talks to the Incus API over HTTPS. `server` is required, as a setting or for each machine. Incus must trust a certificate of berth, which you store as the secrets `incus_client_cert` and `incus_client_key`. A provisioner may have its own instead. The secret `incus_server_cert` is optional and pins the server's own certificate. `plugins/incus/guide.md` shows how to make and trust the certificate. -Their actions only read: `server` says the server's version and whether it trusts berth, `pools` how full each storage pool is, `images` the images the server keeps, and `networks` its networks. The page offers the server's pools and networks for a machine's `pool` and `network`. +The page offers the server's pools, networks and instances for the arguments `pool`, `network` and `instance`. + +| Action | Does | +| --- | --- | +| `server` | Says the server's version and whether it trusts berth | +| `pools` | Lists the storage pools in a table with the driver, source, size, used and free space of each, and whether it can grow | +| `images`, `networks`, `volumes` | List the images the server keeps, its networks and the custom volumes of every pool | +| `create-pool`, `remove-pool` | Create and remove a storage pool | +| `resize-pool` | Grows a loop-backed `btrfs`, `zfs` or `lvm` pool to `size`. It refuses a `dir` pool, a pool on a disk or dataset given when it was made, and any smaller size, as Incus never shrinks a pool | +| `set-pool` | Sets a setting `key` of a pool that Incus changes once the pool is made, such as `description` or `volume.size`, to `value` | +| `create-volume`, `remove-volume` | Create and remove a custom volume | +| `resize-volume` | Gives a custom volume a new `size`. A block volume only grows | +| `resize-disk` | Grows the root disk of `instance` to `size`. A container has it at once. A running virtual machine needs a restart, which happens only with `restart=yes` | +| `remove-image` | Removes a cached image | + +A size is a whole number and a unit, such as `50GiB` or `200GB`. The plugin checks it before it asks Incus. `plugins/incus/guide.md` has what each driver allows. ```bash berth provisioner add ovh-incus incus/vm server=https://192.0.2.10:8443 diff --git a/plugins/incus/bin/provision b/plugins/incus/bin/provision index 6a0a12a..c95a11c 100755 --- a/plugins/incus/bin/provision +++ b/plugins/incus/bin/provision @@ -543,7 +543,8 @@ def server_status(incus): {'label': 'Project', 'value': incus.project}, ] values = {} - for argument, path in (('pool', '/1.0/storage-pools'), ('network', '/1.0/networks')): + for argument, path in (('pool', '/1.0/storage-pools'), ('network', '/1.0/networks'), + ('instance', '/1.0/instances')): try: values[argument] = [name for name in names(incus, path) if name] except IncusError: @@ -557,6 +558,62 @@ def server_status(incus): # What change applies to an instance in place; the rest are fixed when it is made. CHANGEABLE = ('cpus', 'memory', 'disk') +# The byte suffixes Incus takes for sizes ("Units for storage, memory and network limits"); none is bytes. +UNITS = {'': 1, 'B': 1, 'bytes': 1, + 'kB': 1000, 'MB': 1000 ** 2, 'GB': 1000 ** 3, 'TB': 1000 ** 4, 'PB': 1000 ** 5, 'EB': 1000 ** 6, + 'KiB': 1024, 'MiB': 1024 ** 2, 'GiB': 1024 ** 3, 'TiB': 1024 ** 4, 'PiB': 1024 ** 5, 'EiB': 1024 ** 6} +SIZE = re.compile(r'([0-9]+)(' + '|'.join(unit for unit in UNITS if unit) + ')?') +SIZE_HELP = ('a whole number and a unit with no space between them, such as 50GiB or 200GB ' + '(kB, MB, GB, TB in powers of 1000; KiB, MiB, GiB, TiB in powers of 1024)') + + +def bytes_of(text): + """The bytes a size as Incus writes it stands for, or None for one it would not take.""" + match = SIZE.fullmatch((text or '').strip()) + return int(match[1]) * UNITS[match[2] or ''] if match else None + + +def size_of(arguments, what='size'): + """The size given, checked before it is sent: Incus takes nothing else.""" + size = (arguments.get(what) or '').strip() + if not size: + raise Failure(f'Give the {what}: {SIZE_HELP}.') + if not bytes_of(size): + raise Failure(f'{size!r} is not a size Incus takes. Give {SIZE_HELP}.') + return size + + +def optional_size(arguments): + """A size that may be left out, checked like any other.""" + return size_of(arguments) if (arguments.get('size') or '').strip() else '' + + +def root_device(instance): + """The name and settings of the instance's root disk, and whether they are its own + rather than its profile's.""" + for own, devices in ((True, instance.get('devices') or {}), (False, instance.get('expanded_devices') or {})): + for name, device in sorted(devices.items()): + if (device or {}).get('type') == 'disk' and device.get('path') == '/': + return name, dict(device), own + return None, None, False + + +def grow_root(incus, name, instance, size): + """The patch that gives the instance's root disk `size`, or None when it has it already. + A root disk the instance has from its profile is overridden by one of its own with the new size.""" + device, root, own = root_device(instance) + if not root: + raise Failure(f'{name} has no root disk berth can find: neither it nor its profiles have a disk at /.') + current = root.get('size') or '' + if bytes_of(current) == bytes_of(size): + return None + if bytes_of(current) and bytes_of(size) < bytes_of(current): + raise Failure(f'The root disk of {name} is {current}, and berth only grows it: Incus cannot shrink a ' + 'virtual machine\'s disk, and a container\'s only on some drivers and never below what it holds.') + if not own: + say(f'{name} has its root disk from its profile; it gets one of its own of {size}.') + return {'devices': {**(instance.get('devices') or {}), device: {**root, 'size': size}}} + def change(incus, request, arguments): """New limits for an instance: its CPUs and memory, and a larger root disk.""" @@ -574,9 +631,12 @@ def change(incus, request, arguments): patch['config']['limits.cpu'] = arguments['cpus'] if arguments.get('memory'): patch['config']['limits.memory'] = arguments['memory'] - root = ((instance.get('devices') or {}).get('root') or {}) - if arguments.get('disk') and root and root.get('size') != arguments['disk']: - patch['devices'] = {**(instance.get('devices') or {}), 'root': {**root, 'size': arguments['disk']}} + # berth sends every value; the disk is grown only when it was given anew, so a disk + # resize-disk grew is not set back to the size berth made it with. + if arguments.get('disk') and 'disk' in request.get('changed', ['disk']): + grown = grow_root(incus, name, instance, size_of(arguments, 'disk')) + if grown: + patch.update(grown) if patch['config'] or 'devices' in patch: say(f'Changing {name}…') incus.call('PATCH', f'/1.0/instances/{name}', patch) @@ -640,17 +700,214 @@ def act_server(incus, arguments): return {'message': status['message'], 'details': status['details']} +def table(header, rows): + """Rows of an answer that read as a table: the first cell is the label, the rest are + lined up in the value, with the header as the first row.""" + cells = [list(header)] + [list(row) for row in rows] + widths = [max(len(row[column]) for row in cells) for column in range(len(header))] + lined = [] + for row in cells: + value = ' '.join(cell.ljust(widths[column]) for column, cell in enumerate(row[1:], 1)).rstrip() + lined.append({'label': row[0], 'value': value}) + return lined + + +# Drivers whose loop file Incus grows when the pool's size is raised ("Resize a storage pool"). +GROWS = ('btrfs', 'zfs', 'lvm') + + +def resizable(incus, pool): + """Whether Incus can grow the pool by its size, and why not.""" + driver = pool.get('driver') or '?' + source = ((pool.get('config') or {}).get('source') or '').strip() + if driver == 'dir': + return False, 'a dir pool has no size of its own, only the space of its file system' + if driver not in GROWS: + return False, f'Incus grows only loop-backed btrfs, zfs and lvm pools, not {driver}' + # A loop file Incus made is DISKS/NAME.img; any other source is a device, dataset or group the admin gave. + if source and not source.endswith('.img'): + return False, f'it is on {source}, given when it was made; grow that instead' + extensions = incus.about.get('api_extensions') + if isinstance(extensions, list) and 'storage_pool_loop_resize' not in extensions: + return False, 'this Incus lacks the API extension storage_pool_loop_resize' + return True, '' + + +def space_of(incus, name): + """The total and used bytes of a pool, or (None, None) when Incus cannot tell.""" + try: + space = (incus.call('GET', f'/1.0/storage-pools/{name}/resources') or {}).get('space') or {} + except IncusError: + return None, None + return space.get('total'), space.get('used') + + +def space_said(total, used): + if total is None: + return 'its use is not known' + return f'{gib(total)}, {gib(used)} used, {gib((total or 0) - (used or 0))} free' + + def act_pools(incus, arguments): rows = [] - for pool in incus.call('GET', '/1.0/storage-pools', query={'recursion': 1}) or []: + pools = incus.call('GET', '/1.0/storage-pools', query={'recursion': 1}) or [] + for pool in pools[:ROWS]: name = pool.get('name') or '' - try: - space = (incus.call('GET', f'/1.0/storage-pools/{name}/resources') or {}).get('space') or {} - used = f'{gib(space.get("used"))} of {gib(space.get("total"))} used' - except IncusError: - used = 'its use is not known' - rows.append({'label': name, 'value': f'{pool.get("driver") or "?"} · {used}'}) - return {'message': f'{counted(len(rows), "storage pool")} on {incus.server}', 'details': rows[:ROWS]} + config = pool.get('config') or {} + total, used = space_of(incus, name) + can, why = resizable(incus, pool) + known = total is not None + rows.append((name, pool.get('driver') or '?', config.get('source') or '', + gib(total) if known else config.get('size') or '?', + gib(used) if known else '?', gib((total or 0) - (used or 0)) if known else '?', + 'yes' if can else f'no: {why}')) + message = f'{counted(len(pools), "storage pool")} on {incus.server}' + if len(pools) > ROWS: + message += f', the first {ROWS} shown' + return {'message': message, + 'details': table(('Pool', 'Driver', 'Source', 'Size', 'Used', 'Free', 'Can grow'), rows)} + + +def pool_named(incus, name): + pool = incus.call('GET', f'/1.0/storage-pools/{name}', missing=True) + if not pool: + raise Failure(f'{incus.server} has no storage pool {name}.') + return pool + + +def act_resize_pool(incus, arguments): + name, size = pool_of(arguments), size_of(arguments) + pool = pool_named(incus, name) + can, why = resizable(incus, pool) + if not can: + raise Failure(f'The pool {name} cannot be resized: {why}.') + total, used = space_of(incus, name) + current = (pool.get('config') or {}).get('size') or '' + now = bytes_of(current) or total + if now and bytes_of(size) == now: + return {'message': f'The pool {name} is {current or gib(total)} already', + 'details': [{'label': 'Size', 'value': space_said(total, used)}]} + if now and bytes_of(size) < now: + raise Failure(f'The pool {name} is {current or gib(total)}, and Incus only grows a pool: no driver, ' + f'{pool.get("driver")} neither, can shrink one. Give a size larger than that.') + say(f'Before: {space_said(total, used)}') + say(f'Growing the {pool.get("driver")} pool {name} to {size}…') + incus.call('PATCH', f'/1.0/storage-pools/{name}', {'config': {'size': size}}) + after = space_of(incus, name) + say(f'After: {space_said(*after)}') + return {'message': f'The pool {name} on {incus.server} is {size} now', + 'details': [{'label': 'Before', 'value': space_said(total, used)}, + {'label': 'After', 'value': space_said(*after)}]} + + +# The pool settings set-pool changes: those Incus applies to a pool that exists. The ones that +# say where and how it was made, such as source, zfs.pool_name and lvm.vg_name, stay as they were. +POOL_KEYS = { + 'description': 'what the pool is for, in words', + 'volume.size': 'the size of a new volume or root disk that names none', + 'volume.snapshots.schedule': 'when new volumes take snapshots, as a cron expression or @daily', + 'volume.snapshots.expiry': 'when snapshots of new volumes are removed, such as 2w', +} +DRIVER_KEYS = { + 'dir': {'rsync.bwlimit': 'the bandwidth rsync may use to move volumes, such as 50MB', + 'rsync.compression': 'whether rsync compresses what it moves: true or false'}, + 'btrfs': {'btrfs.mount_options': 'the options the pool is mounted with'}, + 'zfs': {'zfs.clone_copy': 'true, false or rebase: how instances are copied from images', + 'zfs.export': 'whether the zpool is exported when it is unmounted: true or false'}, +} + + +def act_set_pool(incus, arguments): + name = pool_of(arguments) + key, value = (arguments.get('key') or '').strip(), (arguments.get('value') or '').strip() + pool = pool_named(incus, name) + driver = pool.get('driver') or '?' + known = {**POOL_KEYS, **DRIVER_KEYS.get(driver, {})} + if key == 'size': + raise Failure('The size of a pool is changed with resize-pool, which says whether it can grow.') + if key not in known: + raise Failure(f'berth does not change {key or "(no key)"} of a {driver} pool. It changes ' + + ', '.join(known) + '; the rest are fixed when the pool is made.') + if key == 'volume.size' and value: + size_of({'size': value}) + before = pool.get('description') if key == 'description' else (pool.get('config') or {}).get(key) + if key == 'description': + incus.call('PATCH', f'/1.0/storage-pools/{name}', {'description': value}) + elif value: + incus.call('PATCH', f'/1.0/storage-pools/{name}', {'config': {key: value}}) + else: + # An empty value unsets the key, as `incus storage unset` does: the pool is put back without it. + config = {k: v for k, v in (pool.get('config') or {}).items() if k != key} + incus.call('PUT', f'/1.0/storage-pools/{name}', {'config': config, 'description': pool.get('description') or ''}) + return {'message': f'The pool {name}: {key} is {value or "unset"} now', + 'details': [{'label': 'Before', 'value': before or '(unset)'}, + {'label': 'After', 'value': value or '(unset)'}]} + + +def act_resize_volume(incus, arguments): + pool, size = pool_of(arguments), size_of(arguments) + name = storage_name({'name': arguments.get('volume')}, 'volume') + path = f'/1.0/storage-pools/{pool}/volumes/custom/{name}' + volume = incus.call('GET', path, missing=True) + if not volume: + raise Failure(f'The pool {pool} has no custom volume {name} in the project {incus.project}.') + current = (volume.get('config') or {}).get('size') or '' + content = volume.get('content_type') or 'filesystem' + if current and bytes_of(size) == bytes_of(current): + return {'message': f'The volume {pool}/{name} is {current} already'} + if current and bytes_of(size) < (bytes_of(current) or 0): + if content == 'block': + raise Failure(f'{pool}/{name} is a block volume of {current}, and Incus cannot shrink a block volume.') + say(f'Shrinking {pool}/{name}: Incus refuses it if the volume holds more than {size}.') + used = '' + try: + state = incus.call('GET', f'{path}/state', missing=True) or {} + if (state.get('usage') or {}).get('used') is not None: + used = f', {gib(state["usage"]["used"])} used' + except IncusError: + pass + say(f'Before: {current or "no set size"}{used}') + incus.call('PATCH', path, {'config': {'size': size}}) + say(f'After: {size}') + return {'message': f'The volume {pool}/{name} is {size} now', + 'details': [{'label': 'Before', 'value': (current or 'no set size') + used}, + {'label': 'After', 'value': size}]} + + +def act_resize_disk(incus, arguments): + name = (arguments.get('instance') or '').strip() + if not re.fullmatch(r'[A-Za-z][A-Za-z0-9-]{0,62}', name): + raise Failure(f'{name!r} is not an Incus instance: give its name, as the server lists it.') + size = size_of(arguments) + restart = (arguments.get('restart') or 'no').strip() + if restart not in ('yes', 'no'): + raise Failure(f'restart is yes or no, not {restart!r}.') + instance = incus.instance(name) + if not instance: + raise Failure(f'{incus.server} has no instance {name} in the project {incus.project}.') + _, root, _ = root_device(instance) + before = (root or {}).get('size') or 'no set size' + patch = grow_root(incus, name, instance, size) + if not patch: + return {'message': f'The root disk of {name} is {before} already'} + say(f'Growing the root disk of {name} from {before} to {size}…') + incus.call('PATCH', f'/1.0/instances/{name}', patch) + running = state_of(instance) == 'running' + vm = instance.get('type') == 'virtual-machine' + if not vm: + said = 'A container has the new size at once; it was not restarted.' + elif not running: + said = 'The virtual machine is stopped; it has the new size when it starts.' + elif restart == 'yes': + say(f'Restarting {name}…') + incus.set_state(name, 'restart') + said = 'The virtual machine was restarted, and its guest has the new size.' + else: + said = ('The virtual machine\'s guest sees the new size after a restart: run this again with restart=yes, ' + 'or restart it when it suits.') + say(said) + return {'message': f'The root disk of {name} is {size} now. {said}', + 'details': [{'label': 'Before', 'value': before}, {'label': 'After', 'value': size}]} def act_images(incus, arguments): @@ -707,7 +964,8 @@ def act_volumes(incus, arguments): def act_create_volume(incus, arguments): pool, name = pool_of(arguments), storage_name(arguments, 'volume') - config = {'size': arguments['size'].strip()} if (arguments.get('size') or '').strip() else {} + size = optional_size(arguments) + config = {'size': size} if size else {} incus.call('POST', f'/1.0/storage-pools/{pool}/volumes/custom', {'name': name, 'type': 'custom', 'config': config}) return {'message': f'Created the volume {name} in the pool {pool} on {incus.server}'} @@ -722,9 +980,10 @@ def act_remove_volume(incus, arguments): def act_create_pool(incus, arguments): name = storage_name(arguments, 'storage pool') driver = (arguments.get('driver') or 'dir').strip() + size = optional_size(arguments) config = {} - if (arguments.get('size') or '').strip() and driver != 'dir': - config['size'] = arguments['size'].strip() + if size and driver != 'dir': + config['size'] = size say(f'Creating the {driver} pool {name}…') incus.call('POST', '/1.0/storage-pools', {'name': name, 'driver': driver, 'config': config}) return {'message': f'Created the {driver} storage pool {name} on {incus.server}'} @@ -755,7 +1014,9 @@ def act_remove_image(incus, arguments): ACTIONS = {'server': act_server, 'pools': act_pools, 'images': act_images, 'networks': act_networks, 'volumes': act_volumes, 'create-volume': act_create_volume, 'remove-volume': act_remove_volume, - 'create-pool': act_create_pool, 'remove-pool': act_remove_pool, 'remove-image': act_remove_image} + 'create-pool': act_create_pool, 'remove-pool': act_remove_pool, 'remove-image': act_remove_image, + 'resize-pool': act_resize_pool, 'set-pool': act_set_pool, 'resize-volume': act_resize_volume, + 'resize-disk': act_resize_disk} def act(incus, request, arguments): diff --git a/plugins/incus/guide.md b/plugins/incus/guide.md index 6c105ab..28aed98 100644 --- a/plugins/incus/guide.md +++ b/plugins/incus/guide.md @@ -191,7 +191,7 @@ actions: | Action | What it does | | --- | --- | | `server` | Says the server's version, its kernel and storage drivers, and whether it trusts berth. | -| `pools` | The storage pools, with how much of each is used. | +| `pools` | A table of the storage pools with the driver, source, size, used and free space of each, and whether it can grow. | | `images` | The images the server keeps in the project, with their sizes. | | `networks` | The networks a machine can be attached to. | | `volumes` | The custom volumes of every pool, with their sizes and whether an instance uses each. | @@ -200,8 +200,78 @@ actions: | `create-pool` | Creates a storage pool with the driver `dir`, `zfs`, `btrfs` or `lvm`. The last three take a size for their loop file. | | `remove-pool` | Removes a storage pool. Incus refuses one that still holds something. | | `remove-image` | Removes a cached image, by its alias or the start of its fingerprint. | +| `resize-pool` | Grows a storage pool to `size`. | +| `set-pool` | Sets one setting `key` of a pool to `value`. An empty value unsets it. | +| `resize-volume` | Gives a custom volume a new `size`. | +| `resize-disk` | Grows the root disk of `instance` to `size`, and restarts a virtual machine with `restart=yes`. | -The page also offers the server's pools and networks for `pool` and `network`. +The page also offers the server's pools, networks and instances for `pool`, +`network` and `instance`. + +## Storage + +A size is a whole number and a unit with no space between them, such as +`50GiB` or `200GB`. `kB`, `MB`, `GB` and `TB` count in powers of 1000, and +`KiB`, `MiB`, `GiB` and `TiB` in powers of 1024. A number alone is bytes. The +plugin checks a size before it sends anything to Incus. + +```bash +berth provisioner run ovh-incus pools +berth provisioner run ovh-incus resize-pool pool=default size=200GiB +berth provisioner run ovh-incus resize-disk instance=berth-studio size=100GiB restart=yes +``` + +### Growing a pool + +`resize-pool` raises the pool's `size`, and Incus grows its loop file. The +job says the pool's size and free space before and after. Incus grows a pool +and never shrinks one, with any driver. The plugin refuses a smaller size. + +| Pool | `resize-pool` | +| --- | --- | +| `btrfs`, `zfs` or `lvm` on a loop file Incus made | Grows it | +| `dir` | Refused, as the pool has no size of its own, only the space of the file system it's in | +| On a disk, partition, ZFS dataset or LVM volume group given when it was made | Refused. Grow that on the server instead | +| Another driver, such as `ceph` | Refused | + +An Incus without the API extension `storage_pool_loop_resize` can't grow a +pool, and the plugin says so. + +### Pool settings + +`set-pool` changes only the settings that Incus takes for a pool that exists. +The ones that say where and how the pool was made, such as `source`, +`zfs.pool_name` or `lvm.vg_name`, stay as they are. The plugin refuses any +other key and lists the ones it knows. + +| Pool | Settings | +| --- | --- | +| Any | `description`, and the defaults of new volumes: `volume.size`, `volume.snapshots.schedule` and `volume.snapshots.expiry` | +| `dir` | Also `rsync.bwlimit` and `rsync.compression` | +| `btrfs` | Also `btrfs.mount_options` | +| `zfs` | Also `zfs.clone_copy` and `zfs.export` | + +### Volumes and root disks + +`resize-volume` sets the size of a custom volume. Incus grows any volume. It +shrinks a volume of the content type `filesystem` only down to what it holds, +and refuses anything smaller. A `block` volume never shrinks, and the plugin +refuses that. A volume in a `dir` pool has a size only when the file system +under the pool has project quotas. + +`resize-disk` sets the size of the instance's root disk. When the instance has +its root disk from a profile, it gets a disk of its own with the new size, and +the profile stays as it is. The plugin only grows a root disk. + +| Instance | After `resize-disk` | +| --- | --- | +| Container | Has the new size at once, with no restart | +| Running virtual machine | Its guest sees the new size after a restart. The plugin restarts it only with `restart=yes` | +| Stopped virtual machine | Has the new size when it starts | + +A machine's `change` grows its disk too, when `disk` is given anew. It leaves a +disk that `resize-disk` grew as it is, though berth's record still shows the +size it was made with. ## Limits diff --git a/plugins/incus/package.json b/plugins/incus/package.json index 2658729..63202b6 100644 --- a/plugins/incus/package.json +++ b/plugins/incus/package.json @@ -93,7 +93,7 @@ ], "actions": [ {"name": "server", "label": "Check the server", "group": "Server", "changes": false, "help": "Ask the Incus server for its version, its kernel and its storage drivers, and whether it trusts berth's client certificate."}, - {"name": "pools", "label": "Storage pools", "group": "Storage", "changes": false, "help": "The storage pools of the server, with how much of each is used."}, + {"name": "pools", "label": "Storage pools", "group": "Storage", "changes": false, "help": "The storage pools of the server as a table: each one's driver, source, size, used and free space, and whether it can grow, or why not."}, {"name": "images", "label": "Cached images", "group": "Storage", "changes": false, "help": "The images the server keeps in its project, with their sizes. Incus fetches one from images.linuxcontainers.org when a machine first needs it."}, {"name": "networks", "label": "Networks", "group": "Server", "changes": false, "help": "The networks of the server a machine can be attached to."}, {"name": "volumes", "label": "Volumes", "group": "Storage", "changes": false, "help": "The custom volumes of every storage pool in the project, with their sizes and whether an instance uses each."}, @@ -121,6 +121,29 @@ {"name": "remove-image", "label": "Remove image", "group": "Storage", "confirm": "Remove this image from the server?", "help": "Remove a cached image from the project. Incus fetches it again when something needs it.", "arguments": [ {"name": "image", "label": "Image", "default": "", "help": "The image's alias, or the start of its fingerprint as the cached images show it."} + ]}, + {"name": "resize-pool", "label": "Grow storage pool", "group": "Storage", "confirm": "Grow this storage pool? Incus cannot shrink it again.", "help": "Grow a loop-backed btrfs, zfs or lvm pool to a new size. A dir pool has no size of its own, and a pool on a device or dataset that was given when it was made is grown there instead.", + "arguments": [ + {"name": "pool", "label": "Pool", "default": "default", "help": "The storage pool to grow."}, + {"name": "size", "label": "Size", "default": "", "help": "Its new size, larger than it is now, such as 100GiB or 200GB."} + ]}, + {"name": "set-pool", "label": "Change storage pool", "group": "Storage", "help": "Change a setting of a storage pool that Incus allows to change once it is made: description, volume.size, volume.snapshots.schedule and volume.snapshots.expiry, rsync.bwlimit and rsync.compression of a dir pool, btrfs.mount_options of a btrfs pool, and zfs.clone_copy and zfs.export of a zfs pool.", + "arguments": [ + {"name": "pool", "label": "Pool", "default": "default", "help": "The storage pool to change."}, + {"name": "key", "label": "Setting", "default": "", "help": "The setting, such as volume.size or description."}, + {"name": "value", "label": "Value", "default": "", "help": "Its new value, such as 20GiB for volume.size. Empty unsets it."} + ]}, + {"name": "resize-volume", "label": "Resize volume", "group": "Storage", "help": "Give a custom volume a new size. Incus grows any volume, and shrinks a filesystem volume only to no less than it holds; a block volume cannot shrink.", + "arguments": [ + {"name": "pool", "label": "Pool", "default": "default", "help": "The storage pool it is in."}, + {"name": "volume", "label": "Volume", "default": "", "help": "The name of the volume."}, + {"name": "size", "label": "Size", "default": "", "help": "Its new size, such as 20GiB."} + ]}, + {"name": "resize-disk", "label": "Grow root disk", "group": "Storage", "help": "Grow the root disk of an instance. One that has its root disk from a profile gets one of its own. A container has the new size at once; a virtual machine's guest sees it after a restart, which happens only with restart=yes.", + "arguments": [ + {"name": "instance", "label": "Instance", "default": "", "help": "The instance's name as the server lists it, such as berth-studio for the host studio."}, + {"name": "size", "label": "Size", "default": "", "help": "The new size of its root disk, larger than it is now, such as 80GiB."}, + {"name": "restart", "label": "Restart", "default": "no", "values": ["no", "yes"], "help": "Whether to restart a running virtual machine so that its guest sees the new size. A container needs no restart."} ]} ] }, @@ -170,7 +193,7 @@ ], "actions": [ {"name": "server", "label": "Check the server", "group": "Server", "changes": false, "help": "Ask the Incus server for its version, its kernel and its storage drivers, and whether it trusts berth's client certificate."}, - {"name": "pools", "label": "Storage pools", "group": "Storage", "changes": false, "help": "The storage pools of the server, with how much of each is used."}, + {"name": "pools", "label": "Storage pools", "group": "Storage", "changes": false, "help": "The storage pools of the server as a table: each one's driver, source, size, used and free space, and whether it can grow, or why not."}, {"name": "images", "label": "Cached images", "group": "Storage", "changes": false, "help": "The images the server keeps in its project, with their sizes. Incus fetches one from images.linuxcontainers.org when a machine first needs it."}, {"name": "networks", "label": "Networks", "group": "Server", "changes": false, "help": "The networks of the server a machine can be attached to."}, {"name": "volumes", "label": "Volumes", "group": "Storage", "changes": false, "help": "The custom volumes of every storage pool in the project, with their sizes and whether an instance uses each."}, @@ -198,6 +221,29 @@ {"name": "remove-image", "label": "Remove image", "group": "Storage", "confirm": "Remove this image from the server?", "help": "Remove a cached image from the project. Incus fetches it again when something needs it.", "arguments": [ {"name": "image", "label": "Image", "default": "", "help": "The image's alias, or the start of its fingerprint as the cached images show it."} + ]}, + {"name": "resize-pool", "label": "Grow storage pool", "group": "Storage", "confirm": "Grow this storage pool? Incus cannot shrink it again.", "help": "Grow a loop-backed btrfs, zfs or lvm pool to a new size. A dir pool has no size of its own, and a pool on a device or dataset that was given when it was made is grown there instead.", + "arguments": [ + {"name": "pool", "label": "Pool", "default": "default", "help": "The storage pool to grow."}, + {"name": "size", "label": "Size", "default": "", "help": "Its new size, larger than it is now, such as 100GiB or 200GB."} + ]}, + {"name": "set-pool", "label": "Change storage pool", "group": "Storage", "help": "Change a setting of a storage pool that Incus allows to change once it is made: description, volume.size, volume.snapshots.schedule and volume.snapshots.expiry, rsync.bwlimit and rsync.compression of a dir pool, btrfs.mount_options of a btrfs pool, and zfs.clone_copy and zfs.export of a zfs pool.", + "arguments": [ + {"name": "pool", "label": "Pool", "default": "default", "help": "The storage pool to change."}, + {"name": "key", "label": "Setting", "default": "", "help": "The setting, such as volume.size or description."}, + {"name": "value", "label": "Value", "default": "", "help": "Its new value, such as 20GiB for volume.size. Empty unsets it."} + ]}, + {"name": "resize-volume", "label": "Resize volume", "group": "Storage", "help": "Give a custom volume a new size. Incus grows any volume, and shrinks a filesystem volume only to no less than it holds; a block volume cannot shrink.", + "arguments": [ + {"name": "pool", "label": "Pool", "default": "default", "help": "The storage pool it is in."}, + {"name": "volume", "label": "Volume", "default": "", "help": "The name of the volume."}, + {"name": "size", "label": "Size", "default": "", "help": "Its new size, such as 20GiB."} + ]}, + {"name": "resize-disk", "label": "Grow root disk", "group": "Storage", "help": "Grow the root disk of an instance. One that has its root disk from a profile gets one of its own. A container has the new size at once; a virtual machine's guest sees it after a restart, which happens only with restart=yes.", + "arguments": [ + {"name": "instance", "label": "Instance", "default": "", "help": "The instance's name as the server lists it, such as berth-studio for the host studio."}, + {"name": "size", "label": "Size", "default": "", "help": "The new size of its root disk, larger than it is now, such as 80GiB."}, + {"name": "restart", "label": "Restart", "default": "no", "values": ["no", "yes"], "help": "Whether to restart a running virtual machine so that its guest sees the new size. A container needs no restart."} ]} ] } diff --git a/tests/test_incus_plugin.py b/tests/test_incus_plugin.py index 2373565..0fbcf5a 100644 --- a/tests/test_incus_plugin.py +++ b/tests/test_incus_plugin.py @@ -33,8 +33,9 @@ def __init__(self, context): self.socket = context.wrap_socket(self.socket, server_side=True) self.trusted = True self.instances = {} - self.pools = {'default': 'zfs', 'fast': 'dir'} + self.pools = {} self.volumes = {} + self.extensions = ['storage_pool_loop_resize'] self.operations = {} self.events = [] self.lock = threading.Lock() @@ -53,6 +54,34 @@ def public(instance): return {k: v for k, v in instance.items() if k not in ('files', 'request', 'console', 'logs')} +def pools(): + """The pools of a server that is set up: a loop-backed zfs pool, a dir pool and an lvm pool on a disk.""" + gib = 1024 ** 3 + return { + 'default': {'name': 'default', 'driver': 'zfs', 'description': '', 'used': 10 * gib, + 'config': {'source': '/var/lib/incus/disks/default.img', 'size': '100GiB', + 'zfs.pool_name': 'default'}}, + 'fast': {'name': 'fast', 'driver': 'dir', 'description': '', 'used': 5 * gib, 'total': 200 * gib, + 'config': {'source': '/var/lib/incus/storage-pools/fast'}}, + 'disk': {'name': 'disk', 'driver': 'lvm', 'description': '', 'used': 0, 'total': 500 * gib, + 'config': {'source': '/dev/sdb', 'lvm.vg_name': 'disk'}}, + } + + +SIZES = {'GiB': 1024 ** 3, 'GB': 1000 ** 3} + + +def size_in_bytes(text): + for unit, factor in SIZES.items(): + if text.endswith(unit): + return int(text[:-len(unit)]) * factor + return int(text) + + +def public_pool(pool): + return {k: v for k, v in pool.items() if k not in ('used', 'total')} + + class Handler(BaseHTTPRequestHandler): def log_message(self, *args): pass @@ -92,15 +121,32 @@ def route(self): return self.answer(200, {'auth': 'trusted' if server.trusted else 'untrusted', 'api_version': '1.0', 'environment': {'server_version': '6.18', 'server_name': 'dock', 'kernel': 'Linux', 'kernel_version': '6.17.0', - 'storage_supported_drivers': [{'name': 'zfs'}, {'name': 'dir'}]}}) + 'storage_supported_drivers': [{'name': 'zfs'}, {'name': 'dir'}]}, + 'api_extensions': server.extensions}) if not server.trusted: return self.answer(403, 'not authorized', 'error') if parts == ['1.0', 'storage-pools'] and self.command == 'GET': - return self.answer(200, [{'name': name, 'driver': driver} for name, driver in server.pools.items()]) + return self.answer(200, [public_pool(pool) for pool in server.pools.values()]) if parts == ['1.0', 'storage-pools'] and self.command == 'POST': data = json.loads(body) server.events.append(('pool', data)) - server.pools[data['name']] = data['driver'] + server.pools[data['name']] = {'name': data['name'], 'driver': data['driver'], 'description': '', + 'config': data.get('config') or {}, 'used': 0} + return self.answer(200, {}) + if parts[:2] == ['1.0', 'storage-pools'] and len(parts) == 3 and self.command in ('GET', 'PATCH', 'PUT'): + pool = server.pools.get(parts[2]) + if pool is None: + return self.answer(404, 'Storage pool not found', 'error') + if self.command == 'GET': + return self.answer(200, public_pool(pool)) + data = json.loads(body) + server.events.append(('pool-' + self.command.lower(), parts[2], data)) + if self.command == 'PUT': + pool['config'] = data.get('config') or {} + else: + pool['config'].update(data.get('config') or {}) + if 'description' in data: + pool['description'] = data['description'] return self.answer(200, {}) if parts[:2] == ['1.0', 'storage-pools'] and len(parts) == 3 and self.command == 'DELETE': if any(pool == parts[2] for pool, _ in server.volumes): @@ -116,6 +162,18 @@ def route(self): server.volumes[(pool, data['name'])] = {'name': data['name'], 'config': data.get('config') or {}, 'content_type': 'filesystem', 'used_by': []} return self.answer(200, {}) + volume = server.volumes.get((pool, parts[5])) if len(parts) > 5 else None + if len(parts) >= 6 and volume is None: + return self.answer(404, 'Storage volume not found', 'error') + if len(parts) == 6 and self.command == 'GET': + return self.answer(200, volume) + if len(parts) == 6 and self.command == 'PATCH': + data = json.loads(body) + server.events.append(('volume-patch', pool, parts[5], data)) + volume['config'].update(data.get('config') or {}) + return self.answer(200, {}) + if parts[6:] == ['state']: + return self.answer(200, {'usage': {'used': volume.get('used', 0)}}) if len(parts) == 6 and self.command == 'DELETE': if server.volumes.pop((pool, parts[5]), None) is None: return self.answer(404, 'Storage volume not found', 'error') @@ -126,9 +184,11 @@ def route(self): server.events.append(('image-delete', parts[2])) return self.answer(202, server.operation(), 'async') if parts[:2] == ['1.0', 'storage-pools'] and parts[3:] == ['resources']: - if parts[2] == 'fast': - return self.answer(500, 'no resources for dir', 'error') - return self.answer(200, {'space': {'used': 10 * 1024 ** 3, 'total': 100 * 1024 ** 3}}) + pool = server.pools[parts[2]] + if pool.get('unknown'): + return self.answer(500, 'Failed getting the pool resources', 'error') + total = pool.get('total') or size_in_bytes(pool['config'].get('size') or '0') + return self.answer(200, {'space': {'used': pool['used'], 'total': total}}) if parts == ['1.0', 'networks']: return self.answer(200, [{'name': 'incusbr0', 'type': 'bridge', 'managed': True}, {'name': 'eth0', 'type': 'physical', 'managed': False}]) @@ -183,7 +243,7 @@ def route(self): if rest == ['state'] and self.command == 'PUT': action = json.loads(body) server.events.append((action['action'], instance['name'], action.get('force', False))) - instance['status'] = {'start': 'Running', 'stop': 'Stopped'}[action['action']] + instance['status'] = {'start': 'Running', 'stop': 'Stopped', 'restart': 'Running'}[action['action']] return self.answer(202, server.operation(), 'async') if rest == ['exec'] and self.command == 'POST': server.events.append(('exec', instance['name'], json.loads(body))) @@ -258,6 +318,9 @@ def setUp(self): self.server.trusted = True self.server.instances.clear() self.server.events.clear() + self.server.pools = pools() + self.server.volumes.clear() + self.server.extensions = ['storage_pool_loop_resize'] def run_verb(self, verb, provisioner='container', secrets=None, **fields): request = {'version': 2, 'verb': verb, 'provisioner': provisioner, @@ -459,13 +522,13 @@ def test_status_without_an_id_is_the_server(self): self.assertEqual(code, 0, err) self.assertEqual(answer['state'], 'ready') self.assertEqual(answer['message'], f'Incus 6.18 answers at {self.address} and trusts berth') - self.assertEqual(answer['values'], {'pool': ['default', 'fast'], 'network': ['eth0', 'incusbr0']}) + self.assertEqual(answer['values'], {'pool': ['default', 'disk', 'fast'], 'network': ['eth0', 'incusbr0'], + 'instance': []}) rows = {row['label']: row['value'] for row in answer['details']} self.assertEqual((rows['Name'], rows['Storage'], rows['Kernel']), ('dock', 'zfs, dir', 'Linux 6.17.0')) def test_volumes_and_pools_are_created_listed_and_removed(self): - def act(action, **arguments): - return self.run_verb('act', action={'name': action, 'arguments': arguments}) + act = self.act code, answer, err = act('create-pool', name='bulk', driver='zfs', size='50GiB') self.assertEqual(code, 0, err) self.assertIn(('pool', {'name': 'bulk', 'driver': 'zfs', 'config': {'size': '50GiB'}}), self.server.events) @@ -489,8 +552,6 @@ def act(action, **arguments): def test_actions_answer_with_rows(self): code, answer, err = self.run_verb('act', action={'name': 'pools', 'arguments': {}}) self.assertEqual(code, 0, err) - self.assertEqual(answer['details'], [{'label': 'default', 'value': 'zfs · 10.0 GiB of 100.0 GiB used'}, - {'label': 'fast', 'value': 'dir · its use is not known'}]) code, answer, err = self.run_verb('act', action={'name': 'images'}) self.assertEqual(code, 0, err) self.assertEqual(answer['details'], [{'label': 'Ubuntu 26.04', 'value': 'container · 2.0 GiB · cached'}]) @@ -502,6 +563,195 @@ def test_actions_answer_with_rows(self): self.assertEqual(code, 1) self.assertIn("no action 'wipe'", err) + def act(self, action, **arguments): + return self.run_verb('act', action={'name': action, 'arguments': arguments}) + + def test_pools_are_a_table_that_says_which_can_grow(self): + self.server.pools['bare'] = {'name': 'bare', 'driver': 'btrfs', 'description': '', 'used': 0, + 'unknown': True, 'config': {'source': '/var/lib/incus/disks/bare.img', + 'size': '20GiB'}} + code, answer, err = self.act('pools') + self.assertEqual(code, 0, err) + self.assertEqual(answer['message'], f'4 storage pools on {self.address}') + rows = {row['label']: row['value'].split() for row in answer['details']} + self.assertEqual(answer['details'][0]['label'], 'Pool') + self.assertEqual(rows['Pool'], ['Driver', 'Source', 'Size', 'Used', 'Free', 'Can', 'grow']) + self.assertEqual(rows['default'], ['zfs', '/var/lib/incus/disks/default.img', '100.0', 'GiB', + '10.0', 'GiB', '90.0', 'GiB', 'yes']) + self.assertEqual(rows['fast'][:3], ['dir', '/var/lib/incus/storage-pools/fast', '200.0']) + fast = answer['details'][[r['label'] for r in answer['details']].index('fast')]['value'] + self.assertIn('no: a dir pool has no size of its own', fast) + disk = answer['details'][[r['label'] for r in answer['details']].index('disk')]['value'] + self.assertIn('no: it is on /dev/sdb, given when it was made; grow that instead', disk) + self.assertEqual(rows['bare'][:5], ['btrfs', '/var/lib/incus/disks/bare.img', '20GiB', '?', '?']) + # The columns line up: each starts at the same place in every row. + values = [row['value'] for row in answer['details']] + at = values[0].index('Source') + self.assertTrue(all(value[at - 2:at] == ' ' and value[at] != ' ' for value in values), values) + + def test_a_loop_backed_pool_grows_and_says_its_space_before_and_after(self): + code, answer, err = self.act('resize-pool', pool='default', size='150GiB') + self.assertEqual(code, 0, err) + self.assertIn(('pool-patch', 'default', {'config': {'size': '150GiB'}}), self.server.events) + self.assertEqual(answer['message'], f'The pool default on {self.address} is 150GiB now') + self.assertEqual(answer['details'], [ + {'label': 'Before', 'value': '100.0 GiB, 10.0 GiB used, 90.0 GiB free'}, + {'label': 'After', 'value': '150.0 GiB, 10.0 GiB used, 140.0 GiB free'}]) + self.assertIn('Before: 100.0 GiB, 10.0 GiB used, 90.0 GiB free', err) + self.assertIn('After: 150.0 GiB, 10.0 GiB used, 140.0 GiB free', err) + # The same size again changes nothing. + self.server.events.clear() + code, answer, err = self.act('resize-pool', pool='default', size='150GiB') + self.assertEqual(code, 0, err) + self.assertIn('already', answer['message']) + self.assertFalse([e for e in self.server.events if e[0] == 'pool-patch']) + + def test_a_pool_that_cannot_grow_is_refused_with_the_reason(self): + cases = [ + ({'pool': 'default', 'size': '50GiB'}, 'Incus only grows a pool'), + ({'pool': 'fast', 'size': '500GiB'}, 'a dir pool has no size of its own'), + ({'pool': 'disk', 'size': '1000GiB'}, 'it is on /dev/sdb, given when it was made'), + ({'pool': 'gone', 'size': '50GiB'}, 'no storage pool gone'), + ] + for arguments, said in cases: + code, _, err = self.act('resize-pool', **arguments) + self.assertEqual(code, 1, arguments) + self.assertIn(said, err) + self.server.extensions = [] + code, _, err = self.act('resize-pool', pool='default', size='200GiB') + self.assertEqual(code, 1) + self.assertIn('storage_pool_loop_resize', err) + self.assertFalse([e for e in self.server.events if e[0] == 'pool-patch']) + + def test_sizes_are_checked_before_anything_is_sent(self): + self.server.instances['web'] = {'name': 'web', 'type': 'container', 'status': 'Running', 'config': {}, + 'devices': {'root': {'type': 'disk', 'path': '/', 'pool': 'default', + 'size': '10GiB'}}, 'files': {}} + for size in ('50 GiB', '50gb', '1.5GiB', '-5GiB', 'big', '0GiB', '50G'): + for action, arguments in (('resize-pool', {'pool': 'default'}), + ('resize-volume', {'pool': 'default', 'volume': 'data'}), + ('resize-disk', {'instance': 'web'}), + ('create-volume', {'pool': 'default', 'name': 'data'}), + ('create-pool', {'name': 'new', 'driver': 'zfs'})): + code, _, err = self.act(action, size=size, **arguments) + self.assertEqual(code, 1, (action, size)) + self.assertIn(f'{size!r} is not a size Incus takes', err) + self.assertIn('such as 50GiB or 200GB', err) + code, _, err = self.act('resize-pool', pool='default', size='') + self.assertEqual(code, 1) + self.assertIn('Give the size', err) + self.assertFalse([e for e in self.server.events if e[0] not in ('client-cert',)]) + # Bytes alone and decimal units are sizes too. + self.assertEqual(self.act('resize-pool', pool='default', size='200GB')[0], 0) + self.assertEqual(self.act('resize-pool', pool='default', size='300000000000')[0], 0) + + def test_set_pool_changes_the_settings_incus_allows_and_no_others(self): + code, answer, err = self.act('set-pool', pool='default', key='volume.size', value='20GiB') + self.assertEqual(code, 0, err) + self.assertEqual(self.server.pools['default']['config']['volume.size'], '20GiB') + self.assertEqual(answer['details'], [{'label': 'Before', 'value': '(unset)'}, + {'label': 'After', 'value': '20GiB'}]) + code, answer, err = self.act('set-pool', pool='default', key='description', value='The fast disks') + self.assertEqual(code, 0, err) + self.assertEqual(self.server.pools['default']['description'], 'The fast disks') + self.assertEqual(self.act('set-pool', pool='default', key='zfs.clone_copy', value='rebase')[0], 0) + self.assertEqual(self.act('set-pool', pool='fast', key='rsync.bwlimit', value='50MB')[0], 0) + # An empty value unsets it, and the rest of the pool stays. + code, answer, err = self.act('set-pool', pool='default', key='volume.size', value='') + self.assertEqual(code, 0, err) + self.assertNotIn('volume.size', self.server.pools['default']['config']) + self.assertEqual(self.server.pools['default']['config']['zfs.clone_copy'], 'rebase') + self.assertEqual(self.server.pools['default']['description'], 'The fast disks') + self.server.events.clear() + for arguments, said in ( + ({'pool': 'default', 'key': 'source', 'value': '/dev/sdc'}, + 'does not change source of a zfs pool. It changes description, volume.size'), + ({'pool': 'default', 'key': 'zfs.pool_name', 'value': 'other'}, 'zfs.clone_copy, zfs.export'), + ({'pool': 'fast', 'key': 'zfs.export', 'value': 'false'}, 'rsync.bwlimit, rsync.compression'), + ({'pool': 'default', 'key': 'size', 'value': '200GiB'}, 'changed with resize-pool'), + ({'pool': 'default', 'key': 'volume.size', 'value': '20 gigs'}, 'not a size Incus takes'), + ({'pool': 'gone', 'key': 'description', 'value': 'x'}, 'no storage pool gone')): + code, _, err = self.act('set-pool', **arguments) + self.assertEqual(code, 1, arguments) + self.assertIn(said, err) + self.assertFalse([e for e in self.server.events if e[0].startswith('pool-')]) + + def test_a_volume_is_resized_and_a_block_volume_does_not_shrink(self): + self.server.volumes[('default', 'data')] = {'name': 'data', 'config': {'size': '10GiB'}, + 'content_type': 'filesystem', 'used_by': [], 'used': 2 * 1024 ** 3} + self.server.volumes[('default', 'disk')] = {'name': 'disk', 'config': {'size': '10GiB'}, + 'content_type': 'block', 'used_by': []} + code, answer, err = self.act('resize-volume', pool='default', volume='data', size='20GiB') + self.assertEqual(code, 0, err) + self.assertEqual(self.server.volumes[('default', 'data')]['config']['size'], '20GiB') + self.assertEqual(answer['details'], [{'label': 'Before', 'value': '10GiB, 2.0 GiB used'}, + {'label': 'After', 'value': '20GiB'}]) + # A filesystem volume may shrink to what it holds; Incus says when it cannot. + code, answer, err = self.act('resize-volume', pool='default', volume='data', size='5GiB') + self.assertEqual(code, 0, err) + self.assertIn('Incus refuses it if the volume holds more than 5GiB', err) + self.assertEqual(self.act('resize-volume', pool='default', volume='disk', size='20GiB')[0], 0) + code, _, err = self.act('resize-volume', pool='default', volume='disk', size='5GiB') + self.assertEqual(code, 1) + self.assertIn('Incus cannot shrink a block volume', err) + self.assertEqual(self.server.volumes[('default', 'disk')]['config']['size'], '20GiB') + code, _, err = self.act('resize-volume', pool='default', volume='gone', size='5GiB') + self.assertEqual(code, 1) + self.assertIn('no custom volume gone', err) + + def test_resize_disk_grows_the_root_disk_and_restarts_a_vm_only_when_asked(self): + self.assertEqual(self.create('vm')[0], 0) + self.server.events.clear() + code, answer, err = self.act('resize-disk', instance='berth-studio-2', size='80GiB') + self.assertEqual(code, 0, err) + self.assertEqual(self.server.instances['berth-studio-2']['devices']['root']['size'], '80GiB') + self.assertIn('sees the new size after a restart: run this again with restart=yes', answer['message']) + self.assertEqual(answer['details'], [{'label': 'Before', 'value': '60GiB'}, {'label': 'After', 'value': '80GiB'}]) + self.assertFalse([e for e in self.server.events if e[0] in ('restart', 'stop', 'start')]) + code, answer, err = self.act('resize-disk', instance='berth-studio-2', size='100GiB', restart='yes') + self.assertEqual(code, 0, err) + self.assertIn(('restart', 'berth-studio-2', False), self.server.events) + self.assertIn('was restarted', answer['message']) + # It only grows. + code, _, err = self.act('resize-disk', instance='berth-studio-2', size='50GiB') + self.assertEqual(code, 1) + self.assertIn('is 100GiB, and berth only grows it', err) + code, _, err = self.act('resize-disk', instance='berth-studio-2', size='120GiB', restart='maybe') + self.assertEqual(code, 1) + self.assertIn("restart is yes or no, not 'maybe'", err) + code, _, err = self.act('resize-disk', instance='nowhere', size='120GiB') + self.assertEqual(code, 1) + self.assertIn('no instance nowhere', err) + + def test_resize_disk_overrides_a_root_disk_from_the_profile(self): + self.server.instances['web'] = { + 'name': 'web', 'type': 'container', 'status': 'Running', 'config': {}, 'files': {}, + 'devices': {'data': {'type': 'disk', 'path': '/srv', 'source': 'data', 'pool': 'default'}}, + 'expanded_devices': {'root': {'type': 'disk', 'path': '/', 'pool': 'default', 'size': '10GiB'}, + 'data': {'type': 'disk', 'path': '/srv', 'source': 'data', 'pool': 'default'}}} + code, answer, err = self.act('resize-disk', instance='web', size='30GiB', restart='yes') + self.assertEqual(code, 0, err) + [patch] = [e for e in self.server.events if e[0] == 'patch'] + self.assertEqual(patch[2]['devices'], { + 'data': {'type': 'disk', 'path': '/srv', 'source': 'data', 'pool': 'default'}, + 'root': {'type': 'disk', 'path': '/', 'pool': 'default', 'size': '30GiB'}}) + self.assertIn('gets one of its own of 30GiB', err) + self.assertIn('A container has the new size at once; it was not restarted.', answer['message']) + self.assertFalse([e for e in self.server.events if e[0] == 'restart']) + + def test_change_leaves_a_disk_that_was_not_given_anew(self): + self.assertEqual(self.create()[0], 0) + self.assertEqual(self.act('resize-disk', instance='berth-studio-2', size='100GiB')[0], 0) + self.server.events.clear() + # berth's record still says 60GiB, which it sends with every change. + code, _, err = self.run_verb('change', id='berth-studio-2', changed=['memory'], + arguments={'memory': '4GiB', 'disk': '60GiB'}) + self.assertEqual(code, 0, err) + self.assertEqual(self.server.instances['berth-studio-2']['devices']['root']['size'], '100GiB') + code, _, err = self.run_verb('change', id='berth-studio-2', changed=['disk'], arguments={'disk': '80GiB'}) + self.assertEqual(code, 1) + self.assertIn('berth only grows it', err) + def test_change_sets_new_limits_and_grows_the_disk(self): self.create() code, answer, err = self.run_verb('change', id='berth-studio-2',