Jekyll2022-12-16T21:11:33+00:00www.codeapex.com/feed.xmlCode ApexDeleting Duplicate Rows In SQL2021-07-26T17:10:47+00:002021-07-26T17:10:47+00:00www.codeapex.com/deleting-duplicate-rows-in-sql<h2 id="situation">Situation:</h2>
<p>There is a table in our SQL database which contains rows which we consider duplicate based upon the values in some or all of the columns.</p>
<p>We wish to delete these duplicate rows.</p>
<h2 id="objective">Objective:</h2>
<p>To demonstrate and explain a short memorable method of deleting duplicate rows in a SQL database table using a common table expression and a window function.</p>
<p>Specifically if columns <code class="language-plaintext highlighter-rouge">[col1],[col2],[col5]</code> define uniqueness for some table, say <code class="language-plaintext highlighter-rouge">dbo.Table</code>, the following query will delete duplicates:</p>
<p><code class="language-plaintext highlighter-rouge">;WITH CTE AS(<br></br>SELECT ROW_NUMBER()OVER(PARTITION BY col1, col2, col5 ORDER BY col1, col2, col5) AS RN<br></br>FROM dbo.Table<br></br>)<br></br>DELETE FROM CTE WHERE RN > 1</code></p>
<h2 id="plan">Plan:</h2>
<p>First we define which columns being the same between rows implies the rows are duplicate or in other words what columns taken together we want unique across the table.</p>
<p>Next we partition and enumerate each set of unique rows using a common table expression (CTE) and the window function <code class="language-plaintext highlighter-rouge">ROW_NUMBER()OVER()</code>.</p>
<p>What this means is we are partitioning the rows into sets of the same row and for each partition assigning a 1 to the first row in the partition, 2 to the next row in the partition, 3 in the next row in the partition, and so on; call this number RN.</p>
<p>Then we delete all such rows where the row number is greater than 1 (RN > 1).</p>
<h2 id="execution">Execution:</h2>
<p><strong>Example Table:</strong> <code class="language-plaintext highlighter-rouge">dbo.Table</code></p>
<table>
<thead>
<tr>
<th> </th>
<th>col1</th>
<th>col2</th>
<th>col3</th>
<th>col4</th>
<th>col5</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Alpha</td>
<td>1</td>
<td>1</td>
<td>CO</td>
<td>42</td>
</tr>
<tr>
<td>2</td>
<td>Alpha</td>
<td>1</td>
<td>1</td>
<td>CO</td>
<td>42</td>
</tr>
<tr>
<td>3</td>
<td>Beta</td>
<td>2</td>
<td>2</td>
<td>MN</td>
<td>29</td>
</tr>
<tr>
<td>4</td>
<td>Beta</td>
<td>2</td>
<td>2</td>
<td>MN</td>
<td>29</td>
</tr>
<tr>
<td>5</td>
<td>Beta</td>
<td>2</td>
<td>2</td>
<td>MN</td>
<td>37</td>
</tr>
</tbody>
</table>
<p><strong>Define uniqueness / duplicate:</strong></p>
<p>Let’s say a certain set of columns from the table taken together should be unique across the table; say columns <code class="language-plaintext highlighter-rouge">[col1],[col2],[col5]</code>.</p>
<p><strong>Enumeration / CTE part of query:</strong></p>
<p>We create a CTE for our table that partitions the rows into sets of the same row and enumerates each partition.</p>
<p>The result of the CTE should look like this:</p>
<table>
<thead>
<tr>
<th> </th>
<th>col1</th>
<th>col2</th>
<th>col3</th>
<th>col4</th>
<th>col5</th>
<th>RN</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Alpha</td>
<td>1</td>
<td>1</td>
<td>CO</td>
<td>42</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>Alpha</td>
<td>1</td>
<td>1</td>
<td>CO</td>
<td>42</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>Beta</td>
<td>2</td>
<td>2</td>
<td>MN</td>
<td>29</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>Beta</td>
<td>2</td>
<td>2</td>
<td>MN</td>
<td>29</td>
<td>2</td>
</tr>
<tr>
<td>5</td>
<td>Beta</td>
<td>2</td>
<td>2</td>
<td>MN</td>
<td>37</td>
<td>1</td>
</tr>
</tbody>
</table>
<p>A CTE that accomplishes this is:</p>
<p><code class="language-plaintext highlighter-rouge">;WITH CTE AS(<br></br>SELECT col1, col2, col5,<br></br>ROW_NUMBER()OVER(PARTITION BY col1, col2, col5 ORDER BY col1, col2, col5) AS RN<br></br>FROM dbo.Table<br></br>)</code></p>
<p>The key part is we PARTITION BY the columns that define uniqueness.</p>
<p><strong>Deletion part of query:</strong></p>
<p>Using our CTE we delete the rows from the table where RN > 1 as such rows are duplicate.</p>
<p><code class="language-plaintext highlighter-rouge">DELETE FROM CTE WHERE RN > 1</code></p>
<p><strong>Result:</strong></p>
<p>After deletion our example table would look like this:</p>
<table>
<thead>
<tr>
<th> </th>
<th>col1</th>
<th>col2</th>
<th>col3</th>
<th>col4</th>
<th>col5</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Alpha</td>
<td>1</td>
<td>1</td>
<td>CO</td>
<td>42</td>
</tr>
<tr>
<td>2</td>
<td>Beta</td>
<td>2</td>
<td>2</td>
<td>MN</td>
<td>29</td>
</tr>
<tr>
<td>3</td>
<td>Beta</td>
<td>2</td>
<td>2</td>
<td>MN</td>
<td>37</td>
</tr>
</tbody>
</table>
<h2 id="conclusion">Conclusion:</h2>
<p>For the CTE some of the columns in the SELECT portion are optional and were included for illustrative purposes only.</p>
<p>Specifically only the RN column is needed since the DELETE statement only works with RN.</p>
<p>Consequently the others can be dropped from the query without altering the end result giving us our final query:</p>
<p><strong>Final query:</strong></p>
<p><code class="language-plaintext highlighter-rouge">;WITH CTE AS(<br></br>SELECT ROW_NUMBER()OVER(PARTITION BY col1, col2, col5 ORDER BY col1, col2, col5) AS RN<br></br>FROM dbo.Table<br></br>)<br></br>DELETE FROM CTE WHERE RN > 1</code></p>Code ApexSituation:Increasing Linux VPS RAM for Free Using ZRAM - udev rule2021-01-12T23:08:10+00:002021-01-12T23:08:10+00:00www.codeapex.com/increasing-linux-vps-ram-for-free-using-zram-udev-rule<h2 id="situation">Situation:</h2>
<p>You are reaching or exceeding the RAM limits of your Linux VPS.</p>
<h2 id="objective">Objective:</h2>
<p>An increase in the amount of non-virtualized memory available to your applications on your VPS without resorting to paying for an upgraded VPS.</p>
<h2 id="plan--execution">Plan + Execution:</h2>
<p>Note we want an increase in the amount of fast physical memory available to our applications. Increasing virtualized memory (via an increase in swap partition or swap file sizes) would not meet our objective; though if your VPS is on SSDs then upping swap space may not be a bad option since solid state disks are substantially faster than traditional hard disks (but still not as fast as physical memory).</p>
<p>One way of making more physical memory available to your applications is to be more efficient with the use of the RAM available. We can accomplish this by compressing data stored in memory; specifically we will use <a href="http://en.wikipedia.org/wiki/Zram" title="ZRAM Wikipedia">ZRAM</a> to appropriate a percentage of total RAM as compressed block devices. Data stored in the area will be compressed and will take up less memory than if they weren’t.</p>
<p>Realize though that ZRAM will put a little more load on the CPU since it now has to compress/decompress data in that portion. So if you also reaching or exceeding the CPU limits of your VPS you should do testing to see if ZRAM helps your situation or perhaps consider actually upgrading your VPS.</p>
<p>Note some commands listed below may require superuser priviledges. Either</p>
<ol>
<li>use the <code class="language-plaintext highlighter-rouge">su</code> command to change the login session’s owner to root; or</li>
<li>prepend the commands with <code class="language-plaintext highlighter-rouge">sudo</code> (e.g. <code class="language-plaintext highlighter-rouge">sudo swapon -s</code>); or</li>
<li>login as root directly</li>
</ol>
<h3 id="checking-kernel-support">Checking Kernel Support:</h3>
<p>In a terminal run this command:<br />
<code class="language-plaintext highlighter-rouge">modprobe -nv zram</code></p>
<p>If the result is a path to zram.ko then your kernel supports zram as a loadable module.</p>
<h3 id="checking-if-zram-is-already-enabled">Checking if ZRAM is Already Enabled:</h3>
<p>Some distributions come with ZRAM enabled by default (such as Lubuntu). Before continuing onward we should double check to ensure we are not trying to accomplish something that is already done.<br />
Check to see if the module is loaded:<br />
<code class="language-plaintext highlighter-rouge">lsmod | grep zram</code></p>
<p>If it is loaded (the result is a line starting with zram) check to see if the compressed block devices are setup and running as available swap devices:<br />
<code class="language-plaintext highlighter-rouge">swapon -s</code></p>
<p>If you see devices listed such as /dev/zram0, /dev/zram1,… then ZRAM is already running and there is nothing you need to do further.</p>
<h3 id="creating-the-udev-rule">Creating the udev rule</h3>
<p>Enable the module:<br />
/etc/modules-load.d/zram.conf<br />
<code class="language-plaintext highlighter-rouge">zram</code></p>
<p>Configure the number of /dev/zram nodes you need.<br />
/etc/modprobe.d/zram.conf<br />
<code class="language-plaintext highlighter-rouge">options zram num_devices=2</code></p>
<p>Create the udev rule as shown in the example.<br />
/etc/udev/rules.d/99-zram.rules<br />
<code class="language-plaintext highlighter-rouge">KERNEL=="zram0", ATTR{disksize}="512M" RUN="/sbin/mkswap /dev/zram0", TAG+="systemd"<br></br>KERNEL=="zram1", ATTR{disksize}="512M" RUN="/sbin/mkswap /dev/zram1", TAG+="systemd"</code></p>
<p>Add /dev/zram to your fstab.<br />
/etc/fstab<br />
<code class="language-plaintext highlighter-rouge">/dev/zram0 none swap sw 0 0<br></br>/dev/zram1 none swap sw 0 0</code></p>
<p>As for how large should the zram devices be in total depends on the kind of workload, note the following comment from one of ZRAM’s developers (back when it was known as Compcache): <a href="https://code.google.com/p/compcache/wiki/CompilingAndUsingNew" title="ZRAM (Compcache) Nitin Gupta Comment">https://code.google.com/p/compcache/wiki/CompilingAndUsingNew</a></p>
<blockquote>
<p><span class="author">Comment by project member <a href="https://code.google.com/u/nitingupta910@gmail.com/">nitingupta910@gmail.com</a>, </span> <span class="date" title="Fri May 7 01:35:52 2010">May 7, 2010</span></p>
<p>@Paczesiowa: ramzswap should not be given so much of memory as its just a (virtual) swap device — it can compress only anonymous (say, process stack, heap etc.) memory. There are also other kinds of caches in the system, say filesystem cache which also require lot of memory. So, to have a good balance among all the different caches, ramzswap should not be given nearly 100% of RAM.</p>
<p>Appropriate amount of ramzswap memory depends on kind of workload. For typical desktop workloads, I found 25% to work just fine (this is the default).</p>
</blockquote>
<h3 id="reboot">Reboot:</h3>
<p>Reboot.</p>
<h3 id="verify">Verify:</h3>
<p>Verify that the module is loaded:<br />
<code class="language-plaintext highlighter-rouge">lsmod | grep zram</code></p>
<p>If the result is a line starting with zram then it is loaded.</p>
<p>Verify that the compressed block devices are setup and running as available swap devices:<br />
<code class="language-plaintext highlighter-rouge">swapon -s</code></p>
<p>If you see devices listed such as /dev/zram0, /dev/zram1,… then the compressed block devices are enabled as swap devices and the setup of ZRAM is complete.</p>Code ApexSituation:Installing Bodhi Linux on an Acer C720 Chromebook with SeaBIOS as default.2014-12-15T18:59:22+00:002014-12-15T18:59:22+00:00www.codeapex.com/installing-bodhi-linux-on-an-acer-c720-chromebook-with-seabios-as-default<h2 id="situation">Situation:</h2>
<p>We have an Acer C720 Chromebook and want Chrome OS removed and Bodhi Linux installed on it. Additionally we want to have it boot to Linux by default without having to start SeaBIOS manually by pressing <code class="language-plaintext highlighter-rouge">Ctrl + L</code> at the white boot splash screen.</p>
<h2 id="objective">Objective:</h2>
<p>Acer C720 Chromebook with Chrome OS removed and Bodhi Linux installed with SeaBIOS booting by default.</p>
<h2 id="plan--execution">Plan + Execution:</h2>
<h3 id="outline">Outline</h3>
<p>The general installation procedure:</p>
<ol>
<li>Enable developer mode to allow access to the superuser shell in Chrome OS.</li>
<li>Accessing the superuser shell.</li>
<li>Enable legacy boot / SeaBIOS.</li>
<li>Set SeaBIOS as default.</li>
<li>Prepare the installation media (USB stick).</li>
<li>Boot from the installation media and install Bodhi Linux.</li>
</ol>
<h3 id="enabling-developer-mode"><span class="mw-headline" id="Enabling_Developer_Mode">Enabling Developer Mode</span></h3>
<ol>
<li>Turn on the Chromebook.</li>
<li>Press and hold the <code class="language-plaintext highlighter-rouge">Esc + F3 (Refresh)</code> keys, then press the <code class="language-plaintext highlighter-rouge">Power</code> button. This enters Recovery Mode.</li>
<li>Press <code class="language-plaintext highlighter-rouge">Ctrl + D</code> (no prompt). It will ask you to confirm, then the system will revert its state and enable Developer Mode.</li>
</ol>
<div> **Note:** Press `Ctrl + D` (or wait 30 seconds for the beep and boot) at the white boot splash screen to enter Chrome OS.</div>
<h3 id="accessing-the-superuser-shell"><span class="mw-headline" id="Accessing_the_superuser_shell">Accessing the superuser shell</span></h3>
<p>Start a terminal session. There are two ways to start a terminal session in Chrome OS (in dev-mode): VT-2 or crosh. For our purposes here it does not matter which way is used.</p>
<p><span class="collapseomatic " id="id6361a8ca9ce48" tabindex="0" title="Get the command prompt through VT-2">Get the command prompt through VT-2</span></p>
<div class="collapseomatic_content " id="target-id6361a8ca9ce48">> #### Get the command prompt through VT-2
>
> One way to get the login prompt is through something called VT-2, or “virtual terminal 2”. This is probably familiar to the Linux hackers out there. You can get to VT-2 by pressing:
>
> <div>[ Ctrl ] [ Alt ] [ => ]</div>…where the \[ => \] key is the right-arrow key just above the number 3 on your keyboard.
>
> Once you have the login prompt, you should see a set of instructions telling you about command-line access. <span style="color: #ff0000;">By default, you can login as the </span>`chronos`<span style="color: #ff0000;"> user with no password. This includes the ability to do password-less sudo.</span> The instructions on the screen will tell you how you can set a password. They also tell you how to disable screen dimming.
>
> <div>The instructions do tell you how to get back to the browser, but because it’s so important, I’ll also put it here. Just press:</div>> <div><div>[ Ctrl ] [ Alt ] [ <= ]</div></div>
>
> <div>…where the [ <= ] key is the left-arrow key just above the number 1 on your keyboard.</div>**SIDE NOTE**: For the technical-minded, you may realize that the top-rows of the keyboard on a Chrome OS device are actually treated by Linux as the keys F1 through F10. Thus, the \[ => \] key is actually F2 and the \[ <= \] key is actually F1.
>
> **SIDE NOTE**: If you’re fooling around, you might also notice that kernel messages show up on VT-8.
>
> - - - - - -
</div>
<p><span class="collapseomatic " id="id6361a8ca9ce87" tabindex="0" title="Getting the command prompt through crosh">Getting the command prompt through crosh</span><div class="collapseomatic_content " id="target-id6361a8ca9ce87">#### Getting the command prompt through crosh</p>
<div>Because you booted into developer mode, you also have an alternate way to get a terminal prompt. I’ll mention this as well, since the alternate shell is a little nicer (in the very least, it keeps your screen from dimming on you), even if it is a little harder to get to. To use this alternate way:</div>
<div>1. Go through the standard Chrome OS login screen (you’ll need to setup a network, etc) and get to the web browser. It’s OK if you login as guest.
2. Press \[ Ctrl \] \[ Alt \] \[ T \] to get the crosh shell.
3. Use the `shell` command to get the shell prompt. NOTE: even if you set a password for the `chronos` user, you won’t need it here (though you still need it for `sudo` access)
</div>
<p>Note that entering the shell this way doesn’t give you all the instructions that VT-2 does (like how to set your password). You might want to follow the VT-2 steps once just to get the instructions.</p>
<p>If you want to get back to the browser without killing the shell, you can do it with [ Alt ] [ Tab ].</p>
<p><strong>SIDE NOTE</strong>: You can actually create as many shells as you want. Just hit [ Ctrl ] [ Alt ] [ T ] again and a second shell will be opened. You can [ Alt ] [ Tab ] between them.</p>
<p></div>In the terminal open a bash shell with the <code class="language-plaintext highlighter-rouge">shell</code> command. Then become superuser with <code class="language-plaintext highlighter-rouge">sudo bash</code>.</p>
<h3 id="enabling-seabios"><span class="mw-headline" id="Enabling_SeaBIOS">Enabling SeaBIOS</span></h3>
<p>This method will allow you to access the pre-installed version of SeaBIOS through the Developer Mode screen in Coreboot.</p>
<ul>
<li>Inside your superuser shell enter:</li>
</ul>
<p><code class="language-plaintext highlighter-rouge">crossystem dev_boot_usb=1 dev_boot_legacy=1</code></p>
<ul>
<li>Reboot the machine.</li>
</ul>
<p>You can now start SeaBIOS by pressing <code class="language-plaintext highlighter-rouge">Ctrl + L</code> at the white boot splash screen.</p>
<h3 id="boot-to-seabios-by-default"><span class="mw-headline" id="Boot_to_SeaBIOS_by_default">Boot to SeaBIOS by default</span></h3>
<p>To boot SeaBIOS by default, you will need to run <a href="https://chromium.googlesource.com/chromiumos/platform/vboot_reference/+/master/scripts/image_signing/set_gbb_flags.sh"><code class="language-plaintext highlighter-rouge">set_gbb_flags.sh</code></a> in Chrome OS (already included in Chrome OS).</p>
<div> **Note:** It is a good idea to do this and have SeaBIOS boot by default even if you do not mind pressing `Ctrl + L` at boot. The reason being is if you do not set the GBB flags then your system might become corrupted on empty battery, Chrome OS will be forced to recover and you will lose your Linux installation on the internal storage.</div>
<div> **Warning:** If you do not disable the write protection before setting the GBB flags you endanger wiping out the RW-LEGACY part of the firmware (i.e. SeaBIOS) and your system might not boot (should be recoverable with Chrome OS recovery media). Updated versions of [`set_gbb_flags.sh`](https://chromium.googlesource.com/chromiumos/platform/vboot_reference/+/master/scripts/image_signing/set_gbb_flags.sh) won’t let you set the GBB flags without disabling the write protection.</div>
<ul>
<li>
<p>Disable the hardware write protection by removing screw 7 here: <a href="http://www.chromium.org/_/rsrc/1381990807648/chromium-os/developer-information-for-chrome-os-devices/acer-c720-chromebook/c720-chromebook-annotated-innards.png">http://www.chromium.org/_/rsrc/1381990807648/chromium-os/developer-information-for-chrome-os-devices/acer-c720-chromebook/c720-chromebook-annotated-innards.png </a>Note: Putting the cover back on with just screw 6 in should be enough to boot and complete the rest of the needed steps before putting the write-protect screw back in.</p>
</li>
<li>
<p>Boot and start a <a href="https://wiki.archlinux.org/index.php/Chromebook#Accessing_the_superuser_shell" title="Chromebook">superuser shell</a> and enter:</p>
</li>
</ul>
<p><code class="language-plaintext highlighter-rouge">sudo su<br></br></code></p>
<ul>
<li>Disable the software write protection.</li>
</ul>
<p><code class="language-plaintext highlighter-rouge">flashrom --wp-disable<br></br></code></p>
<ul>
<li>Check that write protection is disabled.</li>
</ul>
<p><code class="language-plaintext highlighter-rouge">flashrom --wp-status<br></br></code></p>
<ul>
<li>Run <code class="language-plaintext highlighter-rouge">set_gbb_flags.sh</code> with no parameters.</li>
</ul>
<p><code class="language-plaintext highlighter-rouge">set_gbb_flags.sh<br></br></code></p>
<div> **Note:** Recent versions of Chrome OS have moved the script to /usr/share/vboot/bin/set_gbb_flags.sh which isn’t in $PATH by default.</div>
<ul>
<li>Make sure you get the following output.</li>
</ul>
<p><code class="language-plaintext highlighter-rouge">GBB_FLAG_DEV_SCREEN_SHORT_DELAY 0x00000001<br></br>GBB_FLAG_FORCE_DEV_SWITCH_ON 0x00000008<br></br>GBB_FLAG_FORCE_DEV_BOOT_LEGACY 0x00000080<br></br>GBB_FLAG_DEFAULT_DEV_BOOT_LEGACY 0x00000400<br></br></code></p>
<ul>
<li>Now set SeaBIOS as default.</li>
</ul>
<p><code class="language-plaintext highlighter-rouge"># set_gbb_flags.sh 0x489<br></br></code></p>
<ul>
<li>Enable back the software write protection.</li>
</ul>
<p><code class="language-plaintext highlighter-rouge">flashrom --wp-enable<br></br></code><br />
Your Chromebook now will boot to SeaBIOS by default, at this point you should re-enable the hardware write protection by reinserting the write protect screw.</p>
<h3 id="prepare-the-installation-media">Prepare the Installation Media</h3>
<p>Navigate and retrieve the latest Bodhi Linux ISO from here:</p>
<p><a href="http://sourceforge.net/projects/bodhilinux/files/?source=navbar" title="Bodhi Linux ISO">http://sourceforge.net/projects/bodhilinux/files/?source=navbar</a></p>
<p>As of this writing the current one we want is: <a href="http://sourceforge.net/projects/bodhilinux/files/3.0.0-rc1/bodhi-3.0.0-chromebook-rc1.iso/download" title="bodhi-3.0.0-chromebook-rc1.iso">bodhi-3.0.0-chromebook-rc1.iso</a></p>
<p>Assuming we are preparing the installation media in a Linux environment, change to the directory where you have downloaded the ISO image to and run:<br />
<code class="language-plaintext highlighter-rouge">dd if=bodhi-c720-chromebook-rc1.iso of=/dev/sdX</code><br />
Where X is replaced with the drive letter of your USB stick. A quick way to find this out is checking the output of the command <code class="language-plaintext highlighter-rouge">dmesg</code> after inserting the usb drive.</p>
<p>If you prefer a GUI tool or are using OSX/Windows to create the flash drive you can find detailed information on this process <a href="https://help.ubuntu.com/community/Installation/FromImgFiles">here</a>.</p>
<h3 id="boot-from-the-installation-media-and-install-bodhi-linux">Boot From the Installation Media and Install Bodhi Linux</h3>
<p>Since we have set SeaBIOS to boot by default we will not need to press <code class="language-plaintext highlighter-rouge">Ctrl + L</code> at the white boot splash screen.</p>
<p>A message will appear to press the <code class="language-plaintext highlighter-rouge">ESC</code> key to select to bring up the boot menu, proceed to do so and select your USB device from the list it provides.</p>
<p>From this point you can follow the <a href="http://wiki.bodhilinux.com/doku.php?id=installation_instructions#iiboot_into_the_live_environment">normal Bodhi install instructions</a>.</p>
<hr />
<h2 id="comments">Comments</h2>
<ol>
<li>
<p>r3spo says<br />
January 19, 2015 at 3:10 am</p>
<p>Thanks for the guide, do you think this is working also on an HP14? (not he pavillion, the newest one).<br />
Cheers</p>
<ul>
<li>
<p>Code Apex says<br />
January 19, 2015 at 6:20 pm</p>
<p>The HP14 is SeaBIOS as well so I believe it should work though I have not tried it; but do note the location of the write protect screw is different: <a href="http://s290.photobucket.com/user/bond304/media/IMG_5313_zpsacbb2723.jpg.html">HP14 Write Protect Screw</a><br />
Also note these directions will not work for really old Chromebooks without SeaBIOS though (such as the Acer C710).<br />
Regarding the Bodhi part specifically the creator of Bodhi Linux, Jeff Hoogland, has a blog post about picking up a HP14 <a href="http://jeffhoogland.blogspot.ca/2014/03/buying-chromebooks-for-their-hardware.html">here</a> so I suspect installing Bodhi would work fine.</p>
</li>
</ul>
</li>
</ol>Code ApexSituation:Encrypting Data on a Linux VPS - LUKS Encrypted Volume via File Method2014-09-11T21:25:02+00:002014-09-11T21:25:02+00:00www.codeapex.com/encrypting-data-on-a-linux-vps-luks-encrypted-volume-via-file-method<h2 id="situation">Situation:</h2>
<p>You wish to have certain files encrypted on your VPS and you do not want to repartition to accommodate an encrypted volume on a partition or logical volume.</p>
<h2 id="objective">Objective:</h2>
<p>A mountable LUKS encrypted volume based on a file in the filesystem.</p>
<h2 id="plan--execution">Plan + Execution:</h2>
<h3 id="install-cryptsetup">Install cryptsetup:</h3>
<p>If cryptsetup is not installed already, use the appropriate command to install it for your distro.</p>
<p>Debian based:<br />
<code class="language-plaintext highlighter-rouge">apt-get install cryptsetup</code></p>
<h3 id="create-encrypted-volume-file">Create Encrypted Volume File:</h3>
<p>An empty non-sparse file is needed to serve as the encrypted volume. We will create such a file of 1G in size named enc-vol-1G at the root level directory, feel free to substitute your own size, name and location preferences for this file; the same goes for any arbitrary naming used here.</p>
<p>Note we need a non-sparse file meaning the full 1G space has been allocated for the file by the filesystem, so we cannot just use the <code class="language-plaintext highlighter-rouge">touch</code> command to create the file; our options are either the <code class="language-plaintext highlighter-rouge">fallocate</code> or <code class="language-plaintext highlighter-rouge">dd</code> commands. For creating the file <code class="language-plaintext highlighter-rouge">fallocate</code> is faster but <code class="language-plaintext highlighter-rouge">dd</code> offers more features, prefilling the allocated space with pseudo-random data using <code class="language-plaintext highlighter-rouge">if=/dev/urandom</code> for instance.</p>
<p><code class="language-plaintext highlighter-rouge">fallocate -l 1GB /enc-vol-1G</code><br />
or<br />
<code class="language-plaintext highlighter-rouge">dd if=/dev/zero of=/enc-vol-1G bs=1M count=1024</code></p>
<h3 id="transform-file-into-a-luks-partition-with-a-filesystem">Transform File into a LUKS Partition with a Filesystem:</h3>
<p>Turn the File Into a LUKS Partition:<br />
<code class="language-plaintext highlighter-rouge">cryptsetup -y luksFormat /enc-vol-1G</code></p>
<p>Open the LUKS Partition and Setup Mapping Name:<br />
<code class="language-plaintext highlighter-rouge">cryptsetup luksOpen /enc-vol-1G encrypted_volume</code></p>
<p>The mapping name is arbitrary and in our case we chose “encrypted_volume”. After running the command a new device is created: <code class="language-plaintext highlighter-rouge">/dev/mapper/encrypted_volume</code> which we can treat in a similar manner as one would a disk partition.</p>
<p>Create a Filesystem on the LUKS Partition:<br />
<code class="language-plaintext highlighter-rouge">mke2fs -t ext4 /dev/mapper/encrypted_volume</code></p>
<p>Close the LUKS Partition Releasing the Mapping Name and Clearing Key from Kernel Memory:<br />
<code class="language-plaintext highlighter-rouge">cryptsetup luksClose encrypted_volume</code></p>
<p>Create a Mount Point:<br />
<code class="language-plaintext highlighter-rouge">mkdir /mnt/secret_stuff</code></p>
<p>Mount point name and location are arbitrary; customize per your needs.</p>
<p>At this point our objective is met and we have a mountable LUKS encrypted volume based on a file in the filesystem.</p>
<h3 id="how-to-use">How to use:</h3>
<p>Opening the LUKS partition:<br />
<code class="language-plaintext highlighter-rouge">cryptsetup luksOpen /enc-vol-1G encrypted_volume</code></p>
<p>Mounting the Encrypted Volume:<br />
<code class="language-plaintext highlighter-rouge">mount /dev/mapper/encrypted_volume /mnt/secret_stuff</code></p>
<p>Anything you now copy to /mnt/secret_stuff will be encrypted.</p>
<p>Examples:<br />
<code class="language-plaintext highlighter-rouge">cp secret_file1.txt /mnt/secret_stuff</code></p>
<p><code class="language-plaintext highlighter-rouge">cp -R secret_dir /mnt/secret_stuff</code></p>
<p>Unmounting:<br />
<code class="language-plaintext highlighter-rouge">umount /mnt/secret_stuff</code></p>
<p>Closing the LUKS Partition:<br />
<code class="language-plaintext highlighter-rouge">cryptsetup luksClose encrypted_volume</code></p>
<h3 id="notes-on-security">Notes on Security:</h3>
<p>Remember this maxim: Physical Access = Root Access</p>
<p>If someone has physical access to a machine they essentially can get root access.</p>
<p>This is more crucial to note when using a VPS. Not only do the operators of the physical server hosting your VPS have “physical” access to your VPS but so does anyone who hacks the actual machine your VPS is on.</p>
<p>While a LUKS partition is open the decryption key is stored in the kernel memory. A skilled person with root access can look for the key in memory or if they have “physical” access to the VPS they can just take a snapshot running state image of the VPS and pull the key from the snapshot.</p>
<p>Essentially what this means is there is a small chance someone could get the encryption key for the encrypted volume while it is unlocked on the VPS.</p>
<p>With that said a LUKS encrypted volume provides protection in the following scenarios assuming the LUKS partition is not currently open during them:</p>
<ol>
<li>The VPS itself is hacked by an intruder who gains root access. Even though the intruder has root access on the VPS they do not have the key to open the encrypted volume (<small>technically though if no one noticed the intrusion they could potentially leave some hidden process to patiently wait for the encrypted volume to be opened to grab the key then</small>).</li>
<li>The physical server hosting the VPS is hacked by an intruder who copies all the data from the physical server offsite before getting found and blocked. This data includes all the data on your VPS’s hard disk (and RAM if an intruder took a snapshot of your running VPS) at the time the data was copied; but since the LUKS encrypted volume was closed at the time and the key not in your VPS’s RAM, the intruder will not be able to access the data in the encrypted volume in their copy of your VPS data.</li>
</ol>
<p>With that said data stored in a LUKS encrypted volume is more secure than otherwise due to the additional hurdles an intruder would have to navigate to even get a chance to access the data.</p>
<h2 id="misc-tips">Misc Tips:</h2>
<h3 id="multiple-passphrases">Multiple Passphrases:</h3>
<p>A LUKS device has 8 key slots that can be used for passphrases (or key files if one is inclined) to unlock it. Use the luksDump action of the cryptsetup command to to show the header information for the LUKS device and at the bottom will be information on the key slots.</p>
<p><code class="language-plaintext highlighter-rouge">cryptsetup luksDump /enc-vol-1G</code></p>
<p>output snippet with key slot information:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><pre class="lang:default decode:true">Key Slot 0: ENABLED
Iterations: 130398
Salt: dc 66 3b 57 8b 70 29 d2 ad 36 78 6c 88 01 65 81
98 df 17 7e 16 e6 76 46 d8 53 d7 40 ec a1 11 04
Key material offset: 8
AF stripes: 4000
Key Slot 1: DISABLED
Key Slot 2: DISABLED
Key Slot 3: DISABLED
Key Slot 4: DISABLED
Key Slot 5: DISABLED
Key Slot 6: DISABLED
Key Slot 7: DISABLED
</code></pre></div></div>
<p>Key Slot 0 is currently being used for the passphrase we initially setup our LUKS device with. To add an additional passphrase:</p>
<p><code class="language-plaintext highlighter-rouge">cryptsetup luksAddKey /enc-vol-1G</code></p>
<p>You will be first asked to enter any passphrase that unlocks the volume, then you will be prompted to enter a new additional passphrase that can be used to unlock the volume. The passphrase will use the next available key slot, alternatively to use a specific key slot for the passphrase (ex. using key slot 7): <code class="language-plaintext highlighter-rouge">cryptsetup --key-slot 7 luksAddKey /enc-vol-1G</code></p>
<p>To remove a passphrase use the luksRemoveKey action of the cryptsetup command:</p>
<p><code class="language-plaintext highlighter-rouge">cryptsetup luksRemoveKey /enc-vol-1G</code></p>
<p>You will be prompted for the passphrase you want to remove. Alternatively to disable/remove a specific key slot (ex. remove key slot 7): <code class="language-plaintext highlighter-rouge">cryptsetup luksKillSlot /enc-vol-1G 7</code></p>Code ApexSituation:Increasing Linux VPS RAM for Free Using ZRAM - rc script2014-07-31T18:03:29+00:002014-07-31T18:03:29+00:00www.codeapex.com/increasing-linux-vps-ram-for-free-zram-rcscript<h2 id="situation">Situation:</h2>
<p>You are reaching or exceeding the RAM limits of your Linux VPS.</p>
<h2 id="objective">Objective:</h2>
<p>An increase in the amount of non-virtualized memory available to your applications on your VPS without resorting to paying for an upgraded VPS.</p>
<h2 id="plan--execution">Plan + Execution:</h2>
<p>Note we want an increase in the amount of fast physical memory available to our applications. Increasing virtualized memory (via an increase in swap partition or swap file sizes) would not meet our objective; though if your VPS is on SSDs then upping swap space may not be a bad option since solid state disks are substantially faster than traditional hard disks (but still not as fast as physical memory).</p>
<p>One way of making more physical memory available to your applications is to be more efficient with the use of the RAM available. We can accomplish this by compressing data stored in memory; specifically we will use <a href="http://en.wikipedia.org/wiki/Zram" title="ZRAM Wikipedia">ZRAM</a> to appropriate a percentage of total RAM as compressed block devices. Data stored in the area will be compressed and will take up less memory than if they weren’t.</p>
<p>Realize though that ZRAM will put a little more load on the CPU since it now has to compress/decompress data in that portion. So if you also reaching or exceeding the CPU limits of your VPS you should do testing to see if ZRAM helps your situation or perhaps consider actually upgrading your VPS.</p>
<p>Note some commands listed below may require superuser priviledges. Either</p>
<ol>
<li>use the <code class="language-plaintext highlighter-rouge">su</code> command to change the login session’s owner to root; or</li>
<li>prepend the commands with <code class="language-plaintext highlighter-rouge">sudo</code> (e.g. <code class="language-plaintext highlighter-rouge">sudo swapon -s</code>); or</li>
<li>login as root directly</li>
</ol>
<h3 id="checking-kernel-support">Checking Kernel Support:</h3>
<p>In a terminal run this command:<br />
<code class="language-plaintext highlighter-rouge">modprobe -nv zram</code></p>
<p>If the result is a path to zram.ko then your kernel supports zram as a loadable module.</p>
<h3 id="checking-if-zram-is-already-enabled">Checking if ZRAM is Already Enabled:</h3>
<p>Some distributions come with ZRAM enabled by default (such as Lubuntu). Before continuing onward we should double check to ensure we are not trying to accomplish something that is already done.<br />
Check to see if the module is loaded:<br />
<code class="language-plaintext highlighter-rouge">lsmod | grep zram</code></p>
<p>If it is loaded (the result is a line starting with zram) check to see if the compressed block devices are setup and running as available swap devices:<br />
<code class="language-plaintext highlighter-rouge">swapon -s</code></p>
<p>If you see devices listed such as /dev/zram0, /dev/zram1,… then ZRAM is already running and there is nothing you need to do further.</p>
<h3 id="creating-the-startup-script">Creating the Startup Script:</h3>
<p>If it does not already exist create the file <strong>/etc/init.d/zram</strong> with the following contents:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><pre class="lang:sh decode:true" title="zram">#!/bin/bash
### BEGIN INIT INFO
# Provides: zram
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Increased Performance In Linux With zRam (Virtual Swap Compressed in RAM)
# Description: Adapted from systemd scripts at https://github.com/mystilleef/FedoraZram
# Included as part of antix-goodies package by anticapitalista <antiX@operamail.com>
# This script was written by tradetaxfree and is found at http://crunchbanglinux.org/forums/topic/15344/zram-a-good-idea/
# Copy this script (as root) from /usr/local/bin to /etc/init.d and then #update-rc.d zram defaults
# After booting verify the module is loaded with: lsmod | grep zram
### END INIT INFO
start() {
# get the number of CPUs
num_cpus=$(grep -c processor /proc/cpuinfo)
# if something goes wrong, assume we have 1
[ "$num_cpus" != 0 ] || num_cpus=1
# set decremented number of CPUs
last_cpu=$((num_cpus - 1))
#default Factor % = 90 change this value here or create /etc/default/zram
FACTOR=25
#& put the above single line in /etc/default/zram with the value you want
[ -f /etc/default/zram ] && source /etc/default/zram || true
factor=$FACTOR # percentage
# get the amount of memory in the machine
memtotal=$(grep MemTotal /proc/meminfo | awk ' { print $2 } ')
mem_by_cpu=$(($memtotal/$num_cpus*$factor/100*1024))
# load dependency modules
# attempt load using syntax for kernels 3.4 onwards falling back to syntax for kernels 3.1 - 3.3
if ! modprobe zram num_devices=$num_cpus && ! modprobe zram zram_num_devices=$num_cpus; then
echo -e "Your Kernel needs to be compiled with ZRAM support:" \
"\n\nDevice Drivers --> Staging Drivers --> Compressed RAM block device support (M)" \
"\nDevice Drivers --> Staging Drivers --> Dynamic compression of swap pages and clean pagecache pages (*)" \
"\n\nThe Liquorix Kernel (http://liquorix.net) has ZRAM support built in."
exit 1
fi
echo "zram devices probed successfully"
# initialize the devices
for i in $(seq 0 $last_cpu); do
echo $mem_by_cpu > /sys/block/zram$i/disksize
# Creating swap filesystems
mkswap /dev/zram$i
# Switch the swaps on
swapon -p 100 /dev/zram$i
done
}
stop() {
# get the number of CPUs
num_cpus=$(grep -c processor /proc/cpuinfo)
# set decremented number of CPUs
last_cpu=$((num_cpus - 1))
# Switching off swap
for i in $(seq 0 $last_cpu); do
if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then
swapoff /dev/zram$i
sleep 1
fi
done
sleep 1
rmmod zram
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 3
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
RETVAL=1
esac
exit $RETVAL
</code></pre></div></div>
<p>The original source of this script can be found here:<br />
<a href="http://crunchbanglinux.org/forums/topic/15344/zram-a-good-idea/" title="zram init script">http://crunchbanglinux.org/forums/topic/15344/zram-a-good-idea/</a></p>
<p>The creator also notes that the latest version of the script can be found here: <a href="http://dl.dropbox.com/u/96561917/zram.zip" title="Latest Version of zram Script">http://dl.dropbox.com/u/96561917/zram.zip</a></p>
<p>We do make some slight changes to the script.</p>
<p>The if statement for the module loading has been corrected to not exit if it successfully loads using syntax for kernels 3.4 and onward.</p>
<p>Additionally take note of the change we make to FACTOR in the script from the default 90 to 25. We implement this change due to the following comment from one of ZRAM’s developers (back when it was known as Compcache): <a href="https://code.google.com/p/compcache/wiki/CompilingAndUsingNew" title="ZRAM (Compcache) Nitin Gupta Comment">https://code.google.com/p/compcache/wiki/CompilingAndUsingNew</a></p>
<blockquote>
<p><span class="author">Comment by project member <a href="https://code.google.com/u/nitingupta910@gmail.com/">nitingupta910@gmail.com</a>, </span> <span class="date" title="Fri May 7 01:35:52 2010">May 7, 2010</span></p>
<p>@Paczesiowa: ramzswap should not be given so much of memory as its just a (virtual) swap device — it can compress only anonymous (say, process stack, heap etc.) memory. There are also other kinds of caches in the system, say filesystem cache which also require lot of memory. So, to have a good balance among all the different caches, ramzswap should not be given nearly 100% of RAM.</p>
<p>Appropriate amount of ramzswap memory depends on kind of workload. For typical desktop workloads, I found 25% to work just fine (this is the default).</p>
</blockquote>
<h3 id="enabling-the-startup-script">Enabling the Startup Script:</h3>
<p>Enable execute permissions on the startup script:<br />
<code class="language-plaintext highlighter-rouge">chmod +x /etc/init.d/zram</code></p>
<p>Add the startup script to the default run level:<br />
<code class="language-plaintext highlighter-rouge">update-rc.d zram defaults</code></p>
<h3 id="verify">Verify:</h3>
<p>Now either run the script manually:<br />
<code class="language-plaintext highlighter-rouge">/etc/init.d/zram start</code><br />
or reboot (preferred since this will test if the script auto executes on boot).</p>
<p>Verify that the module is loaded:<br />
<code class="language-plaintext highlighter-rouge">lsmod | grep zram</code></p>
<p>If the result is a line starting with zram then it is loaded.</p>
<p>Verify that the compressed block devices are setup and running as available swap devices:<br />
<code class="language-plaintext highlighter-rouge">swapon -s</code></p>
<p>If you see devices listed such as /dev/zram0, /dev/zram1,… then the compressed block devices are enabled as swap devices and the setup of ZRAM is complete.</p>Code ApexSituation:Restore an Acer C720 Chromebook to stock after installing Linux on it2014-07-26T01:42:23+00:002014-07-26T01:42:23+00:00www.codeapex.com/restore-an-acer-c720-chromebook-to-stock-after-installing-linux-on-it<h2 id="situation">Situation:</h2>
<p>Following a guide on the internet such as this one <a href="http://www.codeapex.com/installing-bodhi-linux-on-an-acer-c720-chromebook-with-seabios-as-default/" title="Installing Bodhi Linux on an Acer C720 Chromebook with SeaBIOS as default.">here</a> for getting Linux installed on an Acer C720 Chromebook, summarized you:</p>
<ol>
<li>Switched the Chromebook to Developer Mode</li>
<li>Enabled SeaBIOS</li>
<li>Made SeaBIOS default on boot.</li>
<li>Installed Linux</li>
</ol>
<h2 id="objective">Objective:</h2>
<p>Restore the Acer C720 Chromebook back to factory stock.</p>
<h2 id="plan--execution">Plan + Execution:</h2>
<p>First realize there are essentially two things we want back at stock:</p>
<ol>
<li>A fresh Chrome OS installed.</li>
<li>The default BIOS and its stock settings.</li>
</ol>
<h3 id="restoring-chrome-os">Restoring Chrome OS</h3>
<ol>
<li>Find your version of your Chromebook by holding Ctrl+D on boot to get to the recovery screen. You will need it when creating the recovery USB flash drive by entering it when it asks for it in the creation script.</li>
<li>Create a recovery USB flash drive for Chrome OS, follow the directions in step 2 here for the OS you are creating the recovery USB flash drive in: <a href="https://support.google.com/chromebook/answer/1080595?hl=en" title="Create a recovery USB flash drive">https://support.google.com/chromebook/answer/1080595?hl=en</a> or see directions for Linux and Windows below:<br />
<span class="collapseomatic " id="id6361a8ca93898" tabindex="0" title="Directions for creating the recovery USB flash drive in Linux">Directions for creating the recovery USB flash drive in Linux</span><div class="collapseomatic_content " id="target-id6361a8ca93898">
<ol>
<li>
<blockquote>
<p>Click this link to download the Recovery Tool: <a href="https://dl.google.com/dl/edgedl/chromeos/recovery/linux_recovery.sh">https://dl.google.com/dl/edgedl/chromeos/recovery/linux_recovery.sh</a></p>
</blockquote>
</li>
<li>
<blockquote>
<p>Modify the script permissions to allow execution with the following command: <code class="language-plaintext highlighter-rouge">$ sudo chmod 755 linux_recovery.sh</code></p>
</blockquote>
</li>
<li>
<blockquote>
<p>Run the script with root privileges with the following command: <code class="language-plaintext highlighter-rouge">$ sudo bash linux_recovery.sh</code></p>
</blockquote>
</li>
<li>
<blockquote>
<p>Follow the prompts from the tool to complete building the operating system image.</p>
</blockquote>
</li>
</ol>
<p></div><br />
<span class="collapseomatic " id="id6361a8ca938f5" tabindex="0" title="Directions for creating the recovery USB flash drive in Windows">Directions for creating the recovery USB flash drive in Windows</span><div class="collapseomatic_content " id="target-id6361a8ca938f5"></p>
<ol>
<li>
<blockquote>
<p>Click this link to download the Recovery Tool: <a href="https://dl.google.com/dl/chromeos/recovery/chromeosimagecreatorV2.exe">https://dl.google.com/dl/chromeos/recovery/chromeosimagecreatorV2.exe</a> If you’re a network administrator for your school, business, or organization, click this link to download the Recovery Tool: <a href="https://dl.google.com/dl/chromeos/recovery/chromeosimagecreator.exe">https://dl.google.com/dl/chromeos/recovery/chromeosimagecreator.exe</a></p>
</blockquote>
</li>
<li>
<blockquote>
<p>Run the tool and follow the instructions that appear on your screen.</p>
</blockquote>
</li>
<li>
<blockquote>
<p>After you recover your Chromebook, you <strong><em>must</em></strong> format your USB flash drive or SD card using the Recovery Tool. If you don’t format your USB flash drive or SD card, you won’t be able to use all the storage space on your external device. Additionally, your USB flash drive or SD card may not be recognizable by Windows. <a href="https://support.google.com/chromebook/answer/2554370">See instructions for reformatting your USB flash drive or SD card</a>.</p>
</blockquote>
</li>
</ol>
<p></div></p>
</li>
<li>Reinstall Chrome OS, step 3 here <a href="https://support.google.com/chromebook/answer/1080595?hl=en" title="Create a recovery USB flash drive">https://support.google.com/chromebook/answer/1080595?hl=en</a> or see directions below:<br />
<span class="collapseomatic " id="id6361a8ca93916" tabindex="0" title="Reinstall Chrome OS, step 3">Reinstall Chrome OS, step 3</span><div class="collapseomatic_content " id="target-id6361a8ca93916">
<ol>
<li>
<blockquote>
<p>Start your Chromebook.</p>
</blockquote>
</li>
<li>
<blockquote>
<p>When the “Chrome OS is missing or damaged” screen appears, insert the USB flash drive or SD card you created into the USB port or SD card slot on your Chrome device. Note if the Chromebook is not booting to Recovery mode by default, hold down the <span style="color: #990000;">ESC</span> and <span style="color: #990000;">Refresh</span> (F3) keys and press the <span style="color: #990000;">Power</span> button to get into Recovery mode.</p>
</blockquote>
</li>
<li>
<blockquote>
<p>Wait for the Chromebook to boot up from the flash drive.</p>
</blockquote>
</li>
<li>
<blockquote>
<p>Follow the instructions that appear on the screen.</p>
</blockquote>
</li>
<li>
<blockquote>
<p>On successful installation of the Chrome operating system, you will be prompted to remove the USB flash drive or SD card.</p>
</blockquote>
</li>
<li>
<blockquote>
<p>Remove the USB flash drive or SD card when prompted, and your Chromebook will automatically restart.</p>
</blockquote>
</li>
</ol>
<p></div></p>
</li>
</ol>
<h3 id="restore-the-default-bios-with-stock-settings">Restore the default BIOS with stock settings</h3>
<ol>
<li>Remove the write-protect screw inside the Chromebook. Recall from when you did this to set SeaBIOS as default originally, it is screw 7 here: <a href="http://www.chromium.org/_/rsrc/1381990807648/chromium-os/developer-information-for-chrome-os-devices/acer-c720-chromebook/c720-chromebook-annotated-innards.png">http://www.chromium.org/_/rsrc/1381990807648/chromium-os/developer-information-for-chrome-os-devices/acer-c720-chromebook/c720-chromebook-annotated-innards.png </a>Note: Putting the cover back on with just screw 6 in should be enough to boot and complete the rest of the needed steps before putting the write-protect screw back in.</li>
<li>Switch the Chromebook to Developer mode: First hold down the <span style="color: #990000;">ESC</span> and <span style="color: #990000;">Refresh</span> (F3) keys and press the <span style="color: #990000;">Power</span> button to power on the Chromebook. Then at the Recovery screen press <span style="color: #990000;">Ctrl-D</span>. It will ask you to confirm, then reboot into dev-mode.On boot you will see a warning screen and will need to press <span style="color: #990000;">Ctrl-D</span> or wait 30 seconds to continue booting.</li>
<li>Start a terminal session. There are two ways to start a terminal session in Chrome OS (in dev-mode): VT-2 or crosh. For our purposes here it does not matter which way is used.<br />
<span class="collapseomatic " id="id6361a8ca93932" tabindex="0" title="Get the command prompt through VT-2">Get the command prompt through VT-2</span><div class="collapseomatic_content " id="target-id6361a8ca93932">> #### Get the command prompt through VT-2
<blockquote>
<p>One way to get the login prompt is through something called VT-2, or “virtual terminal 2”. This is probably familiar to the Linux hackers out there. You can get to VT-2 by pressing:</p>
<div>[ Ctrl ] [ Alt ] [ => ]</div>
<p>…where the [ => ] key is the right-arrow key just above the number 3 on your keyboard.</p>
<p>Once you have the login prompt, you should see a set of instructions telling you about command-line access. <span style="color: #ff0000;">By default, you can login as the </span><code class="language-plaintext highlighter-rouge">chronos</code><span style="color: #ff0000;"> user with no password. This includes the ability to do password-less sudo.</span> The instructions on the screen will tell you how you can set a password. They also tell you how to disable screen dimming.</p>
<div>The instructions do tell you how to get back to the browser, but because it’s so important, I’ll also put it here. Just press:</div>
<blockquote>
<div><div>[ Ctrl ] [ Alt ] [ <= ]</div></div>
</blockquote>
<div>…where the [ <= ] key is the left-arrow key just above the number 1 on your keyboard.</div>
<p><strong>SIDE NOTE</strong>: For the technical-minded, you may realize that the top-rows of the keyboard on a Chrome OS device are actually treated by Linux as the keys F1 through F10. Thus, the [ => ] key is actually F2 and the [ <= ] key is actually F1.</p>
<p><strong>SIDE NOTE</strong>: If you’re fooling around, you might also notice that kernel messages show up on VT-8.</p>
<hr />
</blockquote>
<p></div><br />
<span class="collapseomatic " id="id6361a8ca93961" tabindex="0" title="Getting the command prompt through crosh">Getting the command prompt through crosh</span><div class="collapseomatic_content " id="target-id6361a8ca93961">#### Getting the command prompt through crosh</p>
<div>Because you booted into developer mode, you also have an alternate way to get a terminal prompt. I’ll mention this as well, since the alternate shell is a little nicer (in the very least, it keeps your screen from dimming on you), even if it is a little harder to get to. To use this alternate way:</div>
<div>
1. Go through the standard Chrome OS login screen (you’ll need to setup a network, etc) and get to the web browser. It’s OK if you login as guest.
2. Press \[ Ctrl \] \[ Alt \] \[ T \] to get the crosh shell.
3. Use the `shell` command to get the shell prompt. NOTE: even if you set a password for the `chronos` user, you won’t need it here (though you still need it for `sudo` access)
</div>
<p>Note that entering the shell this way doesn’t give you all the instructions that VT-2 does (like how to set your password). You might want to follow the VT-2 steps once just to get the instructions.</p>
<p>If you want to get back to the browser without killing the shell, you can do it with [ Alt ] [ Tab ].</p>
<p><strong>SIDE NOTE</strong>: You can actually create as many shells as you want. Just hit [ Ctrl ] [ Alt ] [ T ] again and a second shell will be opened. You can [ Alt ] [ Tab ] between them.</p>
<p></div></p>
</li>
<li>In the terminal open a bash shell with the <code class="language-plaintext highlighter-rouge">shell</code> command. Then become superuser with <code class="language-plaintext highlighter-rouge">sudo bash</code>.</li>
<li>Run these commands:
<ol>
<li><code class="language-plaintext highlighter-rouge">crossystem dev_boot_usb=1 dev_boot_legacy=1 disable_dev_request=1</code></li>
<li><code class="language-plaintext highlighter-rouge">flashrom --wp-disable</code></li>
<li><code class="language-plaintext highlighter-rouge">set_gbb_flags.sh 0×000<br></br></code>(note: if set_gbb_flags.sh is not found then try this full path to script: /usr/share/vboot/bin/set_gbb_flags.sh 0x000)</li>
<li><code class="language-plaintext highlighter-rouge">flashrom --wp-enable</code></li>
<li><code class="language-plaintext highlighter-rouge">chromeos-firmwareupdate --sb_extract /tmp</code></li>
<li><code class="language-plaintext highlighter-rouge">flashrom -w /tmp/bios.bin -i RW_LEGACY</code></li>
</ol>
</li>
<li>Shutdown the Chromebook and replace the write-protect screw. Put the backcover back on with all the screws.</li>
<li>The Chromebook should now be returned to stock.</li>
</ol>
<hr />
<h2 id="comments">Comments</h2>
<ol>
<li>
<p>magnus says<br />
July 4, 2015 at 2:46 pm</p>
<p>Per John Lewis’ script, I completely flashed a custom rom such that my chromebook doesn’t get me to the recovery screen after pressing ctrl+D; my SSD is also dedicated entirely to my linux installation. Any ideas on how to get ChromeOs back?</p>
<ul>
<li>
<p>Code Apex says<br />
July 7, 2015 at 11:56 am</p>
<p>Assuming you already have the made the recovery usb drive and are just having trouble getting into Recovery mode to restore from the usb drive (Restoring Chrome OS step 3 above): Hold down the ESC and Refresh (F3) keys and press the Power button to get into Recovery mode.</p>
</li>
</ul>
</li>
<li>
<p>jesseflb says<br />
July 8, 2015 at 12:55 am</p>
<p>still booting to my Linux installation which is Eos i have already made a recovery drive but when i insert it and power on with the escape and f3 keys held down only the seabios come up then my linux installation ..no chrome os recovery….is there any other work around to this? i am actually removing my linux installation cause i have an issue with my wireless not working anymore just started one day like that ive sen some people with the same problem and there’s no permanent fix only temporary……but switching back solves it and everything workss as new again.</p>
<ul>
<li>
<p>jesseflb says<br />
July 8, 2015 at 12:57 am</p>
<p>btw, i am using an acer c720…… i greatly aprreciate any help you can offer</p>
<ul>
<li>
<p>Code Apex says<br />
July 14, 2015 at 10:06 am</p>
<p>Try booting normally but tap the space bar as its booting. On my C720 right before seabios it briefly shows a screen that says OS verification is off and says to press space to enable but it only shows for 1-2 seconds before jumping to seabios.</p>
</li>
</ul>
</li>
</ul>
</li>
<li>
<p>Brady Osborne says<br />
August 11, 2015 at 6:02 pm</p>
<p>I think my Chromebook is completely shot. I don’t see the recovery mode option at all, although I can boot into Linux. But how do I run these commands since they aren’t available in my distro?</p>
<ul>
<li>
<p>Code Apex says<br />
August 17, 2015 at 9:40 pm</p>
<p>Where are you getting stuck and are you using a C720?<br />
If you have the usb recovery drive setup and are trying to get the Chromebook to recover from it:<br />
You need to get to the recovery screen and press ctrl+d to start the recovery with the usb drive plugged in.<br />
If you are having trouble getting to the recovery screen try the following:<br />
1) Press and hold the Esc + Refresh keyboard icon keys.<br />
2) Press the Power button, then let go.<br />
3) Continue holding the Esc + Refresh keyboard icon keys until the “Chrome OS is missing or damaged” message appears, then let go.</p>
<p>Some Chromebooks use a special button to enter recovery mode. Refer to your Chromebook’s user manual to find the location of the button.</p>
<p>If the above does not work try tapping the space bar while booting to re-enable OS verification.<br />
(On my C720 right before seabios it briefly shows a screen that says OS verification is off and says to press space to enable but it only shows for 1-2 seconds before jumping to seabios).<br />
When OS verification is enabled then you should be able to enter recovery mode as instructed above.</p>
</li>
<li>
<p>Phil says<br />
February 2, 2016 at 5:43 am</p>
<p>Hi, i know this was a long time ago but did ever get this sorted? im in the same position as you were. I have seabios as default and running ubuntu 14.04 … I cannot get to recovery at all on boot, not even sure its still there. always just boot to seabios menu screen then into ubuntu</p>
<p>did you find a way to get your c720 back to chromeOS ?</p>
<p>any guides would be helpful</p>
<p>Thanks</p>
</li>
</ul>
</li>
<li>
<p>Chris Taylor says<br />
November 26, 2015 at 4:47 am</p>
<p>Hi,<br />
So, I’m in a similar position to the above; I had a linux distort on the SSD of the Acer c720, now I want to get back back to having ChromeOS installed. I have a recovery drive, and this is where I am at:</p>
<ol>
<li>
<p>Boot into recovery mode and install – done.</p>
</li>
<li>
<p>ChromeOS verifies the media and eventually arrives at the “System Recovery Complete” screen – done.</p>
</li>
</ol>
<p>On the restart, regardless of whether or not I hit the space bar to re-enable OS verification, it is ignored and the Seagate BIOS screen tries to boot up from SSD (it is unclear what it tries to boot as the text flashes past) then the screen goes black.</p>
<p>Have you got any suggestions please?</p>
<p>Thanks,
Chris.</p>
</li>
<li>
<p>JD says<br />
January 4, 2016 at 6:45 am</p>
<p>Install Ubuntu Server on my C720 almost 2 yrs ago. Been happy until the SSD died.
Never changed the BIOS, except to enable USB boot.
I have the recovery screen, but every attempt fails at the validation/verification step (around 99%) regardless of how the flash storage was made (Linux script, dd, or Chome browser from Windows). Tried 5 different storage devices – SDHC, USB, USB, USB, SDHC … avoided SanDisk.</p>
<p>Never undid the write-protect screw, until the keyboard needed to be replaced. That’s what started all of this – swapping a broken keyboard out. Put the write protect screw back before attempting any boot. Basically, that screw was only missing when power was removed.</p>
<p>Completely stuck here. Empty SSD, Recovery media seems to be created fine, but never works.
Any leads before I chuck chuck it?</p>
<ul>
<li>
<p>Code Apex says<br />
January 19, 2016 at 10:37 am</p>
<p>For the verification error try using the latest version of the chromebook recovery app to create the media:<br />
<a href="https://chrome.google.com/webstore/detail/chromebook-recovery-utili/jndclpdbaamdhonoechobihbbiimdgai">https://chrome.google.com/webstore/detail/chromebook-recovery-utili/jndclpdbaamdhonoechobihbbiimdgai</a><br />
Earlier versions had a bug where it would fail on verification:<br />
<a href="https://code.google.com/p/chromium/issues/detail?id=352442">https://code.google.com/p/chromium/issues/detail?id=352442</a></p>
<p>Also is the replacement SSD a SATA 42mm M.2 / NGFF not larger than 128GB? I heard there are issues if trying to put anything larger than 128GB into the C720. Check here to make sure the replacement SSD is compatible with the C720:<br />
<a href="http://www.mydigitaldiscount.com/m.2-ngff-ssd-compatibility-list.html">http://www.mydigitaldiscount.com/m.2-ngff-ssd-compatibility-list.html</a></p>
</li>
</ul>
</li>
<li>
<p>peter says<br />
February 11, 2016 at 8:13 am</p>
<p>Same issue here. on an ACER Chromebook 15, CB5-571
I flashed SeaBIOS, WIndows 10 immediately boots.
I do have a very recent Backup of ChromeOS on an DD-Card, but didn’t manage to geet a Backup of the Original BIOS.</p>
<p>There;s NO WAY I can get into BIOS Recovery, I think because the Seabios has completely overwritten the original IOS (and therefore, not showing any Chrome Recovery/Developer options.</p>
<ol>
<li>
<p>I can’t seem to find an original ChromeOS/Coreboot Acer CB5-571 BIOS
IS someone able to post an original BIOS-version?</p>
</li>
<li>
<p>How to flash this original BIOS to completely replace the SeaBIOS from within Windows?</p>
</li>
</ol>
<p>I’ve been reading and searching a lot, founs some info with Linux commands using Flashrom, maybe someone knows another way of going back to original ChromeOS?</p>
</li>
<li>
<p>Jacob Meza says<br />
March 15, 2016 at 12:55 pm</p>
<p>I have a toshiba chromebook 2. I replaced the bios and wrote over the entire hard drive with linux.</p>
<p>The recovery usb process is freezing when I get to ‘syncing your preferences’.</p>
<p>Any help?</p>
</li>
<li>
<p>ShinyCanoe says<br />
May 24, 2016 at 3:18 pm</p>
<p>I got this to work on an Acer C720. What I did was install Chromium OS on the machine, then followed the steps to “Restore the default BIOS with stock settings.” Once the bios was restored I could use the repair usb to reinstall factory chrome is. Chromium OS is the open source equivalent to ChromeOS and it can be flashed to a USB stick. As long as you can still boot from a USB with Chromium on it, you can follow this tutorial and put your computer back good as new. Instructions for installing Chromium can be found here: <a href="http://arnoldthebat.co.uk/wordpress/chromium-os/">http://arnoldthebat.co.uk/wordpress/chromium-os/</a></p>
</li>
<li>
<p>jimmy says<br />
September 10, 2016 at 4:26 pm</p>
<p>how do i backup bios onto sd card and restore it at a later point ?</p>
</li>
<li>
<p>sanjay says<br />
September 11, 2016 at 11:49 am</p>
<p>Hi ShinyCanoe,
I was able to start Chromium OS from SD card, I can see Chromium OS login. what steps you followed to “Restore default BIOS with stock settings…”</p>
</li>
<li>
<p>Ando says<br />
October 20, 2016 at 6:43 am</p>
<p>Thanks for the guide! Worked perfectly 🙂</p>
</li>
<li>
<p>Barry says<br />
January 6, 2017 at 11:20 pm</p>
<p>Thanks for this detailed guide.
Worked perfectly on my C720.</p>
</li>
<li>
<p>tuxnet says<br />
August 14, 2017 at 11:04 am</p>
<p>Thank you.</p>
<p>Work like a charm.</p>
</li>
</ol>Code ApexSituation: