Simulate an Ubuntu-like VM inside MacOS | by Yashodhan Mohan Bhatnagar | Jan, 2024
There are occasions, if you’re engaged on a distant system that has a Unix system like Ubuntu, RHEL, and so forth. You need to develop scripts, checkout if some packages exist, prototype command snippets or perhaps check out some compatibility.
Anytime I’ve had to try this I needed to spin up a t2 VM from AWS’s free tier and try it out after which shut it. At all times appeared a problem particularly since I wanted Web to simulate easy instructions. It’s not like a system like Ubuntu may be very removed from what MacOS already has. However generally there are very refined variations and wiggly bugs that completely want you to make use of the unique OS. Typically easy instructions like date have completely different flag behaviours.
Lately I had began utilizing a Docker container of Ubuntu as a substitute of the t2 VM. For any testing, I’d spin up the container, setup the dependeny packages, check out the instructions, scripts, and so forth. after which let or not it’s. Some occasions it might get killed, some days I absent mindedly cleaned up all of the containers, a couple of times I needed to take away Docker runtime itself (for unrelated causes). It will have been swell if I might deal with that Docker container like my private VM, which might preserve some quantity of state. I don’t need to set up fundamental packages like wget, curl everytime and officiating each minor change inside a Dockerfile was actually a upkeep nightmare for me. If I created a symlink contained in the OS filesystem, I didn’t need to codify it contained in the Dockerfile.
To that finish, I setup a batch of scripts that may simulate a VM, preserve state and let me deal with the container as a VM. That is the story of “Friday”.
There are two sorts of states I needed to keep up —
- System state: That is the state that will get modified after I set up packages, make system modifications, add customers, change permissions and do all form of random admin stuff.
- Quantity state: This was typically heavy obligation stuff like information, CSVs, keys, and so forth that I don’t essentially need to retailer within the container picture. The information inside it could possibly be small to massive to very massive. Taking it inside a container picture can be, inconvenient, to say the least.
To deal with the system state, I used a mixture of “docker export” and “docker import” to routinely, and on-demand, backup the state of the container.
To deal with the amount state, I merely mounted a listing from my MacOS filesystem into the container as “/root”. It has the extra side-effect that transferring information from my Mac to my “Ubuntu VM” is so simple as copying it from one listing to a different. SFTP of the best variety.
Friday is product of 4 easy scripts:
- start-friday.sh
This script checks if Friday is already working and if not, begins Friday, and takes a direct backup of it’s state. - freeze-friday.sh
This script is utilized by all different scripts and easily takes a backup of Friday’s state by working “docker export” to make a TGZ file after which a direct “docker import” to convey it again into the docker picture system. - login-into-friday.sh
This script runs the docker exec command to enter Friday. The particular magic spice is that if you exit the shell cleanly, it’ll take a direct freeze of Friday. So this makes positive that in case you’ve achieved any modifications throughout your shell session, they’re not misplaced. Sort of like how functions ask you “Do you need to save earlier than quitting?” - stop-friday.sh
This script takes a backup of Friday and stops it cleanly.
The core spice of Friday is the freeze-friday logic that ensures that every minor modification to Friday is backed up just like how AWS maintains your EBS backups.
A further safety that you would be able to put round Friday is to setup crontab to take hourly/half-hourly backups of Friday by working freeze-friday.
I’ve put up the scripts beneath which solely want you to change the “ROOT_PATH” variable to a listing the place Friday can retailer its TGZ backups and its Quantity state. To setup, solely as soon as it is advisable to pull your taste or favourite OS’s base picture from DockerHub or your registry to your native system and tag it as “friday:newest”.
The scripts can be found as a Gist here.
For example I needed my VM to be Ubuntu flavored, so:
You may also add freeze-friday to crontab like so: