1*4882a593Smuzhiyun# Extract UUID from ${ROOTFS}, which must have been built 2*4882a593Smuzhiyun# by the time that this function gets called. Only works 3*4882a593Smuzhiyun# on ext file systems and depends on tune2fs. 4*4882a593Smuzhiyundef get_rootfs_uuid(d): 5*4882a593Smuzhiyun import subprocess 6*4882a593Smuzhiyun rootfs = d.getVar('ROOTFS') 7*4882a593Smuzhiyun output = subprocess.check_output(['tune2fs', '-l', rootfs], text=True) 8*4882a593Smuzhiyun for line in output.split('\n'): 9*4882a593Smuzhiyun if line.startswith('Filesystem UUID:'): 10*4882a593Smuzhiyun uuid = line.split()[-1] 11*4882a593Smuzhiyun bb.note('UUID of %s: %s' % (rootfs, uuid)) 12*4882a593Smuzhiyun return uuid 13*4882a593Smuzhiyun bb.fatal('Could not determine filesystem UUID of %s' % rootfs) 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun# Replace the special <<uuid-of-rootfs>> inside a string (like the 16*4882a593Smuzhiyun# root= APPEND string in a syslinux.cfg or systemd-boot entry) with the 17*4882a593Smuzhiyun# actual UUID of the rootfs. Does nothing if the special string 18*4882a593Smuzhiyun# is not used. 19*4882a593Smuzhiyundef replace_rootfs_uuid(d, string): 20*4882a593Smuzhiyun UUID_PLACEHOLDER = '<<uuid-of-rootfs>>' 21*4882a593Smuzhiyun if UUID_PLACEHOLDER in string: 22*4882a593Smuzhiyun uuid = get_rootfs_uuid(d) 23*4882a593Smuzhiyun string = string.replace(UUID_PLACEHOLDER, uuid) 24*4882a593Smuzhiyun return string 25