2

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

vbence
  • 213
  • 2
  • 12
  • Why do you want to change it? – mgorven Jul 29 '12 at 19:30
  • As part of a backup-restore procedure I would like to re-create a partition which will be in sync with the existing system. (For example crypttab uses the UUID). - I do not want to change `crypttab` itself as there is no guarantee that it is only used in there. – vbence Jul 30 '12 at 06:56

2 Answers2

1

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

vbence
  • 213
  • 2
  • 12
Matthew Ife
  • 23,357
  • 3
  • 55
  • 72
  • Thanks for the answer. Only one thing is unclear for me how can you pass the desired new UUID for the script (also what does `uuid.uuid1` do)? – vbence Aug 04 '12 at 21:53
  • that generates a new uuid, you never specified you wanted to use a specific uuid. – Matthew Ife Aug 05 '12 at 00:48
  • I'd like to replicate the `--uuid` behavior of cryptsetup. This would be part of a backup-restore procedure (as I already mentioned in my first comment). – vbence Aug 05 '12 at 12:31
  • @vbence I added the ability to specify the uuid you want to write. – Matthew Ife Aug 08 '12 at 21:03
  • Thanks for the example. I did not realize that the UUID is stored in a textual representation, and I thought your script was missing the conversion part. – vbence Aug 09 '12 at 08:25
0

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.