How to specify the disk size and IP address when creating an instance through pylxd?

The example code is as follows

client = Client(
    verify=False,
    endpoint='https://127.0.0.1:8443',
    cert=('/etc/lxd/keys/lxd.crt', '/etc/lxd/keys/lxd.key'))



config = {
    'name': 'debian',
    'type': 'container',
    'source': {
        'type': 'image',
        "mode": "pull",
        "protocol": "simplestreams",
        "server": "https://images.linuxcontainers.org",
        "alias": "debian/bullseye/amd64"
    },
    'config': {
        'limits.cpu': '1',
        'limits.memory': '2048MiB',
        'security.devlxd': '0'
    }
}
instance = client.instances.create(config, wait=True)
instance.start()
print(instance)

You should be able to set something like:

'devices': {
  'root': {
    'type': "disk",
    'pool': "default",
    'size': "10GiB",
    'path': "/"
  },
  'eth0': {
    'type': "nic",
    'network': "lxdbr0",
    'name': "eth0",
    'ipv4.address': "1.2.3.4"
  }
}

Helo, The IP configuration looks normal, but the storage configuration does not seem to take effect,code show as below

config = {
    'name': 'debian',
    'type': 'container',
    'source': {
        'type': 'image',
        "mode": "pull",
        "protocol": "simplestreams",
        "server": "https://images.linuxcontainers.org",
        "alias": "debian/bullseye/amd64"
    },
    'config': {
        'limits.cpu': '1',
        'limits.memory': '2048MiB',
        'security.devlxd': '0'
    },
    'devices': {
        'eth0': {'type': "nic", 'network': "lxdbr0", 'name': "eth0", 'ipv4.address': "10.5.11.11"},
        'root': {'type': "disk", 'pool': "default", 'size': "5GiB", 'path': "/"},
    }
}

instance = client.instances.create(config, wait=True)
instance.start()
print(instance)

Network Configuration

root@lxc:~# lxc list
WARNING: cgroup v2 is not fully supported yet, proceeding with partial confinement
+--------+---------+-------------------+------+-----------+-----------+
|  NAME  |  STATE  |       IPV4        | IPV6 |   TYPE    | SNAPSHOTS |
+--------+---------+-------------------+------+-----------+-----------+
| debian | RUNNING | 10.5.11.11 (eth0) |      | CONTAINER | 0         |
+--------+---------+-------------------+------+-----------+-----------+
root@lxc:~# lxc exec debian -- ifconfig
WARNING: cgroup v2 is not fully supported yet, proceeding with partial confinement
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.5.11.11  netmask 255.255.255.0  broadcast 10.5.11.255
        inet6 fe80::216:3eff:fe72:2dfc  prefixlen 64  scopeid 0x20<link>
        ether 00:16:3e:72:2d:fc  txqueuelen 1000  (Ethernet)
        RX packets 7  bytes 1465 (1.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 15  bytes 1934 (1.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Hard disk configuration does not seem to take effect

root@lxc:~# lxc exec debian -- df -h
WARNING: cgroup v2 is not fully supported yet, proceeding with partial confinement
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop3       19G  356M   18G   2% /
none            492K  4.0K  488K   1% /dev
udev            1.9G     0  1.9G   0% /dev/tty
tmpfs           100K     0  100K   0% /dev/.lxd-mounts
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           772M   52K  772M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
root@lxc:~# lxc storage list
WARNING: cgroup v2 is not fully supported yet, proceeding with partial confinement
+---------+--------+--------------------------------------------+-------------+---------+
|  NAME   | DRIVER |                   SOURCE                   | DESCRIPTION | USED BY |
+---------+--------+--------------------------------------------+-------------+---------+
| default | btrfs  | /var/snap/lxd/common/lxd/disks/default.img |             | 3       |
+---------+--------+--------------------------------------------+-------------+---------+
root@lxc:~# lxc storage info default
WARNING: cgroup v2 is not fully supported yet, proceeding with partial confinement
info:
  description: ""
  driver: btrfs
  name: default
  space used: 373.25MB
  total space: 20.00GB
used by:
  images:
  - f59dd56e72faa12284eed2fd381a87d9fc71392e221d679c1f6e0870d2bcbcd9
  instances:
  - debian
  profiles:
  - default
root@lxc:~# lxc storage show default
WARNING: cgroup v2 is not fully supported yet, proceeding with partial confinement
config:
  size: 20GB
  source: /var/snap/lxd/common/lxd/disks/default.img
description: ""
name: default
driver: btrfs
used_by:
- /1.0/images/f59dd56e72faa12284eed2fd381a87d9fc71392e221d679c1f6e0870d2bcbcd9
- /1.0/instances/debian
- /1.0/profiles/default
status: Created
locations:
- none

You’re using btrfs, btrfs does not report its quotas in df -h, so chances are that the 5GiB quota is indeed in place.

I used the data type df -h storage device 5GiB?

Now he looks like everything is normal

client = Client(
    verify=False,
    endpoint='https://xxx:8443',
    cert=('xxx/lxd.crt', 'xxx/lxd.key'))


if not client.storage_pools.exists("debian"):
    client.storage_pools.create({
        "name": "debian",
        "driver": "btrfs",
        "config": {"size": "3GB"}
    })

config = {
    'name': 'debian',
    'type': 'container',
    'source': {
        'type': 'image',
        "mode": "pull",
        "protocol": "simplestreams",
        "server": "https://images.linuxcontainers.org",
        "alias": "debian/bullseye/amd64"
    },
    'config': {
        'limits.cpu': '1',
        # 'limits.cpu.allowance': '50%',
        'limits.memory': '2048MiB',
        'security.devlxd': '0'
    },
    'devices': {
        'ssh': {'type': 'proxy', 'connect': 'tcp::22', 'listen': 'tcp:0.0.0.0:2222'},
        'eth0': {'type': "nic", 'network': "lxdbr0", 'name': "eth0", 'ipv4.address': "10.5.11.11"},
        'root': {'type': "disk", 'pool': "debian", 'path': "/"},
    }
}

instance = client.instances.create(config, wait=True)
instance.start()
root@lxc:~# lxc exec debian -- df -h
WARNING: cgroup v2 is not fully supported yet, proceeding with partial confinement
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop5      2.8G  347M  2.0G  15% /
none            492K  4.0K  488K   1% /dev
udev            1.9G     0  1.9G   0% /dev/tty
tmpfs           100K     0  100K   0% /dev/.lxd-mounts
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           772M   52K  772M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock