Changing VMID of a VM

f4242

Active Member
Dec 19, 2016
79
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,137
101
68
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
79
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
54
12
28
31
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
3
0
6
62
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
5
8
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.
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
4
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
4,020
488
88
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...
 

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!