Varnish DAEMON_OPTS Options Errors

10,338

Solution 1

Even if Jacob will probably never read this, visitors from the future might appreciate what I'm going to write.

I believe I know what's wrong, and it looks like a Debian-specific problem, at least verified on Ubuntu 11.04 and Debian Squeeze.

I traced the execution from my /etc/default/varnish that contains the $DAEMON_OPTS to the init script. In the init script /etc/init.d/varnish, the start_varnishd() function is:

start_varnishd() {
    log_daemon_msg "Starting $DESC" "$NAME"
    output=$(/bin/tempfile -s.varnish)
    if start-stop-daemon \
        --start --quiet --pidfile ${PIDFILE} --exec ${DAEMON} -- \
        -P ${PIDFILE} ${DAEMON_OPTS} > ${output} 2>&1; then
        log_end_msg 0
    else
        log_end_msg 1
        cat $output
        exit 1
    fi
    rm $output
}

So I modified it to print the full start-stop-daemon command line, like:

 start_varnishd() {
    log_daemon_msg "Starting $DESC" "$NAME"
    output=$(/bin/tempfile -s.varnish)
+   echo "start-stop-daemon --start --quiet --pidfile ${PIDFILE} --exec ${DAEMON} -- -P ${PIDFILE} ${DAEMON_OPTS} > ${output} 2>&1"
    if start-stop-daemon \
        --start --quiet --pidfile ${PIDFILE} --exec ${DAEMON} -- \
        -P ${PIDFILE} ${DAEMON_OPTS} > ${output} 2>&1; then
        log_end_msg 0

So I got a command line echoed on STDOUT, and copied-pasted it into my shell. And, surprise! It worked. WTF?

Repeated again to be sure. Yes, it works. Mmh. Could it be another of those bash/dash corner cases? Let's try feeding the start-stop-daemon command line to bash, and see how it reacts:

start_varnishd() {
    log_daemon_msg "Starting $DESC" "$NAME"
    output=$(/bin/tempfile -s.varnish)
    if bash -c "start-stop-daemon \
        --start --quiet --pidfile ${PIDFILE} --exec ${DAEMON} -- \
        -P ${PIDFILE} ${DAEMON_OPTS} > ${output} 2>&1"; then
        log_end_msg 0
    else
        log_end_msg 1
        cat $output
        exit 1
    fi
    rm $output
}

Yes, it works just fine, at least for my case. Here's the relevant part of my /etc/default/varnish:

...
## Alternative 2, Configuration with VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request.  Use a 1GB
# fixed-size cache file.
#
DAEMON_OPTS="-a :6081 \
             -T localhost:6082 \
             -f /etc/varnish/geoip-example.vcl \
             -S /etc/varnish/secret \
             -s malloc,100M \
             -p 'cc_command=exec cc -fpic -shared -Wl,-x -L/usr/include/GeoIP.h -lGeoIP -o %o %s'"
...

I've seen posts where someone tried to work around this problem by moving the compile command into a separated shell script. Unfortunately that doesn't change the fact that start-stop-daemon is going to pass the $DAEMON_OPTS var through dash, and that will result in mangled options.

Would be something along the lines of:

-p 'cc_command=exec /etc/varnish/compile.sh %o %s'"

And then the compile.sh script as:

#!/bin/sh
cc -fpic -shared -Wl,-x -L/usr/include/GeoIP.h -lGeoIP -o $@

but it doesn't work, so just patch your init scripts, and you're good to go! Hope you can find this information useful.

Solution 2

You can try using :- DAEMON_OPTS="-a :8080 \ -T localhost:2000 \ -f /etc/varnish/varnish-default.conf \ -s file,/var/lib/varnish/varnish_storage.bin,512M \ -p cc_command='exec cc -fpic -shared -Wl,-x -L/usr/include/libmemcached/memcached.h -lmemcached -o %o %s'"

Share:
10,338
Admin
Author by

Admin

Updated on June 11, 2022

Comments

  • Admin
    Admin almost 2 years

    When using inline C with Varnish I've not been able to get /etc/varnish/default
    to be happy at start up.

    I've tested inline C with varnish for two things: GeoIP detection and Anti-Site-Scraping functions.

    The DAEMON_OPTS always complains even though I'm following what other seem
    to indicate works fine.

    My problem is that this command line start up works:

    varnishd -f /etc/varnish/varnish-default.conf -s file,/var/lib/varnish/varnish_storage.bin,512M -T 127.0.0.1:2000 -a 0.0.0.0:8080 -p 'cc_command=exec cc -fpic -shared -Wl,-x -L/usr/include/libmemcached/memcached.h -lmemcached -o %o %s'
    

    But it errors out with trying to start up from default start scripts:

    /etc/default/varnish has this in it:

    DAEMON_OPTS="-a :8080 \
                 -T localhost:2000 \
                 -f /etc/varnish/varnish-default.conf \
                 -s file,/var/lib/varnish/varnish_storage.bin,512M \
                 -p 'cc_command=exec cc -fpic -shared -Wl,-x -L/usr/include/libmemcached/memcached.h -lmemcached -o %o %s'"
    

    The error is:

    # /etc/init.d/varnish start
    Starting HTTP accelerator: varnishd failed!
    storage_file: filename: /var/lib/varnish/vbox.local/varnish_storage.bin size 512 MB.
    Error:
    Unknown parameter "'cc_command".
    

    If I try change the last line to:

    -p cc_command='exec cc -fpic -shared -Wl,-x -L/usr/include/libmemcached/memcached.h -lmemcached -o %o %s'"
    

    It's error is now:

    # /etc/init.d/varnish start
    Starting HTTP accelerator: varnishd failed!
    storage_file: filename: /var/lib/varnish/vbox.local/varnish_storage.bin size 512 MB.
    Error: Unknown storage method "hared"
    

    It's trying to interpret the '-shared' as -s hared and 'hared' is not a storage type.

    For both GeoIP and the Anti-Site-Scrape I've used the exact recommended daemon options
    plus have tried all sorts of variations like adding \' and '' but no joy.

    Here is a link to the instruction I've followed that work fine except the DAEMON_OPTS part.
    http://drcarter.info/2010/04/how-fighting-against-scraping-using-varnish-vcl-inline-c-memcached/

    I'm using Debian and the exact DAEMON_OPTS as stated in the instructions.

    Can anyone help with a pointer on what's going wrong here?

    Many thanks!

  • Cosimo
    Cosimo over 12 years
    There might be a special syntax or workaround to make this work on dash too. Unfortunately, I don't know much about dash.
  • nicomen
    nicomen over 12 years
    Take a look at this perhaps? stackoverflow.com/questions/1661193/…
  • Cosimo
    Cosimo over 12 years
    I have to say that I didn't understand what's the proposed solution there at the end of the story... :-|
  • kayue
    kayue almost 11 years
    This answer works! thank you so much! the proposed solution is to patch /etc/init.d/varnish with bash
  • user2930516
    user2930516 over 8 years
    Worked for me too. Thanks!