Change LUKS UUID (without cryptsetup > 1.2)

Posted on

Change LUKS UUID (without cryptsetup > 1.2) – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about linux, luks, uuid, , .

According to the cryptsetup changelog (1.2.0):

Allow explicit UUID setting in luksFormat and allow change it later
in luksUUID (–uuid parameter).

My problem is that the current Debian stable (squeeze) uses 1.1.3 – Is there a way to change the LUKS UUID if I can not upgrade this version? (Maybe with an other program)?

Solution :

The luks format looks pretty simple and is text based so should be easy to manipulate. I wrote this in about 10 minutes that should do it.

Backup your luks headers first!

#!/usr/bin/python
import sys
import uuid
import re

if __name__ == "__main__":
    haveuuid = False
    val = ""

    f = open(sys.argv[1], "r+")
    if len(sys.argv) > 2:
       if not re.match('[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}', 
                                                        sys.argv[2]):
          print "Not a valid UUID"
          sys.exit(1)
       else:
          val = sys.argv[2]
    else:
       # Create a new UUID
       val = uuid.uuid1()
    # Be happy this is LUKS
    if f.read(4) == "LUKS":
        # This is the start position of the UUID field.
        f.seek((32*5)+8, 0)

        f.write(val.__str__())
        f.close()
    else:
        print "Not a luks image"

Run it with python /path/to/script.py /path/to/luks/device
Optionally to specify a UUID: python /path/to/script.py /path/to/luks/device abcdef01-abcd-abcd-abcd-abcdef012345

I don’t know if it works, but you can boot on a liveCD with cryptsetup >= 1.2.0 and change the UUID of your volume.

Leave a Reply

Your email address will not be published.