Automatic Time Synchronisation for D-Link ShareCenter DNS-320

If the current date/time for the D-Link ShareCenter DNS-320 gets reset everytime the the NAS shuts down, a script can be used to perform date/time synchronisation on boot-up. (This requires Fonz fun_plug to be installed.)

Create the file /ffp/start/timesync.sh with the following content.

#!/ffp/bin/sh

# PROVIDE: timesync
# REQUIRE: inetd

. /ffp/etc/ffp.subr

name="timesync"
start_cmd="timesync_start"

timesync_start()
{
    date +%s -s @`wget -O - -q http://www.icanhaztimestamp.com/`;
}

run_rc_command "$1"

Explanations

The key to the script lies in the following line.

date +%s -s @`wget -O - -q http://www.icanhaztimestamp.com/`;

It can be broken down as follows.

www.icanhaztimestamp.com

This is a web service that returns the current UNIX timestamp in plain text. (I.e. the HTTP response body contains nothing but the ten-digit current UNIX timestamp.)

wget -O - -q http://www.icanhaztimestamp.com/

This wget command makes a request to the specified URL and outputs the HTTP response body (in this case, the ten-digit current UNIX timestamp).

date +%s -s @`wget -O - -q http://www.icanhaztimestamp.com/`

This date command sets the current date/time to the specified UNIX timestamp which, in this case, is the response from www.icanhaztimestamp.com.

This script would run once when the NAS boots up, and hence, the date/time would be synchronised.

Note: wget had to be used to obtain the current date/time because ntpd did not appear to be present.

Note: Though the DNS-320 administrator console allowed an NTP server to be configured, the NAS did not appear to synchronise the time with the server. (Perhaps because ntpd was not present.)

References:

[1] https://nas-tweaks.net/tutorials/
[2] http://www.icanhaztimestamp.com/

Adding Album Cover to a FLAC File

When converting a wave file to a FLAC file using the flac command, an album cover can also be added by using the –picture argument with an input image file.

$ flac -0 ... --picture="3|image/jpeg|||cover.jpg" ... filename.wav

where 3 means that the picture is a front cover, cover.jpg is the cover image file, and filename.wav is the name of the input wave file.

Alternatively, metaflac can be used to add an album cover to a FLAC file.

$ metaflac --import-picture-from="3|image/jpeg|||cover.jpg" filename.flac

where the format of the –import-picture-from argument follows the –picture argument of the flac command, and filename.flac is the name of the FLAC file to be processed.

Note: flac and metaflac may not be present in a default installation. It can be installed through the package manager.

References

[1] https://sharpbang.wordpress.com/2013/08/07/converting-a-wave-file-to-a-flac-file/
[2] http://linux.die.net/man/1/flac
[3] http://linux.die.net/man/1/metaflac
[4] https://www.xiph.org/vorbis/doc/v-comment.html

Configuring Dovecot and Postfix for Local Webmail Testing

Steps for configuring Dovecot and Postfix for locally testing a webmail deployment (e.g. Roundcube Webmail).

Configure Hostname

1. Change the machine hostname (e.g. mail.testmail.com).

# hostname mail.testmail.com

2. Add or change the following line in the /etc/hosts file to point the configured hostname (e.g. mail.testmail.com) to the loopback address.

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 mail.testmail.com

Add Email User

3. Add and configure a user account for Dovecot IMAP access.

# useradd -m mailuser
# passwd mailuser # and enter a password for the new user
# mkdir /home/mailuser/Maildir
# chown mailuser:mailuser /home/mailuser/Maildir

Install Packages

4. Install the dovecot and postfix packages.

# yum install dovecot postfix

Configure Dovecot

5. Add the following lines in the /etc/pam.d/dovecot file for the Dovecot PAM configuration.

auth       required     pam_unix.so
account    required     pam_unix.so

6. Add or uncomment the following lines in the Dovecot configuration file /etc/dovecot/dovecot.conf.

protocols = imap pop3 lmtp
listen = *, ::
base_dir = /var/run/dovecot/

7. Uncomment and change the following line in the /etc/dovecot/conf.d/10-auth.conf file to allow plaintext authentication for testing purposes.

disable_plaintext_auth = no

8. Uncomment and change the following line in the /etc/dovecot/conf.d/10-mail.conf file to set the email storage location.

mail_location = maildir:~/Maildir

9. Uncomment the following lines in the /etc/dovecot/conf.d/10-master.conf file to configure the IMAP listening port.

service imap-login {
  inet_listener imap {
    port = 143
  }
  ...
}

10. Uncomment and change the following line in the /etc/dovecot/conf.d/15-lda.conf file to set the postmaster email address.

postmaster_address = postmaster@mail.testmail.com

11. Uncomment the following lines in the /etc/dovecot/conf.d/auth-system.conf.ext file to configure Dovecot to use PAM for authentication.

passdb {
  driver = pam
  ...
  args = dovecot
}

Configure Postfix

12. Uncomment the following line in the /etc/postfix/main.cf file to configure Postfix to use the previously set hostname.

inet_interfaces = $myhostname, localhost

13. Comment out the following line in the /etc/postfix/main.cf file.

#inet_interfaces = localhost

14. Add the following line in the /etc/postfix/main.cf file to configure Postfix to use Dovecot LDA for mail delivery.

mailbox_command = /usr/libexec/dovecot/dovecot-lda -f "$SENDER" -a "$RECIPIENT"

Start Dovecot and Postfix

15. Start Dovecot.

# /etc/init.d/dovecot start

16. Start Postfix.

# /etc/init.d/postfix start

Install Webmail

17. Install the webmail package (e.g. Roundcube Webmail).

Troubleshooting

18. For troubleshooting, error messages will be in the /var/log/maillog file.

References

[1] http://wiki2.dovecot.org/
[2] http://wiki2.dovecot.org/PasswordDatabase/PAM
[3] http://wiki2.dovecot.org/LDA/Postfix

Converting a Wave File to a FLAC File

A simple Linux command to convert a wave file to a FLAC file:

$ flac -0 --tag=ALBUM="Some Album" --tag=ARTIST="Some Artist" --tag=GENRE="Rock" --tag=TITLE="Song Title" --tag=TRACKNUMBER="3" filename.wav

where filename.wav is the name of the input wave file. The output FLAC file will have the name filename.flac.

Explanation of the command arguments:

-0

Compression level (in this case, 0). Valid compression levels are from 0 to 8.

--tag=ALBUM="Some Album"

Add the ALBUM tag to the FLAC file (in this case, with the value Some Album)

--tag=ARTIST="Some Artist"

Add the ARTIST tag to the FLAC file (in this case, with the value Some Artist)

--tag=GENRE="Rock"

Add the GENRE tag to the FLAC file (in this case, with the value Rock)

--tag=TITLE="Song Title"

Add the TITLE tag to the FLAC file (in this case, with the value Song Title)

--tag=TRACKNUMBER="3"

Add the TRACKNUMBER tag to the FLAC file (in this case, with the value 3)

Note: Valid tags that can be added are TITLE, VERSION, ALBUM, TRACKNUMBER, ARTIST, PERFORMER, COPYRIGHT, LICENSE, ORGANIZATION, DESCRIPTION, GENRE, DATE, LOCATION, CONTACT and ISRC.

Note: flac may not be present in a default installation. It can be installed through the package manager.

References

[1] http://linux.die.net/man/1/flac
[2] https://www.xiph.org/vorbis/doc/v-comment.html

Fixing Issue With Missing Kernel Headers When Installing VMware Tools in Linux Virtual Machine

When installing VMware Tools in more recent Linux releases, the following error would occur even when the kernel headers were already installed.

Searching for a valid kernel header path...
The path "" is not a valid path to the 3.8.0-19-generic kernel headers.
Would you like to change it? [yes] yes

Enter the path to the kernel header files for the 3.8.0-19-generic kernel? /lib/modules/3.8.0-19-generic/build/include

The path "/lib/modules/3.8.0-19-generic/build/include" is not a valid path to
the 3.8.0-19-generic kernel headers.
Would you like to change it? [yes]

The installer would not find the header files even when the correct path was manually provided.

The reason is that, in version 3.7 of the Linux kernel, the version.h file needed by the VMware Tools installer have been relocated from /usr/src/linux-headers-$(uname -r)/include/linux to /usr/src/linux-headers-$(uname -r)/include/generated/uapi/linux, and the installer has not been updated to handle that.

The solution would be to create a symbolic link to version.h at the location where VMware Tools installer expects it.

$ ln -s /usr/src/linux-headers-$(uname -r)/include/generated/uapi/linux/version.h /usr/src/linux-headers-$(uname -r)/include/linux/version.h

References

[1] https://cmanios.wordpress.com/2013/06/12/fix-vmware-tools-kernel-header-path-is-not-valid-error/
[2] http://askubuntu.com/questions/131351/how-to-install-vmware-tools/286003#286003
[3] https://lkml.org/lkml/2012/7/20/419

Adding an Auto Shutdown Feature to the End of a Script

Whole Script

# Main Script Function ...

# Shutdown Option
export DISPLAY=:0.0;
if [ `/usr/bin/xprintidle` -gt 60000 ]; then
    /usr/bin/zenity --question --title="Job Completed" --timeout=60 \
                    --text="Job Completed; Proceed to Shutdown?" \
                    --ok-label="Yes" --cancel-label="No";
    if [ "$?" -eq "5" ]; then
        # Proceed to Shutdown
        /sbin/shutdown -h now;
    fi
fi

Explanations

export DISPLAY=:0.0;

This is necessary if the script is run outside of an X session (e.g. as a cron job), so that xprintidle can get the correct X session idle time, and zenity can show the dialog on the correct X server display.

if [ `/usr/bin/xprintidle` -gt 60000 ]; then

Gets the X session idle time, and presents the shutdown option if the X session has been inactive for some time (in this case, 60000 milliseconds). Otherwise, it is assumed that the user is active and does not want the system to shutdown.

/usr/bin/zenity --question --title="Job Completed" --timeout=60 \
                --text="Job Completed; Proceed to Shutdown?" \
                --ok-label="Yes" --cancel-label="No";

Displays the shutdown option dialog with the specified title, text, and options. If none of the options is selected within the timeout period (in this case, 60 seconds), the dialog closes with the “Yes” option selected by default.

if [ "$?" -eq "5" ]; then

Shuts down the system if the “Yes” option was selected, or if no option was selected within the timeout period (hence defaulting to the “Yes” option). Otherwise (i.e. the “No” option was selected within the timeout period), the system does not shut down.

/sbin/shutdown -h now;

Shuts down the system.

Note: xprintidle and zenity may not be present in a default installation. They can be installed through the package manager.

References

[1] https://www.linuxquestions.org/questions/linux-newbie-8/export-display%3D-0-0-a-682926/
[2] http://freecode.com/projects/xprintidle
[3] https://help.gnome.org/users/zenity/stable/