I thought I could do something like:
sudo unshare -T bash -c 'date -s "$1" && foobar' sh "$(date -d -1day)"
so foobar
would see a different system time from the rest of the system. However, it seems the change of system time is not contained. It changes the system time of the whole system.
This LWN article seems to suggest this namespace was meant for the use I tried to give it.
System calls that adjust the system time will, when called outside of the root time namespace, adjust the namespace-specific offsets instead.
Looking at strace date -s ...
, I see among other output:
clock_settime(CLOCK_REALTIME, {tv_sec=1619044910, tv_nsec=0}) = 0
However, reading time_namespaces(7)
:
This affects various APIs that measure against these clocks, including: clock_gettime(2), clock_nanosleep(2), nanosleep(2), timer_settime(2), timerfd_settime(2), and /proc/uptime.
I see it doesn't mention clock_settime(2)
. The wording "including" tells me this is perhaps not the complete list, but maybe it is.
I also don't understand --boottime
/--monotonic
. Looking at clock_settime(2)
, I see:
CLOCK_MONOTONIC A nonsettable system-wide clock that represents monotonic time since—as described by POSIX—"some unspecified point in the past". On Linux, that point corresponds to the number of seconds that the system has been running since it was booted.
CLOCK_BOOTTIME (since Linux 2.6.39; Linux-specific) A nonsettable system-wide clock that is identical to CLOCK_MONOTONIC, except that it also includes any time that the system is suspended.
However, when trying them, they don't seem to change the uptime
:
$ uptime -s
2021-04-10 10:30:45
$ sudo unshare -T --boottime 1000000000 uptime -s
2021-04-10 10:30:45
$ sudo unshare -T --monotonic 1000000000 uptime -s
2021-04-10 10:30:45
$ sudo unshare -T --boottime -100000 uptime -s
2021-04-10 10:30:45
$ sudo unshare -T --monotonic -100000 uptime -s
2021-04-10 10:30:45
I see from strace uptime
that it reads /proc/uptime
instead of calling clock_gettime(2)
, and /proc/uptime
doesn't seem to be affected by the unshare
calls and their offsets, despite the documentation at time_namespaces(7)
saying that it affects /proc/uptime
as I quoted above.
How is this namespace supposed to be used? I can't seem to find any command that would be affected by unshare --time
.