Skip to content

Send mail from linux init boot phase

August 3, 2012

As promised I’m here to tell you how to send a mail from linux init boot phase.
First. I don’t know whether ‘linux init boot phase’ is the correct definition but just to be clear what I’m referring to is aka initrd, initramfs, initramfs-tools.
This won’t be neither a 10 minutes nor 1 minute initrd course tougth I’ll introduce few concepts.
Second. To send the mail I use msmtp a tool that don’t need a local mail server and that supports tls. Those’s are important because:
1) don’t wan’t (can’t?) install a local mail server in init process.
2) I’m sending via gmail that requires tls.
as we are going to use msmtp we have to install it. I’m using debian (squeeze) and that can be done
sudo apt-get install msmtp
If you need to use tls you have to install certificates. My squeeze already have but they can be installed
sudo apt-get install ca-certificates
Now that msmtp is installed you can try i (just to be sure it works before to put in the initramfs). All is needed is a configuration file .msmtprc in home directory

defaults
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

account my-account-with-gmail.com
host smtp.googlemail.com
port 587
auth on
user insert_your_mail_server_account
password insert_here_the_account_password
from boot.myhost
syslog off
logfile

Just one note: account is just to idenfy the section it is not related with user or mail address.
Now
echo -e “Subject: mail from me.\nThis is a mail from me” | /bin/msmtp –file=/root/.msmtprc –account my-account-with-gmail.com — address@e-mail.net
should send the mail if all is fine (of ocurse change adress@e-mail.net with something meaningful and edit .mstmrc changing user and password entries.
Next. Now is time to tell initramfs to use msmtp – if available of course. What we have to do is:
a) instruct initramfs-tools to add stuff we need to send the mail
b) create the init file
We start from a).
Main initramfs-tools configuration is in /usr/share/initramfs-tools/ and /etc/initramfs-tools/. When we will execute step b), initramfs-tools will go throught that configuration in order to understand what must drop into init file.
To define what must drop there we have to add file to the hooks directory: initrams-tools will execute one by one files in such a directory. So there we must put the script that add msmtp stuff.
This is the my /usr/share/initramfs-tools/hooks/msmtp.

#!/bin/sh

PREREQ="dropbear"

prereqs() {
echo "$PREREQ"
}

case "$1" in
prereqs)
prereqs
exit 0
;;
esac

. "${CONFDIR}/initramfs.conf"
. /usr/share/initramfs-tools/hook-functions

# Install msmtp if explicitly enabled, or in case of a dropbear setup if not
# explicitly disabled
if [ "${MSMTP}" = "y" ] || ( [ "${MSMTP}" != "n" ] && [ -x "${DESTDIR}/sbin/dropbear" ] ); then
if [ ! -x "/usr/bin/msmtp" ]; then
if [ "${MSMTP}" = "y" ]; then
echo "msmtp: FAILURE: msmtp not found!"
else
echo "msmtp: WARNING: msmtp not found, you won't be notified via mail when system is booting up!"
fi
else
rm -f "${DESTDIR}/bin/msmtp"
copy_exec "/usr/bin/msmtp" "/bin/"
rm -f "${DESTDIR}/root/.msmtprc"
cp /etc/initramfs-tools/root/.msmtprc "${DESTDIR}/root/"
cp /etc/resolv.conf "${DESTDIR}/etc/"
mkdir -p "${DESTDIR}/etc/ssl/certs/"
cp /etc/ssl/certs/ca-certificates.crt "${DESTDIR}/etc/ssl/certs/"
fi
fi

I copied skeleton from dropbear hook but logic is very simple: is pre-requisites are satisfied then copy needed executable and configuration files into ${DESTDIR} structure.
Eventually initramfs-tools will pack ${ÐESTDIR} into init file.
resolv.conf is needed beacuse msmtp uses Internet and so must resolv names.
.msmtprc is the configuration file msmtp wil use to send the mail(s). You can use the one we saw above.
Where’s ${MSMTP} defined? Here /user/share/initramfs-tools/conf-hooks.d/msmtp

#
# MSMTP: [ y | n ]
#
# Use msmtp if available. If not specified, msmtp will be used - if
# possible - in case of dropbear.
#

#MSMTP=y

#
# UMASK: [ 4-DIGIT OCTAL UMASK ]
#
# umask to use when creating an initramfs
#

UMASK=0077

Just an hint: if you want to add mstmp to init without having dropbear set ${MSMTP} to y.
Ok, we have hooks and related files to get working msmtp into init file. HOw we send mail(s)? init file while running execute files that are in the scripts directory. There you’ll find – among other objects – these directories: init-{bottom,premount,top} and local-{bottom,premount,top}. I don’t remember the exact in which order they are parsed. I guess init-* are excuted before local-*. top and premount are for inizialization, while bottom are for deinitialization. Example: in premount start dropbear ssh server,  in top stop the server.
Where script to send mail to be put? Well local-premount is a good candidate. Here my /usr/share/initramfs-tools/scripts/local-top/send_mail:

#!/bin/sh

PREREQ="dropbear"

prereqs() {
echo "$PREREQ"
}

case "$1" in
prereqs)
prereqs
exit 0
;;
esac

. /scripts/functions

[ -x /bin/msmtp ] || exit 0

log_begin_msg "Sending mail"

. /conf/initramfs.conf

echo -e "Subject: mail from init.\nThis is a mail from init" | /bin/msmtp --file=/root/.msmtprc --account my-account-with-gmail.com -- address@e-mail.net

Two notes:
a) remember my-account-with-gmail.com look at .msmtprc above;
b) replace address@e-mail.net with mail address to which send the mail.
How to bring network up? There a number a means.
One it to specify a network devide for DEVICE entry in /etc/initramfs-tools/initramfs.conf. The one I choose is to specify ip option to kernel bootarg.
For my default grub entry I have:
linux   /vmlinuz-2.6.32-5-486 root=/dev/mapper/lvm.system-root ip=192.168.0.2:::255.255.255.0:myhost:: ro  quiet
I got lost in grub configuration so I hardcoded that opt in /boot/grub/grub.cfg. You ou how to bring it in grub configuration tell me adding a comment: Thanks!!!
We are almost done. Now is turn of point ‘b) create the init file’:
very simple: sudo update-initramfs -u -v. Omit -v if you don’t want to see verbose output.
Reboot your system… you will get a mail.

From → Technology

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: