Create a new VM and set the disk size via cli

Teucrus

Active Member
Jul 7, 2018
15
0
41
38
Happy new year everyone.

I am trying to use the CLI to create a new VM with a pre-existing image.
Specifically I am trying to install a mikrotik CHR using the bash script found here: https://wiki.mikrotik.com/wiki/Manual:CHR_ProxMox_installation

Everything works fine, but a minor inconvenience. The script does not set a disk size and I have to manually set it after it's creation.
I was wondering if I can set the disk size during it's creation.

I believe the size must be set here:

qm create $vmID \ --name chr-$version \ --net0 virtio,bridge=vmbr0 \ --bootdisk virtio0 \ --ostype l26 \ --memory 256 \ --onboot no \ --sockets 1 \ --cores 1 \ --virtio0 local:$vmID/vm-$vmID-disk-1.qcow2

In the documentation I found: "Use the special syntax STORAGE_ID:SIZE_IN_GiB to allocate a new volume."
but couldn't find an example use for it.

Appreciate your help and time.
Regards
 
Last edited:
hi,

In the documentation I found: "Use the special syntax STORAGE_ID:SIZE_IN_GiB to allocate a new volume."
but couldn't find an example use for it.
it means like: qm create 123 ... --virtio0 local:8 will create a disk on storage local with size 8 GiB. if you want it to be a qcow2 disk you can use --virtio0 local:8,format=qcow2
 
hi,


it means like: qm create 123 ... --virtio0 local:8 will create a disk on storage local with size 8 GiB. if you want it to be a qcow2 disk you can use --virtio0 local:8,format=qcow2
So, in the above example, it would be like this: qm create 123 ... --virtio0 local:8:$vmID/vm-$vmID-disk-1.qcow2 ?
 
So, in the above example, it would be like this: qm create 123 ... --virtio0 local:8:$vmID/vm-$vmID-disk-1.qcow2 ?
no, it would be like what i wrote above. this syntax is only for creating a new disk.

if you have a pre-existing disk file that you're giving to the VM (which is the case for that bash script) then you can't change the size of the disk at creation.

you can however just increase the size of the disk after the VM is created... (qm resize <vmid> <disk> <size> , for example qm resize 123 virtio0 +10G will increase the size by 10G. shrinking is not supported) but be aware that the partition table inside the disk will be unaffected by the change, so you'll still have to adjust/extend the partition table from inside the VM.
 
Hi @oguz

I have tried to run the script but it fails to create the image.

this is the output :

Bash:
root@pve-router:/tmp# ./mki.sh
############## Start of Script ##############

## Checking if temp dir is available...
-- Directory exists!
Install unzip
Hit:1 http://security.debian.org bullseye-security InRelease
Hit:2 http://download.proxmox.com/debian/pve bullseye InRelease
Hit:3 http://ftp.es.debian.org/debian bullseye InRelease
Hit:4 http://ftp.es.debian.org/debian bullseye-updates InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
unzip is already the newest version (6.0-26).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
## Preparing for image download and VM creation!
Please input CHR version to deploy ( 6.49.1 (Stable) 6.49rc2 (Testing) 7.1 (Testing)):7.3
-- Downloading CHR 7.3 image file.
---------------------------------------------------------------------------
--2022-06-07 21:26:35--  https://download.mikrotik.com/routeros/7.3/chr-7.3.img.zip
Resolving download.mikrotik.com (download.mikrotik.com)... 159.148.147.204, 159.148.172.226, 2a02:610:7501:1000::204, ...
Connecting to download.mikrotik.com (download.mikrotik.com)|159.148.147.204|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 38885743 (37M) [application/zip]
Saving to: ‘chr-7.3.img.zip’

chr-7.3.img.zip                    100%[================================================================>]  37.08M  25.0MB/s    in 1.5s

2022-06-07 21:26:37 (25.0 MB/s) - ‘chr-7.3.img.zip’ saved [38885743/38885743]

Archive:  chr-7.3.img.zip
  inflating: chr-7.3.img
---------------------------------------------------------------------------
== Printing list of VM's on this hypervisor!

Please Enter free vm ID to use:666

-- Creating VM image dir!
WARNING: Image format was not specified for '/root/temp/chr-7.3.img' and probing guessed raw.
         Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
         Specify the 'raw' format explicitly to remove the restrictions.
Image resized.
-- Creating new CHR VM
unable to create VM 666 - no such logical volume pve/vm-666-disk-0
-- Import RAW image to local-lvm
Configuration file 'nodes/pve-router/qemu-server/666.conf' does not exist
############## End of Script ##############
root@pve-router:/tmp#

Is it possible that by updating the version of qemu, the way of setting the disk type has changed?

thank you for your cooperation

Diego
 
Your issue does not have anything to do with qemu, rather PVE specific steps.
The original script creates a volume/disk and then a VM that points to that disk:
Code:
qemu-img convert \
    -f raw \
    -O qcow2 \
    /root/temp/chr-$version.img \
    /var/lib/vz/images/$vmID/vm-$vmID-disk-1.qcow2

You seem to have modified your script, as you attempt to create a VM that points to non-existing disk, which fails. Then you attempt to import a disk using a VM ID that doesnt exist...

If your PVE is updated you should just use
Code:
--scsi0 $STORAGE:0,import-from=$ISO/$OSIMAGE
in your VM create command

Otherwise, you need to create a VM _without_ the disk, then after VM config exists - import and set it:
Code:
   qm importdisk $vmid $ISO/$OSIMAGE $STORAGE --format raw
   qm set $vmid --"$DEVICE"0 $STORAGE:vm-$vmid-disk-0


Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox
 
Your issue does not have anything to do with qemu, rather PVE specific steps.
The original script creates a volume/disk and then a VM that points to that disk:
Code:
qemu-img convert \
    -f raw \
    -O qcow2 \
    /root/temp/chr-$version.img \
    /var/lib/vz/images/$vmID/vm-$vmID-disk-1.qcow2

You seem to have modified your script, as you attempt to create a VM that points to non-existing disk, which fails. Then you attempt to import a disk using a VM ID that doesnt exist...

If your PVE is updated you should just use
Code:
--scsi0 $STORAGE:0,import-from=$ISO/$OSIMAGE
in your VM create command

Otherwise, you need to create a VM _without_ the disk, then after VM config exists - import and set it:
Code:
   qm importdisk $vmid $ISO/$OSIMAGE $STORAGE --format raw
   qm set $vmid --"$DEVICE"0 $STORAGE:vm-$vmid-disk-0


Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox

thanks for your help, but I have not been able to get it to work.

I have tried as you say it does not finish, it looks like it is because of the source format of the MikroTik img, with previous versions (7.1) the script works but with the new 7.3.x) it does not...

thanks again