Should I run docker-machine stop default every time before I shutdown my Mac? Or is it ok to shutdown with the machine 'running'?
It's (mostly) ok to shutdown your system with "running" machines.
For local machines you will be relying on your VM's normal shutdown behaviour at system shutdown. For externally hosted machines, they will be left running.
With docker-machine on OSX and VirtualBox 5.x, any machines running on VirtualBox VM's will be paused and have their current state saved when the host is shutdown. The machine will be left in this state at system startup until you start them back up (via docker-machine or some VirtualBox method)
docker-machine does not attempt to do anything to your machines on a shutdown signal as it is not a system daemon. docker-machine is a cli utility you manually run to manage machines.
The "mostly" caveat is that some applications really struggle with the time dilation that occurs from pausing a VM. If you do run into issues with your os or apps you could have launchd manage the vm completely so it starts and stops automatically when you login. There is most likely a plist to make launchd run a docker-machine stop default at logoff too.
Docker will receive the shutdown signal and try to shut itself down. It however does not guarantee a graceful shutdown for all of the containers; and it might prevent your mac from shutting down in the process.
Edit
From their source code
// containerStop halts a container by sending a stop signal, waiting for the given
// duration in seconds, and then calling SIGKILL and waiting for the
// process to exit. If a negative duration is given, Stop will wait
// for the initial signal forever. If the container is not running Stop returns immediately.
I have not found any mention that containers will be paused and committed
Related
Suspending & resuming my virtual-machine does break the k8s deployment
When I suspend with minikube stop and then resume the Virtual Machine with minikube start, Minikube re-deploys my app from scratch.
I see this behaviour with newer versions of Minikube higher than v1.18 (I run on v1.19).
The setup:
The Kubernetes deployment mounts a volume with the source code from my host machine, via hostPath.\
Also I have a container of initContainers that setups the application.
Since the new "redeploy behaviour on resume" happens, the init-container breaks my deploy, if I have work-in-progress code on my host machine..
The issue:
Now, if I have temporary/non-perfectly-running code, I cannot suspend the machine with unfinished work anymore, between working days; because every time I resume it Minikube will try to deploy again but with broken code and fail with an Init:CrashLoopBackOff.
The workaround:
For now, each time I resume the machine I need to
stash/commit my WIP code
checkout the last commit with working deployment
run the deployment & wait for it to complete the initialization (minutes...)
checkout/stash-pop the code saved at point 1).
I can survive, but the workflow is terrible.
How do I restore the old behaviour?
How do I make my deploys to stay untouched, as expected when suspending the VM, instead of being re-deployed every time I resume?
In short words there are two ways to achieve what you want:
On current versions of minikube and virtualbox you can use save state option in Virtual box directly.
Move initContianer's code to a separate job.
More details about minikube + virtual box
I have an environment with minikube version 1.20, virtual box 6.1.22 (from yesterday) and MacOS. Also minikube driver is set to virtualbox.
First with minikube + VirtualBox. Different scenarios:
minikube stop does following:
Stops a local Kubernetes cluster. This command stops the underlying VM
or container, but keeps user data intact.
What happens is virtual machine where minikube is set up stops entirely. minikube start starts the VM and all processes in it. All containers are started as well, so if your pod has an init-container, it will run first anyway.
minikube pause pauses all processes and free up CPU resourses while memory will still be allocated. minikube unpause brings back CPU resources and continues executing containers from a state when they were paused.
Based on different scenarios I tried with minikube it's not achievable using only minikube commands. To avoid any state loss on your minikube environment due to host restart or necessity to stop a VM to get more resources, you can use save state feature in VirtualBox in UI or cli. Below what it does:
VBoxManage controlvm savestate: Saves the current state of the VM to disk and then stops the VM.
Virtual box creates something like a snapshot with all memory content within this snapshot. When virtual machine is restarted, Virtual box will restore the state of VM to the state when the VM was saved.
One more assumption is if this works the same way in v. 1.20 - this is expected behaviour and not a bug (otherwise it would've been fixed already)
Init-container and jobs
You may consider moving your init-container's code to a a separate job so you will avoid any issues with unintended pod restarts and braking your deployment in the main container. Also it's advised to have init-container's code idempotent.
Here's a quote from official documentation:
Because init containers can be restarted, retried, or re-executed,
init container code should be idempotent. In particular, code that
writes to files on EmptyDirs should be prepared for the possibility
that an output file already exists.
This can be achieved by using jobs in Kubernetes which you can run manually when you need to do so.
To ensure following the workflow you can place a check for a Job completion or a specific file on a data volume to the deployment's pod init container to indicate that code is working, deployment will be fine.
Links with more information:
VirtualBox save state
initContainers
kubernetes jobs
I am new to Mac OSX and i am trying to develop a daemon (i am familiar with windows services, and trying to achieve about the same things).
What i did so far:
Managed to write a plist file in LaunchDaemons (i found out that those daemons run under system account, not user, just like a windows service with local system permissions).
I managed to make it run on startup, and run my simple hello world application that just writes something to a file every 10 seconds.
The problem is, when i unload the daemon with launchd, it just kills my process.
Is it any way to have the daemon either run something else on unload, or communicate to my process that it is shutting down, so i can perform an unload action in my code?
Thank you
The lifecycle of a daemon is documented by Apple and they describe how to handle Terminating Processes
If you're using Objective-c or Swift, you should be able to implement the applicationshouldterminate delegate method.
If you're not using Cocoa:
Non-Cocoa applications receive a “Quit Application” Apple event (kAEQuitApplication), as a courtesy, to give them a chance to shut down gracefully
Finally, since you're running a daemon (background process)
For background processes, the procedure is a little different. The loginwindow program notifies the process that it is about to be terminated by sending it a Quit Application Apple event (kAEQuitApplication). Unlike foreground processes, however, loginwindow does not wait for a reply. It proceeds to terminate any open background processes by sending a SIGKILL signal, regardless of any returned errors.
If the system is being shut down or restarted, it sends a SIGTERM signal to all daemons, followed a few seconds later by SIGKILL signal.
There's a discussion on handling SIGTERM here.
When I'm about to shutdown my host machine off should I:
vagrant halt
OR
vagrant suspend
What's the difference?
tl;dr
Use vagrant halt when you want to power off your machine, use vagrant suspend when you want to hibernate your machine.
From the Vagrant docs:
vagrant suspend
A suspend effectively saves the exact point-in-time state of the
machine, so that when you resume it later, it begins running
immediately from that point, rather than doing a full boot.
This generally requires extra disk space to store all the contents of
the RAM within your guest machine, but the machine no longer consumes
the RAM of your host machine or CPU cycles while it is suspended.
vagrant halt
This command shuts down the running machine Vagrant is managing.
Which one you want to use?
It is basically up to you - do you have on-going work on the VM (maybe multiple applications opened through GUI, etc) you would prefer to suspend the VM so when you power up the machine, everything is there (Vagrant/VirtualBox would need to store the state of the instance on your hard drive consuming some hard drive space from your host). If you want to start from a clean start as all your process are setup from init, then go for vagrant halt
Example:
If you don't work much on your VM, meaning that all your project files are stored on your host and shared with your VM to see how they reflect using a
LAMP server, then you can safely go with vagrant halt
If when you start your instance, you need to manually start specific processes, or you work on files directly in the VM; then it's better to suspend it so that when you turn it back on, it'll save your session and retrieve the instance in the same state it was in before you suspended it.
I have an OS which doesn't shut down properly when I run it in one particular hypervisor (KVM) even though it works on all other hypervisors. Instead, what it does is sync all data to disk and then hang indefinitely with the message "Hit any button to reboot" until you issue a hard shutdown from the hypervisor.
I'm trying to automate a no-touch installation of this OS from an .iso file into a .box file using Packer. However, the Packer run fails every time because it hits the shutdown_timeout (from the QEMU builder) while the OS is hung waiting for input. I'm looking for a workaround -- it seems like either of the following could work (and maybe there are other ways), but I can't figure out any way to do them! Some ideas I've searched for were:
tell Packer to do a hard shutdown after a certain amount of time
tell Packer that hitting this timeout isn't an error and it should just do a hard shutdown and continue with the provisioner steps
Upon rereading the docs, I found the answer:
shutdown_command (string) - The command to use to gracefully shut down the machine once all the provisioning is done. By default this is an empty string, which tells Packer to just forcefully shut down the machine.
D'oh!
Yesterday I ran a BIOS update on a Windows7 computer. The BIOS update tool performed a shutdown of Windows and without restarting the computer (so directly after Windows shutdown), a DOS-like tool started that updated the BIOS.
I searched the WWW but all I can find is how to execute applications at Windows shutdown (so before windows goes down) or at logoff (logoff scripts).
Does anyone have any clue on how to achieve this?
It is my guess that the machine had in fact performed a soft-reboot and was running BIOS code. That's the usual approach, and in some cases the soft-reboot may not be visible to the user, i.e., the BIOS self-update code may run before the POST.
However, it is (or at least used to be) possible to run Windows code post-shutdown, although obviously it is limited in what it can do. The key information can be found in the MSDN library entry on the Service Control Handler Function:
After this time expires, system shutdown proceeds regardless of whether service shutdown is complete. Note that if the system is left in the shutdown state (not restarted or powered down), the service continues to run.
So, at least in principle, all you need to do is to install a service that asks for shutdown notification but does not actually stop when shutdown occurs, and then instruct Windows to shut down without rebooting or powering off. I have actually seen this happen, but only on older versions of Windows (before automatically powering off at shutdown became the default!) so I can't guarantee that it still works, although it definitely did once.
It should, however, be noted that this will not necessarily give you the sort of environment you may be thinking of. For example, device drivers are not necessarily unloaded before shutdown; after all, what would be the point?
You can run Local Group policy - gpedit.msc (From run).
Under Computer Configuration choose Windows Settings
choose Scripts(Startup/Shutdown)
On Shutdown properties you can add script and also mention parameters if needed.
I also advised you to look at the following question - how to delay shutdown and run a process in window service