ailispaw/docker-root

Make a permanent folder

Closed this issue ยท 40 comments

Hi,

How make a permanent folder ?

I create an folder /var/lib/rancher with files in VM, after vagrant halt and vagrant up, folder disappeared.

Thank you

docker-root.box has 40GB HDD and mounts it at /mnt/sda1 (/dev/sda1).
https://github.com/ailispaw/docker-root-packer#dockerroot-packer-for-virtualbox-and-qemu

You can make any folder under the /mnt/sda1 as a persistent one.

Yes, I already fix my problem, not had time to update issue ๐Ÿ˜‰

Thank you !

Maybe add information about /mnt/sda1 in Readme.md ?

I use :

node.vm.provision "shell",
    inline: "rm -f /var/lib/docker && rm -f /var/lib/docker-root && mv /var/lib/* /mnt/sda1/var/lib/ && ln -s /mnt/sda1/var/lib /var/lib"

Because, I can't create symbolic link /var/lib/rancher on provisioning. Docker container want to create it

Provision is execute only first up. how can I make persistent the symbolic link ?

Possible add /var overall persistent on docker-root please ?

Would be more clean and logic ๐Ÿ˜‰

/var/lib/docker && /var/lib/docker-root are reserved by DockerRoot.
Please use as below instead.

  node.vm.provision "shell",
    inline: "mkdir -p /mnt/sda1/var/lib/rancher && \
      rm -f /var/lib/rancher && ln -s /mnt/sda1/var/lib/rancher /var/lib/rancher"

Impossible sorry, rancher-agent want create /var/lib/rancher. Is /var/lib/rancher exist, failed start ๐Ÿ˜Ÿ

You can put any scripting into /var/lib/docker-root/start.sh.
https://github.com/ailispaw/docker-root/blob/master/docs/customization.md#customizing-init-scripts-on-booting-up

Or you can use `run: "always" option for a Vagrant provisioner.

You see rm -f /var/lib/rancher in my script.

Oh, rancher-agent dose!

I think rancher-agent should work in that situation.

Ok for script to make persistent symbolic link with init.d, I'll try, thank you !

But no solution to first start VM. rancher-agent want create /var/lib/rancher on first run, so I can't create symbolic link before run ๐Ÿ˜ข

You may want to try vagrant-triggers to run it after up.

  if Vagrant.has_plugin?("vagrant-triggers") then
    config.trigger.after [:up, :resume] do
      info "Adjusting datetime after suspend and resume."
      run_remote "sudo sntp -4sSc pool.ntp.org; date"
    end
  end

It was wrong.

I just got an idea.
You can make an init script in the SysV manner to backup/restore the data in /var/lib/rancher instead of symlink.

Anyway, rancher-agent should work in such a situation.

Oh great idea ! I try ๐Ÿ˜ƒ

Wait a minute.
-v /var/lib/rancher:/var/lib/rancher rancher/agent:v0.9.2 means that /var/lib/rancher has already existed at run the container. ??? I don't understand that failure.

Ok works !

But, if the VM crash, no backup ๐Ÿ˜Ÿ

Yes, this command to add custom host :

sudo docker run -d --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v0.10.0 https://infra.cedvan.com/v1/scripts/4A21993727DD77639CA1:1458604800000:hBFQhNHEkdXRKy8ZKtg3ItAw2gk

means that /var/lib/rancher already exist with symbolic link create before first run

Ok great !

My solution :

lib-persistent-folder.sh

#!/bin/sh
#
# Configure lib persistent folder....
#


case "$1" in
  install)
        echo "Install lib persistent folder..."
        rm -f /var/lib/docker
        rm -f /var/lib/docker-root
        cp -rf /var/lib/* /mnt/sda1/var/lib/
        rm -rf /var/lib
        ln -s /mnt/sda1/var/lib /var/lib
        ;;
  start)
    echo "Mount lib persistent folder..."
        rm -rf /var/lib
    ln -s /mnt/sda1/var/lib /var/lib
    ;;
  stop)
    printf "Unmount lib persitent folder..."
    rm -f /var/lib
    ;;
  restart|reload)
    "$0" stop
    "$0" start
    ;;
  *)
    echo "Usage: $0 {install|start|stop|restart}"
    exit 1
esac

exit $?

Vagrantfile

...

order = 60
script_name_source = "lib-persistent-folder.sh"
script_name_destination = "S#{order}lib-persistent-folder"
node.vm.provision "file", source: "./#{script_name_source}", destination: "/tmp/#{script_name_destination}"
node.vm.provision "shell",
inline: "mv /tmp/#{script_name_destination} /etc/init.d/#{script_name_destination} && chmod +x /etc/init.d/#{script_name_destination} && /etc/init.d/#{script_name_destination} install"

...

With this solution data saved on crash :)

Why do you not make persistent the folder /var completely ?

It is not a good idea to remove /var/lib/docker on provision while running Docker daemon.

I just follow the boot2docker way about /var/lib/docker.

Ok, it is better to stop docker daemon, delete, create the symbolic link /var/lib and restart the docker daemon ?

  install)
        echo "Install lib persistent folder..."
        /etc/init.d/docker stop
        rm -f /var/lib/docker
        rm -f /var/lib/docker-root
        cp -rf /var/lib/* /mnt/sda1/var/lib/
        rm -rf /var/lib
        ln -s /mnt/sda1/var/lib /var/lib
        /etc/init.d/docker start
        ;;

Those script should be in start for every boot.
DockerRoot /init will create symlinks to /var/lib/docker and /var/lib/docker-root again on boot.

BTW,
Why don't you open an issue for this at rancher/agent repo?

In start, you don't need to stop/start Docker daemon, because Docker daemon stops in the init.d process.

Issue to rancher/rancher is open rancher/rancher#4073 ๐Ÿ˜‰

Step install is executed on provisioning, so docker daemon is running. Stop/start docker is necessary no ?
Steps start and stop executed by init.d, so not need stop/start docker

Step install is executed on provisioning, so docker daemon is running.
Stop/start docker is necessary.

Yes

Steps start and stop executed by init.d, so not need stop/start docker

No, you don't need.

Ok, so my solution is good ?
(Just to wait that rancher/rancher solve problem)

Let me see the final version of your script. Probably fine.

lib-persistent-folder.sh

#!/bin/sh
#
# Configure lib persistent folder....
#

case "$1" in
  install)
        echo "Install lib persistent folder..."
        /etc/init.d/docker stop
        rm -f /var/lib/docker
        rm -f /var/lib/docker-root
        cp -rf /var/lib/* /mnt/sda1/var/lib/
        rm -rf /var/lib
        ln -s /mnt/sda1/var/lib /var/lib
        /etc/init.d/docker start
        ;;
  start)
    echo "Mount lib persistent folder..."
        rm -rf /var/lib
    ln -s /mnt/sda1/var/lib /var/lib
    ;;
  stop)
    printf "Unmount lib persitent folder..."
    rm -f /var/lib
    ;;
  restart|reload)
    "$0" stop
    "$0" start
    ;;
  *)
    echo "Usage: $0 {install|start|stop|restart}"
    exit 1
esac

exit $?

Vagrantfile

...

order = 60
script_name_source = "lib-persistent-folder.sh"
script_name_destination = "S#{order}lib-persistent-folder"
node.vm.provision "file", source: "./#{script_name_source}", destination: "/tmp/#{script_name_destination}"
node.vm.provision "shell",
    inline: "mv /tmp/#{script_name_destination} /etc/init.d/#{script_name_destination} && chmod +x /etc/init.d/#{script_name_destination} && /etc/init.d/#{script_name_destination} install"

...

I guess,

        rm -f /var/lib/docker
        rm -f /var/lib/docker-root
        cp -rf /var/lib/* /mnt/sda1/var/lib/
        rm -rf /var/lib
        ln -s /mnt/sda1/var/lib /var/lib

should be in start for every boot.

Did you try -v /mnt/sda1/var/lib/rancher:/var/lib/rancher ?

should be in start for every boot.

Why ?

Did you try -v /mnt/sda1/var/lib/rancher:/var/lib/rancher ?

Lol, no I don't try, I wanted to keep rancher default config. But I'll try ๐Ÿ˜„

Works ๐Ÿ˜ !

should be in start for every boot.
Why ?

DockerRoot /init script creates symlinks /var/lib/docker and /var/lib/docker-root before init.d scripts on every boot.

Ok, I understand

Why do you not make persistent the folder /var completely ?

Other system processes like syslog and programs use under /var, so it's not good to move completely even on boot.