<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>linux 彙整 - 泰克哪裡去</title>
	<atom:link href="https://tech.uccu.website/tag/linux/feed" rel="self" type="application/rss+xml" />
	<link>https://tech.uccu.website/tag/linux</link>
	<description>一個科技相關的隨手記錄網站</description>
	<lastBuildDate>Sun, 09 Oct 2022 14:59:11 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
<site xmlns="com-wordpress:feed-additions:1">119574712</site>	<item>
		<title>【2022鐵人賽】使用Task與CLI的抉擇</title>
		<link>https://tech.uccu.website/2022ironman-day22-task-or-cli.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=2022ironman-day22-task-or-cli</link>
					<comments>https://tech.uccu.website/2022ironman-day22-task-or-cli.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Sat, 08 Oct 2022 14:37:38 +0000</pubDate>
				<category><![CDATA[2022鐵人賽]]></category>
		<category><![CDATA[Azure DevOps]]></category>
		<category><![CDATA[Docker]]></category>
		<category><![CDATA[iThome鐵人賽]]></category>
		<category><![CDATA[2022ironman]]></category>
		<category><![CDATA[azure devops]]></category>
		<category><![CDATA[container]]></category>
		<category><![CDATA[linux]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=2055</guid>

					<description><![CDATA[<p>透過CLI就是組Scripts來達成目的，所以彈性相對高很多。除了可以Build Source Code之外，要Pack Nuget Package也是同樣透過Sdk CLI就可以達成。但是如果是用Task的話，可能就會需要分成不同的Task。</p>
<p>這篇文章 <a href="https://tech.uccu.website/2022ironman-day22-task-or-cli.html">【2022鐵人賽】使用Task與CLI的抉擇</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>這篇要來聊聊Azure DevOps Pipeline的Task與CLI的抉擇，為什麼會這樣說呢？</p>



<p>因為其實在Azure DevOps Pipeline中已經有提供許多不錯的Task設計，尤其.Net的程式在Azure DevOps Pipeline中有各式各樣對應的Task可以使用，像去年的文章「<a href="https://tech.uccu.website/2021ironman-day7-first-pipeline-template-and-editor.html">CI/CD從這裡：設定第一個Pipeline(範本與編輯介面介紹)</a>」裡面就是使用內建的VSBuild Task來編譯.Net的程式碼，但是今年卻在「<a href="https://tech.uccu.website/2022ironman-day8-ci-pipeline-buildcode.html" target="_blank" rel="noreferrer noopener">基本版-建立CI Pipeline(1)</a>」文章內一開始就使用.Net Sdk的Container來編譯.Net的程式碼…</p>



<p>除了今年的文章屬於進階應用之外，還有其它幾個不同的因素考量：</p>



<ul class="wp-block-list"><li>Task要透過GUI才能快速方便知道有什麼參數可以選擇與設定</li><li>不同功能可能要選用不同task</li><li>可以達到相同功能的Task可能不只一個</li><li>想要測試看看執行結果如何無法在Pipeline以外的環境執行</li><li>用Container或直接用Sdk CLI可以在本機執行看看結果</li><li>新功能或指令在Task可能沒有設計或支援</li><li>如果是自管的Agent只要裝了Docker就可以</li></ul>



<p>因為透過CLI就是組Scripts來達成目的，所以彈性相對高很多。例如.Net Core Sdk CLI除了可以Build Source Code之外，要Pack Nuget Package也是同樣透過Sdk CLI就可以達成，只是指令的組成不太一樣。但是如果是用Task的話，就會需要分成Nuget Task與.Net Core Task，或是pack也用.Net Core Task而不是用Nuget Task。</p>



<p>另外還有像是我們使用Azure DevOps Artifacts功能來存放私有的Nuget Packages，不過要Push上去是需要身份驗證授權的，在Task的選擇部份就有Nuget authenticate Task(搭配設定Service Connection)，或是有個nuget.config檔案，裡面寫入PAT然後放在特定的位置來自動解決身份驗證的問題。</p>



<p>所以原本的dotnet-build-in-linux-container.yaml就可以重新做一份step template：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-yaml"># steps/dotnet-sdk-in-linux-container.yaml

parameters:
  - name: dotnetSdkImgRepository
    type: string
    default: mcr.microsoft.com/dotnet/sdk:6.0-alpine
  - name: srcPath
    type: string
  - name: outputPath
    type: string
    default: $(Build.ArtifactStagingDirectory)/outputFiles/
  - name: slnOrCsprojName
    type: string
    default: Pipeline.sln
  - name: dotnetCommand
    type: string
  - name: dotnetCommandArgs
    type: string

steps:
  - script: |    
      echo &quot;Dotnet sdk in linux container template&quot;
      echo &quot;${{ convertToJson(parameters) }}&quot;
    displayName: Print template parameters
  - task: Bash@3
    displayName: Run dotnet sdk command in container
    inputs:
      targetType: &#039;inline&#039;
      script: |
        if [ -d ${{ parameters.outputPath }} ]; then echo &quot;${{ parameters.outputPath }} exist&quot;; else mkdir -p ${{ parameters.outputPath }}; fi
        export UID=$(id -u)
        export GID=$(id -g)
        docker run --user $UID:$GID --rm \
        -v ${{ parameters.srcPath }}:/tmp/source \
        -v ${{ parameters.outputPath }}:/tmp/publish \
        -e DOTNET_CLI_HOME=/tmp/.dotnet \
        ${{ parameters.dotnetSdkImgRepository }} \
        dotnet ${{ parameters.dotnetCommand }} /tmp/source/${{ parameters.slnOrCsprojName }} \
        -o /tmp/publish ${{ parameters.dotnetCommandArgs }} \
  - task: ArchiveFiles@2
    displayName: 壓縮成zip
    inputs:
      rootFolderOrFile: $(Build.BinariesDirectory)
      includeRootFolder: false
      archiveType: &#039;zip&#039;
      archiveFile: &#039;$(Build.ArtifactStagingDirectory)/zipFiles/buildResult.zip&#039;
      replaceExistingArchive: true</code></pre>



<p>這樣就可以用同樣的一個範本搞定Build Source Code、Pack Nuget Package之類的事情，對應在jobs/buildCode.yaml就是把引用的template檔案改成新的，然後加上dotnetCommand、dotnetCommandArgs參數的設定就行了。</p>



<pre class="wp-block-prismatic-blocks"><code class="language-yaml">      - template: ../steps/dotnet-sdk-in-linux-container.yaml
        parameters:
          srcPath: ${{ parameters.sourcePath }}
          slnOrCsprojName: ${{ parameters.slnOrCsprojName }}
          dotnetCommand: publish
          dotnetCommandArgs: &#039;-c ${{ parameters.buildConfiguration }}&#039;</code></pre>
<p>這篇文章 <a href="https://tech.uccu.website/2022ironman-day22-task-or-cli.html">【2022鐵人賽】使用Task與CLI的抉擇</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/2022ironman-day22-task-or-cli.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2055</post-id>	</item>
		<item>
		<title>解決.sh檔案在Container內執行時的換行字元(CRLF/LF)問題</title>
		<link>https://tech.uccu.website/convert-shell-file-end-of-line-sequence-at-dockerfile.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=convert-shell-file-end-of-line-sequence-at-dockerfile</link>
					<comments>https://tech.uccu.website/convert-shell-file-end-of-line-sequence-at-dockerfile.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Tue, 24 Nov 2020 09:47:26 +0000</pubDate>
				<category><![CDATA[Docker]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[crlf]]></category>
		<category><![CDATA[dockerfile]]></category>
		<category><![CDATA[dos2unix]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[shell]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=158</guid>

					<description><![CDATA[<p>如果有在使用Docker並且是建置Linux環境的Image，搭配自行撰寫的shell檔案，有時候會因為版本控 ... <a title="解決.sh檔案在Container內執行時的換行字元(CRLF/LF)問題" class="read-more" href="https://tech.uccu.website/convert-shell-file-end-of-line-sequence-at-dockerfile.html" aria-label="Read more about 解決.sh檔案在Container內執行時的換行字元(CRLF/LF)問題">閱讀全文</a></p>
<p>這篇文章 <a href="https://tech.uccu.website/convert-shell-file-end-of-line-sequence-at-dockerfile.html">解決.sh檔案在Container內執行時的換行字元(CRLF/LF)問題</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>如果有在使用Docker並且是建置Linux環境的Image，搭配自行撰寫的shell檔案，有時候會因為版本控管、編輯器(IDE)等雜七雜八的因素，導致在Dockerfile中複製到Image的時候看起來雖然沒問題，但是在Run Container的時候出現因為換行字元(CRLF/LF)導致的錯誤時，除了可以回頭去調整或修改shell檔案，將換行字元從CRLF轉換成LF之外，還有一個更方便的方法，那就是在shell檔案複製到Docker Image的時候再執行轉換的動作。</p>



<p>起先我在寫Shell檔案的時候真的是碰到了許多次因為CRLF / LF換行字元的問題，早期都是回頭去修改原始檔案之後再重新Build Image，久了之後實在是覺得很麻煩，某天忽然想到直接在Dockerfile中安裝<em><strong>dos2unix</strong></em>這個套件，在shell檔案複製之後再執行它來將shell檔案的換行字元的問題解決，從此之後就沒有再為了Windows / Unix-like不同環境的換行字元問題而煩惱過了，因此特別用這篇文章簡短記錄一下，也許有人也為了這個問題困擇了一段時間呢！</p>



<h2 class="wp-block-heading">Dockerfile內容參考範例</h2>



<pre class="wp-block-prismatic-blocks"><code class="language-docker">RUN apt-get install dos2unix;
COPY my-init.sh .
RUN dos2unix my-init.sh
CMD [&quot;bash&quot;, &quot;my-init.sh&quot;]</code></pre>
<p>這篇文章 <a href="https://tech.uccu.website/convert-shell-file-end-of-line-sequence-at-dockerfile.html">解決.sh檔案在Container內執行時的換行字元(CRLF/LF)問題</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/convert-shell-file-end-of-line-sequence-at-dockerfile.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">158</post-id>	</item>
		<item>
		<title>Linux container中修改json檔案設定值的方法</title>
		<link>https://tech.uccu.website/how-to-modify-json-file-in-linux-container.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-modify-json-file-in-linux-container</link>
					<comments>https://tech.uccu.website/how-to-modify-json-file-in-linux-container.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Tue, 24 Nov 2020 09:10:21 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[appsettings.json]]></category>
		<category><![CDATA[container]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[linux]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=151</guid>

					<description><![CDATA[<p>現在許多的程式設計都使用了json格式的檔案作為設定檔的儲存格式，可以跨平台在Linux環境執行的.Net C ... <a title="Linux container中修改json檔案設定值的方法" class="read-more" href="https://tech.uccu.website/how-to-modify-json-file-in-linux-container.html" aria-label="Read more about Linux container中修改json檔案設定值的方法">閱讀全文</a></p>
<p>這篇文章 <a href="https://tech.uccu.website/how-to-modify-json-file-in-linux-container.html">Linux container中修改json檔案設定值的方法</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>現在許多的程式設計都使用了json格式的檔案作為設定檔的儲存格式，可以跨平台在Linux環境執行的.Net Core也不例外，伴隨著appsettings.json檔案中許多的設定，不管是預設的設定，或是不同開發需求自行加入進去的設定值，全部都是json格式。</p>



<p>為了在docker啟動container的時候可以根據繫結的環境參數(environment, -e)值做出相對應的修改，因此需要有個方便的作法可以在container執行的時候動態修改json檔案。</p>



<p>這一篇文章就來介紹如何在Linux中透過jq套件修改json文件。</p>



<span id="more-151"></span>



<h2 class="wp-block-heading">先決條件</h2>



<p>Container中需要先安裝jq套件，所以必須在Dockerfile中先透過<em>apt-get install -y jq</em>指令(Ubuntu/Debian)安裝jq套件，以便Docker Image中有jq套件可以使用。</p>



<h2 class="wp-block-heading">使用方式</h2>



<p>假設在Dockerfile中定義了<em><strong>ENV&nbsp;LOG_LEVEL=Error</strong></em>，並且appsettings.json檔案中有段Serilog的設定如下：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-json">{
    &quot;Serilog&quot;: {
        &quot;MinimumLevel&quot;: &quot;Debug&quot;,
        &quot;WriteTo&quot;: [
            {
                &quot;Name&quot;: &quot;File&quot;,
                &quot;Args&quot;: {
                    &quot;path&quot;: &quot;MyApp.log&quot;,
                    &quot;outputTemplate&quot;: &quot;{Timestamp:yyyy-MM-dd HH:mm:ss} RequestID:{RequestID} User:{UserID} {RequestPath} {SourceContext} [{Level:u3}] {Message:lj}{NewLine}{Exception}&quot;,
                    &quot;rollingInterval&quot;: &quot;Day&quot;
                }
            }
        ],
        &quot;Enrich&quot;: [ &quot;FromLogContext&quot; ]
    }
}</code></pre>



<p>可以看到在Serilog中有個MinimumLevel屬性設定的是Debug，這個設定會記錄大量的Log記錄，但是有時候在執行container的時候並不想要記錄這麼詳細的資訊，以降低檔案大小，減少儲存空間的耗用時，有個LOG_LEVEL的ENV設定就可以方便的調整設定值。</p>



<p>現在假設有個.Net Core的程式叫MyApp，Build完之後有個MyApp.dll檔案，在Dockerfile中設定了ENV和複製相關的執行檔案之外，還需要一個.sh檔案(Linux中的Shell檔，相當於Windows的.cmd或.bat)，假設這個檔案叫做app-init.sh。</p>



<p>接下來在app-init.sh檔案中要做下面這幾件事：</p>



<ol class="wp-block-list"><li>判斷container是不是第一次執行，以避免每次都進行修改appsettings.json的動作。</li><li>透過jq套件將appsettings.json的內容讀入記憶體。</li><li>找到MinimumLevel屬性，將值修改為LOG_LEVEL這個ENV所設定的值。</li><li>將jq套件修改後的內容儲存為appsettings.temp.json。</li><li>將原本的appsettings.json改名為appsettings.ori.json。</li><li>將前面的appsettings.temp.json改名為appsettings.json。</li><li>產生一個用來判斷是否執行過app-init.sh的檔案，例如：inited。</li><li>啟動MyApp。</li></ol>



<p>假設Dockerfile中設定的WORKDIR是/app，第7點的檔案是放在/app/inited，那麼app-init.sh的程式碼內容大概會是下面這個樣子：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-bash">if [ -f &quot;/app/inited&quot; ]; then
    echo &quot;Container inited, skip this shell.&quot;
else
    cat appsettings.json #這行只是先輸出原本的設定值，用來查看用的
    jq &#039;.Serilog.MinimumLevel=env.LOG_LEVEL&#039; appsettings.json &gt; appsettings.temp.json

    cat appsettings.temp.json #查看儲存的檔案內容

    mv appsettings.json appsettings.ori.json #第5點，將原本的設定檔改名
    mv appsettings.temp.json appsettings.json #第6點，將jq儲存的暫存檔改為正式檔名

    echo &quot;Container inited.&quot; &gt; /app/inited #第7點，產生判斷是否init過的檔案
fi

dotnet MyApp.dll #第8點，啟動MyApp</code></pre>



<p>透過上面的app-init.sh檔案，在Container第一次執行的時候就會根據設定的內容修改appsettings.json檔案，產生的inited檔案可以避免container restart之後重複執行init的動作。</p>



<p>透過這樣的技巧，也可以修改appsettings.json中的資料庫連線字串，如果是像Serilog底下的WriteTo屬性是陣列(集合)型式的，則是用[0]、[1]這樣的方式設定，範例如下：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-bash">jq &#039;.Serilog.MinimumLevel=env.LOG_LEVEL&#039; appsettings.json | \
jq &#039;.Serilog.WriteTo[0].Args.rollingInterval=env.LOG_ROLLING_INTERVAL&#039;  &gt; appsettings.temp.json</code></pre>
<p>這篇文章 <a href="https://tech.uccu.website/how-to-modify-json-file-in-linux-container.html">Linux container中修改json檔案設定值的方法</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/how-to-modify-json-file-in-linux-container.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">151</post-id>	</item>
		<item>
		<title>Linux(Ubuntu)磁碟分割</title>
		<link>https://tech.uccu.website/linux-format-disk.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linux-format-disk</link>
					<comments>https://tech.uccu.website/linux-format-disk.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Wed, 11 Nov 2020 10:16:52 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[ubuntu]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=136</guid>

					<description><![CDATA[<p>在雲端上建立的Linux VM預設的空間都不大，或是有些其它需求另外增加一個資料磁碟的情況，做了幾次新增磁碟之 ... <a title="Linux(Ubuntu)磁碟分割" class="read-more" href="https://tech.uccu.website/linux-format-disk.html" aria-label="Read more about Linux(Ubuntu)磁碟分割">閱讀全文</a></p>
<p>這篇文章 <a href="https://tech.uccu.website/linux-format-disk.html">Linux(Ubuntu)磁碟分割</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>在雲端上建立的Linux VM預設的空間都不大，或是有些其它需求另外增加一個資料磁碟的情況，做了幾次新增磁碟之後的磁碟分割、格式化等動作，但是因為脫離GUI的環境要記下龐大的Linux指令是很困難的(年紀大了)，所以趁這次又要做一次的時候趕快把過程記錄下來。</p>



<p>一開始可以使用「sudo fdisk -l」的方式來列出目前系統中的磁碟(Disk)，看看有哪些磁碟(Disk)與分割區(Partition)。</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="511" height="485" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/0d3c8939-image.png?resize=511%2C485&#038;ssl=1" alt="" class="wp-image-137" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/0d3c8939-image.png?w=511&amp;ssl=1 511w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/0d3c8939-image.png?resize=300%2C285&amp;ssl=1 300w" sizes="(max-width: 511px) 100vw, 511px" /><figcaption>系統中的磁碟資訊</figcaption></figure></div>



<p>從上圖看到系統中有三個不同的Disk，而我新增加的是最下面的/dev/sdc 250G大小的Disk，它就是我要下手的目標。</p>



<p>接下來輸入「sudo fdisk /dev/sdc」(後面沒數字)，執行fdisk進入操作模式。</p>



<p>一開始會出現上半部的訊息，如果常操作fdisk工具記得各種選項是做什麼用的，可以直接輸入選項的代號，不然可以輸入m來查看(如下半部)：</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" decoding="async" width="551" height="719" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/ea018de3-image.png?resize=551%2C719&#038;ssl=1" alt="" class="wp-image-138" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/ea018de3-image.png?w=551&amp;ssl=1 551w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/ea018de3-image.png?resize=230%2C300&amp;ssl=1 230w" sizes="(max-width: 551px) 100vw, 551px" /><figcaption>fdisk選項</figcaption></figure></div>



<p>因為是一個新的完全空白的磁碟，所以選擇n來建立新的Partition，接下來會詢問是要建立主要分割區還是延伸分割區、分割區號碼、開始與結束磁區位置(就是要割多大)等，如果是使用預設值的話也可以直接按Enter不用輸入。</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" decoding="async" width="629" height="157" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/ce748140-image.png?resize=629%2C157&#038;ssl=1" alt="" class="wp-image-140" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/ce748140-image.png?w=629&amp;ssl=1 629w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/ce748140-image.png?resize=300%2C75&amp;ssl=1 300w" sizes="(max-width: 629px) 100vw, 629px" /><figcaption>建立一個新的分割區</figcaption></figure></div>



<p>完成上面的步驟之後，這些改變只是暫時存在記憶體，再輸入w選項，將變更寫入磁碟之後離開。</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="352" height="68" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/a6f5c402-image.png?resize=352%2C68&#038;ssl=1" alt="" class="wp-image-143" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/a6f5c402-image.png?w=352&amp;ssl=1 352w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/a6f5c402-image.png?resize=300%2C58&amp;ssl=1 300w" sizes="auto, (max-width: 352px) 100vw, 352px" /><figcaption>將fdisk的變更寫入磁碟後離開</figcaption></figure></div>



<p>再輸入一次「sudo fdisk -l」指令重新查看列表，發現最下面多了/dev/sdc1的項目，這就是剛才分割的Parition了。</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="511" height="561" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/23d11dae-image.png?resize=511%2C561&#038;ssl=1" alt="" class="wp-image-145" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/23d11dae-image.png?w=511&amp;ssl=1 511w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/23d11dae-image.png?resize=273%2C300&amp;ssl=1 273w" sizes="auto, (max-width: 511px) 100vw, 511px" /></figure></div>



<p>fdisk是用來做磁碟分割使用，分割完之後還要format格式化才能儲存檔案，在Linux環境中使用mkfs(<strong><span style="color:#ff0000" class="has-inline-color">M</span></strong>a<strong><span style="color:#ff0000" class="has-inline-color">k</span></strong>e a Linux <strong><span style="color:#ff0000" class="has-inline-color">f</span></strong>ilesy<strong><span style="color:#ff0000" class="has-inline-color">s</span></strong>tem的縮寫)，選項的說明可以輸入「sudo mkfs &#8211;help」來查看。</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="580" height="243" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/f074fc44-image.png?resize=580%2C243&#038;ssl=1" alt="" class="wp-image-144" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/f074fc44-image.png?w=580&amp;ssl=1 580w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/f074fc44-image.png?resize=300%2C126&amp;ssl=1 300w" sizes="auto, (max-width: 580px) 100vw, 580px" /><figcaption>mkfs的選項</figcaption></figure></div>



<p>我打算將它格式化為xfs格式，所以就輸入「sudo mkfs -t xfs /dev/sdc1」(後面數字要正確)，系統使用預設的設定值格式化之後的資訊如下：</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="686" height="137" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/a4fe2eb2-image.png?resize=686%2C137&#038;ssl=1" alt="" class="wp-image-147" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/a4fe2eb2-image.png?w=686&amp;ssl=1 686w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/a4fe2eb2-image.png?resize=300%2C60&amp;ssl=1 300w" sizes="auto, (max-width: 686px) 100vw, 686px" /><figcaption>格式化後的資訊</figcaption></figure></div>



<p>格式化完成之後需要掛載起來，在Windows就是要給它個磁碟機代號(當然也可以掛在某個資料夾下)。</p>



<p>Windows做完格式化並設定完磁碟機代號後就可以使用了，就算重新開機還是會看得到新增的磁碟分割，但是Linux如果要開機時也會自動掛載起來的話，需要將相關資訊寫入/etc/fstab檔案中，在這之前會需要知道分割區的UUID，所以先輸入「sudo blkid」找到對應的UUID。</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="73" src="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/dcb8550d-image-1024x73.png?resize=1024%2C73&#038;ssl=1" alt="" class="wp-image-148" srcset="https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/dcb8550d-image.png?resize=1024%2C73&amp;ssl=1 1024w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/dcb8550d-image.png?resize=300%2C21&amp;ssl=1 300w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/dcb8550d-image.png?resize=768%2C55&amp;ssl=1 768w, https://i0.wp.com/storage.googleapis.com/stateless-tech-uccu-website/2020/11/dcb8550d-image.png?w=1107&amp;ssl=1 1107w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption>找到要掛載的分割區UUID</figcaption></figure>



<p>將UUID裡面的值複製下來之後，輸入「sudo nano /etc/fstab」(我這裡用nano編輯器來修改，也可以用別的)，內容會長得像下面的樣子，分別是Device, Mount point, FileSystem, Parameters, dump, fsck，反正不是很熟就照著第一列的格式輸入大概就行了。</p>



<pre class="wp-block-code"><code>#
# /etc/fstab
# Created by anaconda on Thu Sep  7 20:55:27 2017
#
# Accessible filesystems, by reference, are maintained under &#039;/dev/disk&#039;
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=3c11fba3-32c7-4d0c-b614-aad5630504eb /                       xfs     defaults        0 0
UUID=ca02dd2d-a91b-4fb1-b24b-0fb19c18b89d /boot                   xfs     defaults        0 0
</code></pre>



<p>檔案儲存之後，如果要掛載的位置(資料夾)不存在，需要先建立之後再掛載，輸入「sudo mkdir 掛載路徑」建立完<strong><span style="color:#ff0000" class="has-inline-color">空的資料夾</span></strong>之後，最後再輸入「sudo mount -a」將fstab裡面設定的項目都掛載起來。</p>



<h3 class="wp-block-heading">參考來源</h3>



<ul class="wp-block-list"><li><a href="http://linux.vbird.org/linux_basic/0230filesystem/0230filesystem-fc4.php" target="_blank" rel="noreferrer noopener nofollow">http://linux.vbird.org/linux_basic/0230filesystem/0230filesystem-fc4.php</a></li><li><a href="https://blog.gtwang.org/linux/linux-add-format-mount-harddisk/" target="_blank" rel="noreferrer noopener nofollow">https://blog.gtwang.org/linux/linux-add-format-mount-harddisk/</a></li></ul>



<p></p>
<p>這篇文章 <a href="https://tech.uccu.website/linux-format-disk.html">Linux(Ubuntu)磁碟分割</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/linux-format-disk.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">136</post-id>	</item>
	</channel>
</rss>
