Thursday, February 3, 2011

How to get the timezone of a system image?

Hi,

I'd like to get the timezone of a system. Now some caveats...

  • Im using Linux, and the target systems will be UNIX.
  • I'll be using only system images, ie. not the live system, but the partitions will be mounted.
  • I'll eventually be scripting it.

I was thinking about using /etc/localtime, but sometimes this is a real file, and sometimes a symlink. In either case, I can't seem to get a description of the file format from which to parse.

Anyone have any ideas?

Thanks

  • That file is going to be in the tz file format which is a binary format. You can use DateTime::TimeZone::Tzfile to parse it if you know Perl. If it is a symlink it should not mater because if it is mounted you can just follow it.

    Lastly, if you can control the image creation I highly recommend you just set the system time to UTC.

    Willi Ballenthin : This is perfect, thanks.
  • Do you know what flavor of UNIX these are?

    In addition to /etc/localtime,

    • Redhat variants use /etc/sysconfig/clock
    • Debian variants use /etc/timezone
    Willi Ballenthin : I'd like to stay as general as possible, so if I'm looking in the wrong place, I'd welcome comments. Thanks
    From Matt
  • From the command line you can get it in a safer way than reading /etc/localtime by running:

    date +%Z
    
    Zypher : The systems won't be live, so date commands won't work
    patate : Sorry if I didn't specifically said to chroot before: chroot /path/to/system date +%Z
    From patate
  • The cruel way .... :p If mount is readonly, touch a file and afterwards ... ls -lah ....

    then you 'll have the time that the file was created. Meaning ... system's time +/- some seconds.

    Or somehow, try to modify something explicitly, or implicitly and see the time :>

  • A simple way to handle this on Linux systems is to use the file command;

    file /etc/timezone
    

    or

    file /etc/sysconfig/clock
    

    However as you may be using images of UNIX systems (I am hoping its Solaris as thats as much as I am familiar with) it may be slightly different.

    Without actually writing the script for you, here is my take on it in pseudo code

    get mount
    if mount is ext3 ... then assume linux
     if exists /etc/timezone
     then
       check if file is ascii or gz
        if ascii then cat the timezone
        else
        if exists /etc/redhat-release
        then
         assume redhat
         TZ = cat /etc/sysconfig/clock
       fi
    

    Yes its less than elegant but you shoudl get my drift. So you can make some assumputions on the os based upon the mount.

    The process would be same for say a Solaris mount, you could work out where the file is, use the file command to determine the filetype and from there either just get its contents with cat or grep ^TZ

    Hope that helps.

    AJ01 : Forgot to mention, depending on your script fu, you could run something like this either as the mounting systems root/admin user or indeed within the chroot.
    From AJ01

0 comments:

Post a Comment