Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
    
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 6:56
add comment

migrated from superuser.com Jul 29 '12 at 19:11

This question came from our site for computer enthusiasts and power users.

2 Answers

up vote 1 down vote accepted
+50

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

share|improve this answer
    
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 4 '12 at 21:53
    
that generates a new uuid, you never specified you wanted to use a specific uuid. –  Matthew Ife Aug 5 '12 at 0: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 5 '12 at 12:31
    
@vbence I added the ability to specify the uuid you want to write. –  Matthew Ife Aug 8 '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 9 '12 at 8:25
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.