#955733 cloud-init does not set locale specified in user data

#955733#5
Date:
2020-04-04 10:49:42 UTC
From:
To:
I am launching a Buster 10.3 EC2 instance with the following user data
to initialise and set the default locale:

    #cloud-config
    hostname: argon
    locale: en_GB.UTF-8
    timezone: Europe/London

This fails to set the locale, and "systemctl status cloud-init" shows:

    Apr 04 11:00:30 argon systemd[1]: Starting Apply the settings specified in cloud-config...
    Apr 04 11:00:43 argon cloud-init[546]: Generating locales (this might take a while)...
    Apr 04 11:00:43 argon cloud-init[546]: Generation complete.
    Apr 04 11:00:43 argon cloud-init[546]: *** update-locale: Error: invalid locale settings:  LANG=en_GB.UTF-8
    Apr 04 11:00:43 argon cloud-init[546]: Cloud-init v. 18.3 running 'modules:config' at Sat, 04 Apr 2020 10:00:41 +0000. Up 21.77 seconds.
    Apr 04 11:00:43 argon cloud-init[546]: 2020-04-04 10:00:43,604 - util.py[WARNING]: Running module locale (<module 'cloudinit.config.cc_locale' from '/usr/lib/python3/dist-packages/cloudinit/config/cc_locale.py'>) failed
    Apr 04 11:00:45 argon systemd[1]: cloud-config.service: Main process exited, code=exited, status=1/FAILURE
    Apr 04 11:00:45 argon systemd[1]: cloud-config.service: Failed with result 'exit-code'.
    Apr 04 11:00:45 argon systemd[1]: Failed to start Apply the settings specified in cloud-config.

The log file "cloud-init.log" shows the error is caused by failing to
generate the requested locale.

    2020-04-04 10:00:43,005 - debian.py[DEBUG]: Generating locales for en_GB.UTF-8
    2020-04-04 10:00:43,005 - util.py[DEBUG]: Running command ['locale-gen', 'en_GB.UTF-8'] with allowed return codes [0] (shell=False, capture=False)

The command "locale-gen" does not accept a locale as an argument, but
only generates the locales listed in "/etc/locale.gen".  So the locale
requested does not get generated, and causes the subsequent failure when
setting locale.

    2020-04-04 10:00:43,196 - util.py[DEBUG]: Running command ['update-locale', '--locale-file=/etc/default/locale', 'LANG=en_GB.UTF-8'] with allowed return codes [0] (shell=False, capture=False)

Nick

#955733#10
Date:
2022-03-12 12:40:58 UTC
From:
To:
Tested on bullseye genericcloud and same issue of locale not working

https://cloud.debian.org/images/cloud/bullseye/daily/20220310-944/debia
n-11-genericcloud-amd64-daily-20220310-944.qcow2

cloud-init 20.4.1-2+deb11u1

#955733#15
Date:
2023-06-29 03:51:46 UTC
From:
To:
Confirmed that the behavior as described is still an issue in cloud-init
22.4.2-1 in bookworm.

I think that each of the components is working correct if viewed in
isolation, but the end result is not what you expect.  If we document
that you must first populate /etc/locale.gen with the desired list of
available locales, that may be sufficient.  For example, the following
sets the desired locale:

admin@ip-10-0-3-7:~$ sudo cat /var/lib/cloud/instance/user-data.txt
#cloud-config
write_files:
    - path: /etc/locale.gen
      content: ZW5fR0IuVVRGLTggVVRGLTgK
      encoding: base64
locale: en_GB.UTF-8
admin@ip-10-0-3-7:~$ echo "ZW5fR0IuVVRGLTggVVRGLTgK" | openssl base64 -d
en_GB.UTF-8 UTF-8

Do Ubuntu systems list all known locales in /etc/locale.gen?  That would
make the locale module work without additional configuration, but seems
unnecessarily expensive.

noah

#955733#20
Date:
2023-07-08 13:09:03 UTC
From:
To:
This feels like a workaround for the failure of cloud-init setting the locale
on a Debian install.

The locale configuration in cloud-init is expecting locale-gen to configure the
generated locales with the specified locale.

https://git.launchpad.net/ubuntu/+source/cloud-init/tree/cloudinit/distros/debian.py#n401

Debian just ignores the locale passed as a parameter.

Ubuntu does not list all locales in /etc/locale.gen, but their version of
locale-gen will update /etc/locale.gen to add/uncomment the specified locale
before generating locales.

https://git.launchpad.net/ubuntu/+source/glibc/tree/debian/local/usr_sbin/locale-gen?h=ubuntu/jammy#n115

It seems cloud-init has expectation on what locale-gen will do (i.e. _update_
and generate), and that is not the case.

I don't know if cloud-init should take on the responsibility for updating
/etc/locale.gen, or if locale-gen should update /etc/locale.gen if passed a
parameter.