<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Scott C. Livingston</title>
    <description>The latest articles on DEV Community by Scott C. Livingston (@slivingston).</description>
    <link>https://dev.to/slivingston</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F463688%2F3f56df45-f100-4309-89be-d5d4cb0f63ba.png</url>
      <title>DEV Community: Scott C. Livingston</title>
      <link>https://dev.to/slivingston</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/slivingston"/>
    <language>en</language>
    <item>
      <title>Minimal tips to run isolated code</title>
      <dc:creator>Scott C. Livingston</dc:creator>
      <pubDate>Wed, 04 Sep 2024 21:49:23 +0000</pubDate>
      <link>https://dev.to/slivingston/minimal-tips-to-run-isolated-code-4206</link>
      <guid>https://dev.to/slivingston/minimal-tips-to-run-isolated-code-4206</guid>
      <description>&lt;p&gt;Two frequent motivations for running code in isolation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;you want to try it out before installing it,&lt;/li&gt;
&lt;li&gt;you are writing instructions about installation and need a blank starting point.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The amount of isolation required depends on aspects like trust, host hardware, etc. For most code run by most developers, we can assume the software is not expertly malicious but may have bugs that could break other parts of the local system configuration.&lt;/p&gt;

&lt;p&gt;There are several choices for running code in partial or full isolation. Some languages include lightweight environments that do not interfere with each other, e.g., &lt;a href="https://docs.python.org/3/library/venv.html" rel="noopener noreferrer"&gt;virtual environments in Python&lt;/a&gt;. However, due to caching and links, these are not sufficiently isolated for us. At the other end of the spectrum, we can run code in a node of a cloud computing service. However, the overhead and cost make this not worthwhile given our needs: isolation, but not very strong security requirements. Alternatively, we can run a virtual machine or emulator such as &lt;a href="https://www.qemu.org/" rel="noopener noreferrer"&gt;QEMU&lt;/a&gt;, &lt;a href="https://www.virtualbox.org/" rel="noopener noreferrer"&gt;VirtualBox&lt;/a&gt;, or others. This also has too much overhead given our needs.&lt;/p&gt;

&lt;p&gt;Thus motivated, install &lt;a href="https://podman-desktop.io/" rel="noopener noreferrer"&gt;Podman Desktop&lt;/a&gt;, a Docker-compatible Linux containers tool with &lt;a href="https://podman.io/" rel="noopener noreferrer"&gt;Podman&lt;/a&gt;. After Podman Desktop is installed and running, open a terminal and&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;podman run -it --name demo ubuntu:22.04 bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;which will download an &lt;a href="https://www.releases.ubuntu.com/jammy/" rel="noopener noreferrer"&gt;Ubuntu 22.04&lt;/a&gt; base image and start the &lt;a href="https://www.gnu.org/software/bash/" rel="noopener noreferrer"&gt;Bash&lt;/a&gt; shell. If you exit the shell, then it can be opened again by&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;podman start -ai demo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To start another shell inside the same container, open another terminal and&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;podman exec -it demo bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now that you have an isolated environment, let's step through the process of building and running some code inside it. We will do so for &lt;a href="https://spinroot.com/spin/whatispin.html" rel="noopener noreferrer"&gt;Spin&lt;/a&gt;, a software verification tool.&lt;/p&gt;

&lt;p&gt;First, install Git and a C compiler:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apt update
apt install git build-essential
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then, clone the repository and try to build it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/nimble-code/Spin.git
cd Spin
make
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This may end with an error message: &lt;code&gt;make[1]: yacc: No such file or directory&lt;/code&gt;. Yacc is a POSIX standard parser generator. Let's install the compatible tool &lt;a href="https://www.gnu.org/software/bison/" rel="noopener noreferrer"&gt;Bison&lt;/a&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apt install bison
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then, try to build Spin again:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;make
make install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;which will result in &lt;code&gt;spin&lt;/code&gt; being added to the system path. Let's run an example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd Examples
spin -run sort.pml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The output should show that no errors were found. The file sort.pml models a sorting algorithm in &lt;a href="https://spinroot.com/spin/Man/promela.html" rel="noopener noreferrer"&gt;Promela&lt;/a&gt;. To confirm that &lt;code&gt;spin&lt;/code&gt; is working, we next introduce an error into the model. Copy the example to outside of the container by entering the following in another terminal:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;podman cp demo:/Spin/Examples/sort.pml .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Open sort.pml in an editor and, on line 45, change &lt;code&gt;nextval &amp;lt;  myval&lt;/code&gt; to &lt;code&gt;nextval &amp;gt; myval&lt;/code&gt;. Then, copy the file back into the container:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;podman cp sort.pml demo:/Spin/Examples/sort.pml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the terminal with the container, run Spin on the new sort.pml:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spin -run sort.pml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;which should show &lt;code&gt;errors: 1&lt;/code&gt;, confirming that we introduced a bug.&lt;/p&gt;

&lt;p&gt;When you are done with the container, remove it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;podman rm -f demo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>productivity</category>
      <category>tutorial</category>
      <category>testing</category>
      <category>containers</category>
    </item>
  </channel>
</rss>
