<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by CocoaCaa on Medium]]></title>
        <description><![CDATA[Stories by CocoaCaa on Medium]]></description>
        <link>https://medium.com/@cocoacaa?source=rss-9a5ac175be66------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*77NxbtgoK4LTumWHIn44jA.png</url>
            <title>Stories by CocoaCaa on Medium</title>
            <link>https://medium.com/@cocoacaa?source=rss-9a5ac175be66------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 18:27:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cocoacaa/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[如何使用 CI 來構建你的 Node.js 項目？]]></title>
            <link>https://cocoacaa.medium.com/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8-ci-%E4%BE%86%E6%A7%8B%E5%BB%BA%E4%BD%A0%E7%9A%84-node-js-%E9%A0%85%E7%9B%AE-30ade12f4533?source=rss-9a5ac175be66------2</link>
            <guid isPermaLink="false">https://medium.com/p/30ade12f4533</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[continuous-integration]]></category>
            <dc:creator><![CDATA[CocoaCaa]]></dc:creator>
            <pubDate>Sun, 28 Feb 2021 08:09:55 GMT</pubDate>
            <atom:updated>2024-05-09T01:35:57.719Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c1_sebx-C4S7T5A0mrMRhA.png" /></figure><p>你可以把整個項目壓縮，或者把 git clone 到你的伺服器上運行，</p><p>但是… (´･_･`)</p><h3>為什麼？</h3><p>如果你的項目有一個 native modules，並且和你的本地開發系統不同。你可能需要在服務器上進行 npm install。對於 TypeScript，你可能還需要 transpile 到 JavaScript。如果你的項目是運行在 Docker 上而且有自定義的 Dockerfile 可能需要你在使用前做 docker build。<br>這些步驟可能會讓你在你的本地開發系統上做，但伺服器可能資源較少，不允許你做 build。<br>所以我嘗試使用 CI（持續集成）來構建整個東西，在伺服器上不用 build 而立即使用。d(d＇∀＇)</p><h3>實例需求</h3><p>在本文中，我將用 TypeScript 和 Docker 創建一個 Node.js 項目，並使用自定義的Dockerfile和 native modules（imagemagick，ffmpeg）。<br>其實不僅僅是 Node.js，構建步驟應該適用於大多數情況。但本文將以 Node.js 為例。</p><h3>Step 0 — 代碼和結構</h3><p>首先，設置好項目結構後，我會把它做成如下截圖的樣子：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/321/0*eacSkzdDqQYS8GNZ.png" /><figcaption>例子項目結構</figcaption></figure><p>留意一下 .circleci，在這個例子中，我將使用 CircleCI 作為 CI 構建服務，你可以使用許多不同的 CI 服務，例如 GitLab CI，Travis CI 等等。</p><h3>Step 1 — Dockerfile 和 docker-compose</h3><p>我們可以將所有的 native modules 和 npm modules 安裝到 docker image 裏，在伺服器中直接使用它。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/0*9H_n3ury_JaDMJcQ.png" /><figcaption>例子中的 Dockerfile</figcaption></figure><p>對於本地開發中使用的 docker-compose.yml，我們可以這樣寫：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/0*wY-T4RRlYhs19g52.png" /><figcaption>例子中的 docker-compose.yml</figcaption></figure><p>docker comtainer 是由本地 Dockerfile 構建的，但在伺服器中呢？後面幾步我會給大家展示一下用 “magic” 解決。</p><h3>Step 2 — package.json “scripts”</h3><p>Scripts 可以直接寫在 CI 設置文件上，也可以直接在終端寫，不過我會建議寫在 package.json 上，這樣更容易重用。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/201/0*TaxOdOLLc1YQ79sQ.png" /><figcaption>在 package.json 中的 “scripts” 例子</figcaption></figure><p>“build” transpile 到 JavaScript 中，“start” 是啟動應用程序。</p><h3>Step 3 — 編寫 CircleCI 配置文件</h3><p>在CircleCI中，配置文件將位於&lt;your_project_root&gt;/.circleci/config.yml，CI 工作將是構建 docker image，推送到 Docker hub，構建並壓縮項目文件，推送到 GitHub 的發布頁面。因為它非常長所以我會逐一向你展示配置的部分。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/0*uo1nFM69pwXjYE5V.png" /><figcaption>例子 CircleCI 配置文件 “environment” 和 “executors”</figcaption></figure><p>在例子中，我會把 Docker 推送推到我的 docker hub。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/375/0*P0VXgZE1VX0f4ucI.png" /><figcaption>例子 CircleCI 配置文件的 “workflows”</figcaption></figure><p>只當有一個新的 git 標籤為 /^\d+\/\d+\.\d+$/（如 0.0.1）時，”jobs” 才會執行，並同時運行 Docker build 和項目構建。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/0*c8g27XLpEQDed6yy.png" /><figcaption>例子 CircleCI 配置文件構建 Docker</figcaption></figure><p>build-docker是構建 Docker，之後 persist_to_workspace 為下一個作業共享生成的 docker image。docker-latest 是將構建好的 image 放置到 Docker Hub 中。 $DOCKERHUB_PASS 和 $DOCKERHUB_USERNAME是你的密碼和用戶名，你可以在你的 CircleCI 組織設置中進行設置 → Context。這是用來稍後使用。<a href="https://circleci.com/docs/2.0/env-vars/">https://circleci.com/docs/2.0/env-vars/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0jlpfyZsSdwWAvp5.png" /><figcaption>例子 CircleCI 配置文件構建項目文件</figcaption></figure><p>build 是建立項目文件並存檔。$GITHUB_TOKEN 是你的 GitHub “personal access token” <a href="https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token">https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token</a>，也可以像上面的 Contexts 一樣的設置。你可能會注意到有一個 create-prod-docker-compose.sh，這就是我在Step 1 中提到的 “megic”，現在我們要創建另一個 docker-compose 文件用作在伺服器上。Script 如下：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/407/0*TcWRTpiJXmbdbULN.png" /><figcaption>create-prod-docker-compose.sh</figcaption></figure><p>$IMAGE_NAME 和 $CIRCLE_TAG（git tag）將在CI環境中可用，在這種情況下，將會是 cocoacaa/sample-ci-release-build:0.0.1。</p><h3>Step 4 — 推送到 Git 伺服器</h3><p>在這個例子中，我將把 sources 推到 GitHub，並鏈接到 CircleCI。</p><h3>Step 5 — 設置CI項目</h3><p>進入 CircleCI 項目儀表板，按下剛才推到 GitHub 的項目所在的 “Set Up Project” 按鈕。單擊按鈕後，按下 “Use Existing Config”，因為我們在 sources 中已經有了這個配置文件。</p><h3>Step 6 — 推 git tag，等待構建。</h3><p>現在，我們在主分支中添加一個標籤（如 0.0.1）。回到 CircleCI pipeline 儀表板，你會看到作業正在啟動。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/0*_pLCpQ26t_ME74VI.png" /><figcaption>CircleCI項目建設</figcaption></figure><h3>Step 7 — 檢查內置文件</h3><p>構建完成後，進入Docker Hub，有一個 0.0.1 和最新的標籤。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/340/0*8A8cIv4J7OEO_EbA.png" /><figcaption>Docker Hub 標籤</figcaption></figure><p>另外，在你的 GitHub 發布頁面中，你會在 assets 下找到構建了的檔案。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/198/0*g1dN_8rKlvGcbacL.png" /><figcaption>GitHub 發布頁面中的構建文件</figcaption></figure><p>最後，您可以下載 artifact.tar.gz，解壓後在伺服器中使用 docker-compose up -d 命令運行的項目。</p><p>每次都有代碼修改和提交到 git tag，CI 都會自動構建您的項目文件。部署到您的伺服器上只需要下載發布了的構建文件到您的伺服器上，解壓後立即啟動即可。d(`･∀･)b</p><h3>參考資料</h3><ul><li><a href="https://circleci.com/docs/2.0/building-docker-images/">Running Docker Commands</a></li><li><a href="https://circleci.com/blog/publishing-to-github-releases-via-circleci/">Automate GitHub Releases with CircleCI</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=30ade12f4533" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use CI to build your Node.js project?]]></title>
            <link>https://cocoacaa.medium.com/how-to-use-ci-to-build-your-node-js-project-a2956ebfa4f6?source=rss-9a5ac175be66------2</link>
            <guid isPermaLink="false">https://medium.com/p/a2956ebfa4f6</guid>
            <category><![CDATA[continuous-integration]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[CocoaCaa]]></dc:creator>
            <pubDate>Sun, 28 Feb 2021 07:05:44 GMT</pubDate>
            <atom:updated>2024-05-09T01:35:32.075Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xaMgP9Tg9wSbzEtJBhNRpw.png" /></figure><p>You could just zip your whole project or git clone into your production server and run, but… (´･_･`)</p><h3>Why?</h3><p>If your project has a native module and is different from your local development machine. You may need to do npm install on the server. With TypeScript, you may also need to transpile to JavaScript. If your project is running on Docker with a custom build Dockerfile may require you to do build the image before use.</p><p>Those steps may allow you to do in your local development machine, but the production server may less resource and now allow you to do heavy builds.</p><p>So I try to use CI (Continuous integration) to build the whole thing and on the production server just plug-n-play. d(d＇∀＇)</p><h3>Example requirement</h3><p>In this article, I will create a Node.js project with TypeScript and Docker w/ custom Dockerfile and native modules ( imagemagick, ffmpeg)</p><p>Actually, not only Node.js, the build steps should be applied to most cases. But this article will use Node.js as an example.</p><h3>Step 0 — The codebase and structure</h3><p>First of all, set up the project structure, I will make it like the below screenshot</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/321/1*AcxMH4QHkWOmmU4aF5XpGg.png" /><figcaption>The example project structure</figcaption></figure><p>Note that the .circleci, in this example, I will use CircleCI as the CI build service, there are many different CI services you could use such as GitLab CI, Travis CI, etc. And start to implement the major feature.</p><h3>Step 1 — The Dockerfile and docker-compose</h3><p>We could bundle all the native modules and npm modules into the docker image, in production server only pull it and use it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/1*fiyKwEhV3ElorASGQ1veZw.png" /><figcaption>The example Dockerfile</figcaption></figure><p>For the docker-compose.yml use on local development, we could write it like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/1*1F2KYqel8fmhwwFXgNbbqQ.png" /><figcaption>The example docker-compose.yml</figcaption></figure><p>The container image is built from a local Dockerfile, but how about in production? I will show you the “magic” in the next few steps later.</p><h3>Step 2 — package.json scripts</h3><p>You may write the script directly on the CI settings file or in the terminal, it may better to write it on package.json for reuse more easily.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/201/1*0ccZc5xviDzPaM3S2BPmMQ.png" /><figcaption>Example scripts block in package.json</figcaption></figure><p>Simple build is transpile to JavaScript and start is to start the application.</p><h3>Step 3 — Writing the CircleCI config file</h3><p>In CircleCI, the config file will be located on &lt;your_project_root&gt;/.circleci/config.yml, the CI jobs will be to build the docker image, push to the Docker hub, build and archive the project files, push to the GitHub releases page. I will show you the config part by part (since it is very long).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*gK08KXWHUP2tOKzX-sCmtg.png" /><figcaption>The example CircleCI config file environment and executors</figcaption></figure><p>The environment block stores the environment variable that will use on the below jobs. The executors block is the definition for the jobs. In this case, I will push the Docker image to my docker hub repository.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/375/1*p0pxnArotfozvENf0MMqhQ.png" /><figcaption>The example CircleCI config file workflows</figcaption></figure><p>The jobs will only execute when there is a new tag with /^\d+\/\d+\.\d+$/ (e.g. 0.0.1), and parallelly run the Docker build and project build jobs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*cjNKCZPAtKyd0cXPmTbOqg.png" /><figcaption>The example CircleCI config file build Docker jobs</figcaption></figure><p>build-docker job is to build the Docker image from Dockerfile. After that, persist_to_workspace share the build image for the next job. publish-docker-latest is push the build image to Docker Hub. $DOCKERHUB_PASS and $DOCKERHUB_USERNAME is your Docker Hub login password and username, you could set it on your CircleCI organization setting → Contexts for this config read later. <a href="https://circleci.com/docs/2.0/env-vars/">https://circleci.com/docs/2.0/env-vars/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3e57RbqC6UJt-93AJdF9BA.png" /><figcaption>The example CircleCI config file build project file jobs</figcaption></figure><p>build is build the project files and archive them. The $GITHUB_TOKEN is your GitHub personal token <a href="https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token">https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token</a>, could also set it like the above Contexts. You may notice there is a create-prod-docker-compose.sh, it is “magic” I have mentioned on Step 1, and now we are creating another docker-compose file for production. The script is like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/407/1*lBInEsuOYQ3d9e3MfkC0sg.png" /><figcaption>The create-prod-docker-compose.sh file</figcaption></figure><p>$IMAGE_NAME and $CIRCLE_TAG (git tag) will be available in the CI environment, in this case, will be like cocoacaa/sample-ci-release-build:0.0.1.</p><h3>Step 4 — Push to remote Git server</h3><p>In this example, I will push the source to GitHub, and link to CircleCI.</p><h3>Step 5 — Setup CI project</h3><p>Go to CircleCI projects dashboard and click the Set Up Project button where the project you just pushed to GitHub. After clicking the button, click Use Existing Config since we have this config file already in the sources.</p><h3>Step 6 — Push the git tag and wait for the build</h3><p>Now we add a tag (e.g. 0.0.1) to the main branch and push it. Go back to the CircleCI pipeline dashboard and you will see the jobs are starting.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*bKWw104BPuS_ccquC3QYYg.png" /><figcaption>CicleCI building the project</figcaption></figure><h3>Step 7 — Check the build files</h3><p>After the builds are done, go to Docker Hub, there is a 0.0.1, and the latest tag will be created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/340/1*jeS07cQNCFjF208Z6e_DGg.png" /><figcaption>Docker Hub tags</figcaption></figure><p>Also, on your GitHub repository release page and you will find the build archive under assets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/198/1*2DFIKXkah8cUQ7BLctZwOw.png" /><figcaption>The build file in the GitHub release page</figcaption></figure><p>Finally, you could download the artifact.tar.gz, unarchive it and try to run by using docker-compose up -d command in your server.</p><p>That’s all! And every time code changes and commits to git with the tag, the CI will automate build your project files. Deploy to your server only need to download the release archive file to your server, unarchive it and start it immediately. d(`･∀･)b</p><h3>References</h3><ul><li><a href="https://circleci.com/docs/2.0/building-docker-images/">Running Docker Commands</a></li><li><a href="https://circleci.com/blog/publishing-to-github-releases-via-circleci/">Automate GitHub Releases with CircleCI</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2956ebfa4f6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Raspberry Pi 4 Ubuntu 安裝 Docker]]></title>
            <link>https://cocoacaa.medium.com/raspberry-pi-4-ubuntu-%E5%AE%89%E8%A3%9D-docker-6d8f58f3ff40?source=rss-9a5ac175be66------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d8f58f3ff40</guid>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[樹莓派]]></category>
            <category><![CDATA[ubuntu]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[infrastructure]]></category>
            <dc:creator><![CDATA[CocoaCaa]]></dc:creator>
            <pubDate>Wed, 27 Nov 2019 16:31:49 GMT</pubDate>
            <atom:updated>2019-11-27T16:43:44.837Z</atom:updated>
            <content:encoded><![CDATA[<h3>Raspberry Pi 4 安裝 Docker</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rJWDhg4U9N95_vTNHa2agQ.jpeg" /></figure><p>繼上一次「<a href="https://medium.com/@onikur/raspberry-pi-4-%E9%81%8B%E8%A1%8C-64bit-os-%E5%A4%96%E7%BD%AE%E7%A1%AC%E7%A2%9F-4df2c70d4478">Raspberry Pi 4 運行 64bit OS+ 外置硬碟</a>」之後，開始試試安裝其他東西。</p><p>每次設定伺服器也習慣了用 Docker，安裝不同的系統服務。這可以不用在原本的系統安裝很多依賴包，當需要升級／刪除就會遇上不同奇怪的情況了（例如幾個軟體也是用同一個依賴包的話…… (╯‵□′)╯︵┴─┴），Docker 就可以輕而易舉完成這件事了。</p><p>不過跟着<a href="https://docs.docker.com/install/linux/docker-ce/ubuntu/">官方網站的安裝方法</a>就會發現，原來 Docker 的 <a href="https://download.docker.com/linux/ubuntu/dists/">PPA</a> (Personal Package Archives) 還沒支持 Ubuntu 19.10 (eoan)，而且 docker-compose 用<a href="https://docs.docker.com/compose/install/">官方網站</a>的下載地址也只是回 “404 Not Found”（沒有為 arm64 的版本）</p><p>經過上網找找資料後，終於發現要如何安裝了，部驟如下：</p><h3>載入 PPA</h3><p><a href="https://docs.docker.com/install/linux/docker-ce/ubuntu/">官方網站</a>的 PPA 是沒錯的，只是 Ubuntu 版本問題，只要用 “disco 19.04” 也可以喔～（都是一樣 19.xx 通常不會太大分別，當然如果有一樣的版本就最好了）</p><pre>sudo add-apt-repository \<br>   &quot;deb [arch=arm64] <a href="https://download.docker.com/linux/ubuntu">https://download.docker.com/linux/ubuntu</a> \<br>   disco \<br>   stable&quot;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*86z6RMmcFOSKwdGHYlk9EA.png" /><figcaption>載入 Ubuntu PPA</figcaption></figure><h3>安裝 Docker</h3><p>和官方網站的方法一樣，只要鍵入 sudo apt install docker-ce docker-ce-cli containerd.io 就行。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*bofb2MMVPCWPcJYc9xT-VQ.png" /><figcaption>使用 Apt 安裝 Docker</figcaption></figure><h3>安裝 Python 3+</h3><p>到這裏可能好奇一問，為什麼要安裝 Python？原因是 docker-compose 其實是用 Python 來運行，而且要是版本 3（2.7 或以下會有很多奇怪錯誤彈出來）所以再用 Apt 安裝 sudo apt install python3 python3-pip。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*6axiTjMxiAz_nWtxKXz2GA.png" /><figcaption>使用 Apt 安裝 Python</figcaption></figure><h3>安裝 docker-compose</h3><p>官方網站也有用 pip 的方法，不過要注意的是，我們是用 Python 3，所以是要用 pip3，是 <strong>pip3</strong> 喔（如果你的系統安裝了 Python 2 和 3 的話，pip 會是用版本 2）鍵入 sudo pip3 install docker-compose。</p><p>到這裏基本在 Raspberry Pi 4 的 Docker 設定也完成了。</p><p>Happy Docker ε٩(๑&gt; ₃ &lt;)۶з</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d8f58f3ff40" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Raspberry Pi 4 運行 64bit OS+ 外置硬碟]]></title>
            <link>https://cocoacaa.medium.com/raspberry-pi-4-%E9%81%8B%E8%A1%8C-64bit-os-%E5%A4%96%E7%BD%AE%E7%A1%AC%E7%A2%9F-4df2c70d4478?source=rss-9a5ac175be66------2</link>
            <guid isPermaLink="false">https://medium.com/p/4df2c70d4478</guid>
            <category><![CDATA[infrastructure]]></category>
            <category><![CDATA[樹莓派]]></category>
            <category><![CDATA[custom]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[ubuntu]]></category>
            <dc:creator><![CDATA[CocoaCaa]]></dc:creator>
            <pubDate>Sat, 23 Nov 2019 17:21:54 GMT</pubDate>
            <atom:updated>2019-12-30T08:19:52.261Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xSt0lQI1f8KVWJ0NPwwr4A.jpeg" /></figure><p>我上一部已經是國產紅色版的 Raspberry Pi，當時只有 512MB 的記憶體+ 1 核心 CPU。直到今天，Raspberry Pi 4 具備最高 4GB 的記憶體 + 4 核心 CPU，還有 USB 3.0，卡片大小的電腦已經有這樣的表現 (OAO)。作為一部個人電腦、一般網頁伺服器可說是綽綽有餘呢～</p><p>就是這樣，近日買了一部 Raspberry Pi 4 Modal B - 4GB 試試它的功能，設置為一部伺服器。</p><h3>為何安裝 Ubuntu 而不是 Raspbian？</h3><p>如果大家之前也有用 Raspberry Pi，可能會安裝官方提供的 Raspbian OS。不過 Raspbian OS 是 32bit。如果要運行 64 bit 的程式需要安裝 64bit 的 Kernal，但始終不是真正 64bit OS。而 Ubuntu 有 Raspberry Pi 4 而做的版本，亦是因為 Ubuntu 是用 Debian 作為基本，所以操作上和 Raspbian 分別不會太大。</p><h3>使用外置硬碟的好處？</h3><p>Raspberry Pi 使用 SD卡便可以運行，不過讀出及寫入也太慢了（就算是 Class 10 都只有 10MB/s，課金再高一級也太貴了吧……）現在的 Raspberry Pi 4 具備 USB3.0，連接外置硬碟／固態硬碟也比較經濟實惠而更高的效率，任何需要處理檔案的程式也相對上運行速度更快呢。</p><h3>安裝</h3><h4>開始前的準備</h4><p>開始前需要以下的東西：</p><ul><li>Raspberry Pi 4 (1GB / 2GB / 4GB)<br>官方介紹及購買頁面 <a href="https://www.raspberrypi.org/products/raspberry-pi-4-model-b/">https://www.raspberrypi.org/products/raspberry-pi-4-model-b/</a></li><li>USB 外置硬碟／固態硬碟（建議 ≥60GB）</li><li>microSD 卡（建議 ≥4GB）</li><li>網路線（LAN 線）</li><li>USB Type-C 變壓器（火牛）</li></ul><h4>下載 Ubuntu Server</h4><p>可以經 Raspberry Pi 第三方下載頁 <a href="https://www.raspberrypi.org/downloads/">https://www.raspberrypi.org/downloads/</a>，或直接前往 <a href="https://ubuntu.com/download/raspberry-pi">https://ubuntu.com/download/raspberry-pi</a>。大約 630MB 的 .img.xz 映像檔案。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9ntyh60JwBFer9MFB31CKg.png" /><figcaption>Ubuntu Server Raspberry Pi 3/4 映像檔案下載頁面</figcaption></figure><h4>載入 Ubuntu Server 到 SD 卡</h4><p>Raspberry Pi 官方網頁建議用 <a href="https://www.balena.io/etcher/">balenaEtcher</a> 將映像檔案載入到 SD 卡裏，方便又快捷。那我們使用這個程式吧～（當然也可以用 dd 指令進行這個程序）</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*4eMcxLRWbEcQ2EH2UdDRlQ.png" /><figcaption>balenaEtcher 載入映像檔案界面</figcaption></figure><h4>SSH 設定、開機</h4><p>設定 SSH 為方便不用額外的顯示器和鍵盤，只要在 /system-boot 開一個新檔案名為 ssh（沒有副檔名）就可以 SSH了。然後將 SD 卡放入 Raspberry Pi，插入電源。</p><p>等一下後，開啟你電腦的終端機（在 Windows 可用 PowerShell）鍵入<br>ssh ubuntu@ubuntu<br>密碼是 “ubuntu”（全部都是 ubuntu XD）第一次連入需要更改密碼，之後再連入便可以真真正正進入 Raspberry Pi×Ubuntu 的世界了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*sTnUGFbn00l7jla-h2bZaw.png" /><figcaption>SSH 連入 Raspberry Pi</figcaption></figure><h4>設定外置硬碟分割區</h4><p>分割硬碟有很多不同的工具（例如：fdisk），而我選擇了使用 cfdisk（有互動界面的硬碟分割程式，本來就已經安裝了在 Ubuntu Server）如果只有一硬碟將會在 /dev/sda，鍵入 cfdisk /dev/sda，設定如下：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*SU88DQAuFho7tYwFgG5vxg.png" /><figcaption>cfdisk 分割硬碟界面</figcaption></figure><p>我使用了大約 60GB 的硬碟，54GB 將會是系統的 “/”，而分配了 1.9 GB 作為 swap 空間（避免記憶體用滿後當機）</p><h4>格式化硬碟</h4><p>分割後就需要格式化才能使用，一般都是使用 ext4 格式，鍵入sudo mkfs.ext4 /dev/sdx1再將 /dev/sda2 可以用作 swap，鍵入sudo mkswap /dev/sdx2</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*k_qwYKOZxOUTpRf2aTn-yw.png" /><figcaption>格式化硬碟指令</figcaption></figure><h4>複製系統檔案到外置硬碟</h4><p>使用外置硬碟作為系統的主要存儲，將現正在 SD 卡的檔案複製到外置硬碟，先新增資料夾可用來掛載 /dev/sda1<br>sudo mkdir /mnt/usb<br>然後掛載<br>sudo mount /dev/sdx1 /mnt/usb<br>再將系統檔案複製到外置硬碟<br>sudo rsync -avx / /mnt/usb<br>（這程序需要一些時間去完成， 煮沸開水沖一杯茶就差不多了 w）</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*Slpz7R46VQ-UO10hWpZdlA.png" /><figcaption>複製檔案使用 rsync</figcaption></figure><h4>設定系統 swap</h4><p>複製後，現在會有一個檔案 “fstab” 在 /mnt/usb/etc，這樣我們可以設定下一次開機也會自動掛載 swap 了。在 /mnt/usb/etc/fstab 內加入<br>UUID=&lt;UUID&gt; none swap defaults 0 0<br>查看 &lt;UUID&gt; 可以使用指令 sudo blkid /dev/sda2，將 UUID= 後的字放入 &lt;UUID&gt; 便可（當然也可以直接用 /dev/sda2）</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*ImSKqCP1lMGLqszWKIbQMg.png" /><figcaption>設定掛載 swap</figcaption></figure><h4>修改開機指令</h4><p>這是最後的步驟了，在 /boot/firmware/nobtcmd.txt，更改 root=LABEL=writable 到 root=UUID=&lt;UUID&gt;，一樣可以使用指令 sudo blkid /dev/sda1 來查看 &lt;UUID&gt;（同上也可以直接用 /dev/sda1）</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*7mFGqzzcehBsIa0keutFTw.png" /><figcaption>修改開機指令在 /boot/firmware/nobtcmd.txt</figcaption></figure><p>這裏會發生什麼事？開機時將會用 SD 卡，然後將系統掛載到外置硬碟。所以 <strong>SD 卡和外置硬碟是需要同時插在 Raspberry Pi 上</strong>喔。否則不能開機呢～</p><p>最後重新開機，再 SSH 連入，如果用 df -H 看到 /dev/sda1 掛載在 “/” ，用 htop 看到有 swap，你的 Raspberry Pi 就正在使用 64bit 的 OS+ 外置硬碟了 d(･∀･)b</p><h3>參考</h3><ul><li><a href="https://www.tomshardware.com/news/boot-raspberry-pi-from-usb,39782.html">https://www.tomshardware.com/news/boot-raspberry-pi-from-usb,39782.html</a></li><li><a href="https://bugs.launchpad.net/ubuntu/+source/linux-raspi2/+bug/1848790">https://bugs.launchpad.net/ubuntu/+source/linux-raspi2/+bug/1848790</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4df2c70d4478" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>