Linux – Change LUKS UUID (without cryptsetup > 1.2)

linuxluksuuid

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)?

Best Answer

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

Related Topic