Changing VMID of a VM

f4242

Active Member
Dec 19, 2016
82
3
28
Quebec, QC
Hello,

I use the last bit of ip address to generate my VMID. I refreshed a VM using a temporary ID to replace an existing VM. I made that VM into production and retired the old one. How could I change the VMID? I'm using lvm-thin local storage.

I know I could backup&restore, but I don't like that way of doing a simple thing as changing the VMID.

Thanks.
 

mira

Proxmox Staff Member
Staff member
Aug 1, 2018
1,763
194
83
You can create a full clone and then delete the old one. Or manually rename the config and everything in it referencing the VMID.
 

f4242

Active Member
Dec 19, 2016
82
3
28
Quebec, QC
Well, the clone is very similar to the baclup&restore, very long execution time depending of the content of the disk.

What should I rename and edit? I could write a little helper script that would automate that.

Thanks.
 

timonych

Active Member
Aug 8, 2017
60
13
28
32
HI,

First of all - This script is handmade, but I have ckecked it.

BE SURE THAY YOU HAVE BACKUP OF YOUR VM.

Bash:
export vgNAME=vg-images newVMID=173 oldVMID=175 ;  \
for i in $(lvs -a|grep $vgNAME | awk '{print $1}' | grep $oldVMID); \
do lvrename $vgNAME/vm-$oldVMID-disk-$(echo $i | awk '{print substr($0,length,1)}') vm-$newVMID-disk-$(echo $i | awk '{print substr($0,length,1)}'); done; \
sed -i "s/$oldVMID/$newVMID/g" /etc/pve/qemu-server/$oldVMID.conf; mv /etc/pve/qemu-server/$oldVMID.conf /etc/pve/qemu-server/$newVMID.conf; \
unset vgNAME newVMID oldVMID;

You need to change 3 Variables:
vgNAME
newVMID
oldVMID

vgNAME should consist You Volume Group Name
newVMID should consist new ID
oldVMID should consist old ID

This script only changes for one Volume Group.

By the way, if You need to do it manually You should do this changes:
1. Rename LVM to correct as new ID by command lvrename
2. Rename conf file to new ID in /etc/pve/qemu-server/
3. Replace all vmid in conf file to new ID or names of Your LVM.
 

pavlos

Member
Dec 17, 2017
7
1
23
63
lvs -a outputs nothing.

Code:
pveversion
pve-manager/6.2-15/48bd51b6 (running kernel: 5.4.65-1-pve)

Code:
qm list
      VMID NAME                 STATUS     MEM(MB)    BOOTDISK(GB) PID    
       100 lite                 running    4096              32.00 22323  
       102 pf                   stopped    2048              32.00 0

all I want is to change the VMID 100 to 175, make a backup, move it to another node, and restore.

update (after a few hours):
I rcp the disk image from pve4: to pve5:, went into pve5, restore, and gave it another vmid.
 
Last edited:

xickwy

Member
Nov 10, 2019
10
7
8
33
HI,

First of all - This script is handmade, but I have ckecked it.

BE SURE THAY YOU HAVE BACKUP OF YOUR VM.

Bash:
export vgNAME=vg-images newVMID=173 oldVMID=175 ;  \
for i in $(lvs -a|grep $vgNAME | awk '{print $1}' | grep $oldVMID); \
do lvrename $vgNAME/vm-$oldVMID-disk-$(echo $i | awk '{print substr($0,length,1)}') vm-$newVMID-disk-$(echo $i | awk '{print substr($0,length,1)}'); done; \
sed -i "s/$oldVMID/$newVMID/g" /etc/pve/qemu-server/$oldVMID.conf; mv /etc/pve/qemu-server/$oldVMID.conf /etc/pve/qemu-server/$newVMID.conf; \
unset vgNAME newVMID oldVMID;

You need to change 3 Variables:
vgNAME
newVMID
oldVMID

vgNAME should consist You Volume Group Name
newVMID should consist new ID
oldVMID should consist old ID

This script only changes for one Volume Group.

By the way, if You need to do it manually You should do this changes:
1. Rename LVM to correct as new ID by command lvrename
2. Rename conf file to new ID in /etc/pve/qemu-server/
3. Replace all vmid in conf file to new ID or names of Your LVM.
Works like a charm. It boots and works fine.

For the people that asks for the VG, run lvs -a and the second column shows your VG.

Thanks for the script. I've do a sh script from it. It do a search if it's on a volume group. If not, it exits. Only put the old VMID and the New. The script does the rest

Code:
#!/bin/bash

echo Put the VMID to change
read oldVMID
case $oldVMID in
    ''|*[!0-9]*)
        echo bad input. Exiting
        exit;;
    *)
        echo Old VMID - $oldVMID ;;
esac
echo
echo Put the new VMID
read newVMID
case $newVMID in
    ''|*[!0-9]*)
        echo bad input. Exiting
        exit;;
    *)
        echo New VMID - $newVMID ;;
esac
echo

vgNAME="$(lvs --noheadings -o lv_name,vg_name | grep $oldVMID | awk -F ' ' '{print $2}' | uniq -d)"

case $vgNAME in
    "")
        echo Machine not in Volume Group. Exiting
        exit;;
    *)
        echo Volume Group - $vgNAME ;;
esac

for i in $(lvs -a|grep $vgNAME | awk '{print $1}' | grep $oldVMID);
do lvrename $vgNAME/vm-$oldVMID-disk-$(echo $i | awk '{print substr($0,length,1)}') vm-$newVMID-disk-$(echo $i | awk '{print substr($0,length,1)}');
done;
sed -i "s/$oldVMID/$newVMID/g" /etc/pve/qemu-server/$oldVMID.conf;
mv /etc/pve/qemu-server/$oldVMID.conf /etc/pve/qemu-server/$newVMID.conf;

echo Ta-Da!
 

bcart167

New Member
Dec 31, 2020
1
0
1
38
HI,

First of all - This script is handmade, but I have ckecked it.

BE SURE THAY YOU HAVE BACKUP OF YOUR VM.

Bash:
export vgNAME=vg-images newVMID=173 oldVMID=175 ;  \
for i in $(lvs -a|grep $vgNAME | awk '{print $1}' | grep $oldVMID); \
do lvrename $vgNAME/vm-$oldVMID-disk-$(echo $i | awk '{print substr($0,length,1)}') vm-$newVMID-disk-$(echo $i | awk '{print substr($0,length,1)}'); done; \
sed -i "s/$oldVMID/$newVMID/g" /etc/pve/qemu-server/$oldVMID.conf; mv /etc/pve/qemu-server/$oldVMID.conf /etc/pve/qemu-server/$newVMID.conf; \
unset vgNAME newVMID oldVMID;

You need to change 3 Variables:
vgNAME
newVMID
oldVMID

vgNAME should consist You Volume Group Name
newVMID should consist new ID
oldVMID should consist old ID

This script only changes for one Volume Group.

By the way, if You need to do it manually You should do this changes:
1. Rename LVM to correct as new ID by command lvrename
2. Rename conf file to new ID in /etc/pve/qemu-server/
3. Replace all vmid in conf file to new ID or names of Your LVM.
I am new to both Linux and Proxmox so I apologise in advance for my complete lack of knowledge and needing to be hand-fed everything. I have figured out how to get my vgNAME, but the 3 variables I need to replace in that script are they just the ones on the top line? Or do I need to replace $newVMID, $oldVMID and vgNAME as well? And where do I put it? Do I just copy and paste into the shell through the web GUI? So is this code correct? I don't want to run it and break everything. I am going from VM 103 to VM 102.

Bash:
export pvepve=vg-images newVMID=102 oldVMID=103 ;  \
for i in $(lvs -a|grep $pve | awk '{print $1}' | grep $oldVMID); \
do lvrename $pve/vm-$oldVMID-disk-$(echo $i | awk '{print substr($0,length,1)}') vm-$newVMID-disk-$(echo $i | awk '{print substr($0,length,1)}'); done; \
sed -i "s/$oldVMID/$newVMID/g" /etc/pve/qemu-server/$oldVMID.conf; mv /etc/pve/qemu-server/$oldVMID.conf /etc/pve/qemu-server/$newVMID.conf; \
unset pve 102 103;

Thanks in advance.
 
Last edited:

george

New Member
Jan 11, 2021
5
1
3
@xickwy

The shell sh script does not work as it is. Why is "uniq -d" needed ? Aren't VMIDs unique in a cluster ? If this is removed (or just replaced with "uniq", no "-d") then it works.

Or if the problem is about someone having too many VMs or sets the VMIDs his own then maybe use "grep -w"

Code:
vgNAME="$(lvs --noheadings -o lv_name,vg_name | grep -w $oldVMID | awk -F ' ' '{print $2}'"

e.g. to discriminate between 101 and 1011 ?
 
Last edited:

oguz

Proxmox Staff Member
Staff member
Nov 19, 2018
5,207
677
118
the correct way of doing this is to make a backup/restore as it was pointed out before. the reason for this is that the backing disk also needs to be renamed, the scripts that were shared here only work in an lvm/lvmthin setup but not on the other ones.

you can rename/copy the configuration and edit it manually, but you have to also copy the disk and name it appropriately on the backing storage following the regular naming scheme of the disks vm-VMID-disk-N
 

mkellerman

New Member
Apr 3, 2021
1
0
1
42
the correct way of doing this is to make a backup/restore as it was pointed out before. the reason for this is that the backing disk also needs to be renamed, the scripts that were shared here only work in an lvm/lvmthin setup but not on the other ones.

you can rename/copy the configuration and edit it manually, but you have to also copy the disk and name it appropriately on the backing storage following the regular naming scheme of the disks vm-VMID-disk-N
Where can we suggest features?
this would be such a convenient feature for people with OCD like me that likes keeping their VMs in a particular order...
 

sdet00

Active Member
Nov 18, 2017
44
11
28
The inability the rename the VMID has to be one of my top pet peeves with Proxmox. It's an ID that should be unique, yet it cannot be changed? I'll try this script, but still a bit frustrating that we have to jump through hoops to do some basic tasks. Backing up and restore isn't an option because it is my PBS VM that I need to change the ID of :)
 

dietmar

Proxmox Staff Member
Staff member
Apr 28, 2005
17,124
521
133
Austria
www.proxmox.com
The inability the rename
Please note that each VM has a NAME, and you can change the name as you want.

the VMID has to be one of my top pet peeves with Proxmox. It's an ID that should be unique, yet it cannot be changed?

Exactly. The ID is used to uniquely identify the VM. Changing the ID would be counter productive, because you them have multiple ID for the same VM over time.

I'll try this script, but still a bit frustrating that we have to jump through hoops to do some basic tasks.
This is not a basic task. Instead, this is something you should not do at all.

Backing up and restore isn't an option because it is my PBS VM that I need to change the ID of :)
you can also backup/restorge to/from any file base storage.
 

sdet00

Active Member
Nov 18, 2017
44
11
28
This is not a basic task. Instead, this is something you should not do at all.

Unfortunately it is necessary if you want to avoid conflicts, especially with things such as backups. I have three Proxmox hosts that have been converged into a cluster - and some of the VMs use the same ID. Therefore I must change the ID to avoid issues. Otherwise I wouldn't bother.

Personally I would much prefer it if it were a randomly generated alphanumeric UUID. In the future I'll be ensuring that all VMs that are created are using a pre-defined scope and each VM gets documented to avoid future conflicts, but for now I have to find a way to change the VMID.
 

oguz

Proxmox Staff Member
Staff member
Nov 19, 2018
5,207
677
118
I tried it this way, and you cannot change the id. The vm will have the same id after restore, no way to edit/change it.
of course there is :)

you can use the CLI tool qmrestore. so if you've made a backup and the archive is called vzdump-qemu-666-2022_02_02-12_25_00.vma.zst then you can restore it to a new VMID:
Code:
qmrestore vzdump-qemu-666-2022_02_02-12_25_00.vma.zst 999

and it should make a new VM with the ID 999.


edit:
you can also do it over the GUI too.
instead of clicking the VM and restoring the backup there, you can click on the storage where you made the backup and restore it to an arbitrary VMID ;)

hope this helps!
 
Last edited:

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get your own in 60 seconds.

Buy now!