<?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>parted | ばすにっきTips</title>
	<atom:link href="https://takabus.com/tips/tag/parted/feed/" rel="self" type="application/rss+xml" />
	<link>https://takabus.com/tips</link>
	<description>プログラミングやサーバー、日々のTipsをメモしています。</description>
	<lastBuildDate>Mon, 21 Feb 2022 05:39:32 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.3</generator>

<image>
	<url>https://takabus.com/tips/wp-content/uploads/2022/12/cropped-tips-1-32x32.png</url>
	<title>parted | ばすにっきTips</title>
	<link>https://takabus.com/tips</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【Linux】ハードディスク交換の全手順まとめ！パーティション設定からデータ移行・マウントまで</title>
		<link>https://takabus.com/tips/828/</link>
					<comments>https://takabus.com/tips/828/#respond</comments>
		
		<dc:creator><![CDATA[ばすにっきTips]]></dc:creator>
		<pubDate>Fri, 18 Feb 2022 08:10:23 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[parted]]></category>
		<guid isPermaLink="false">https://takabus.com/tips/?p=828</guid>

					<description><![CDATA[Linuxでのパーティション設定やファイルシステムの作成、データ移行からマウントまで全手順をまとめておきます。CentOS、Ubuntu、Debianすべて対応した方法をご紹介します。]]></description>
										<content:encoded><![CDATA[
<p>ファイルサーバーとして使用している自宅サーバー（Linux）のハードディスクが満杯になってしまったため、HDDを1TBから4TBのものに交換してみました。<span class="marker-under"><strong><span class="bold-red">Linuxでのパーティション設定</span>や<span class="bold-red">ファイルシステムの作成</span>、<span class="bold-red">データ移行からマウントまで</span>全手順をまとめておきます。</strong></span></p>



<p>なお、今回のサーバーはCentOS7ですが、UbuntuなどのDebian系でもまったく同じ手順です。</p>



<h2 class="wp-block-heading" id="サーバーのディスクを1tbx2本-4tbx2本に強化">サーバーのディスクを1TBx2本→4TBx2本に強化！</h2>



<p>もともとのサーバーのディスク構成がこちらです。OS用の320GBとデータ用HDD1TB2本で構成していました。</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="483" height="346" src="https://takabus.com/tips/wp-content/uploads/2022/02/image-13.png" alt="交換前のディスク構成" class="wp-image-1046" srcset="https://takabus.com/tips/wp-content/uploads/2022/02/image-13.png 483w, https://takabus.com/tips/wp-content/uploads/2022/02/image-13-300x215.png 300w" sizes="(max-width: 483px) 100vw, 483px" /></figure>



<p>今回はOS用のHDDはそのままに、<span class="marker-under"><strong>データ用の1TB2本を4TB2本に交換</strong></span>していきます。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="583" height="378" src="https://takabus.com/tips/wp-content/uploads/2022/02/image-14.png" alt="" class="wp-image-1047" srcset="https://takabus.com/tips/wp-content/uploads/2022/02/image-14.png 583w, https://takabus.com/tips/wp-content/uploads/2022/02/image-14-300x195.png 300w" sizes="(max-width: 583px) 100vw, 583px" /></figure>



<p><strong><span class="marker-under">購入したのは<span class="bold-red">WD Blueシリーズ</span>の<span class="bold-red">4TBモデル</span>「<span class="bold">WD40EZAZ-EC</span>」です。</span></strong></p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="579" src="https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_161023_vHDR_On--1024x579.jpg" alt="" class="wp-image-1043" srcset="https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_161023_vHDR_On--1024x579.jpg 1024w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_161023_vHDR_On--300x170.jpg 300w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_161023_vHDR_On--768x434.jpg 768w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_161023_vHDR_On--1536x869.jpg 1536w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_161023_vHDR_On--120x68.jpg 120w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_161023_vHDR_On--160x90.jpg 160w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_161023_vHDR_On--320x180.jpg 320w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_161023_vHDR_On--374x210.jpg 374w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_161023_vHDR_On-.jpg 2000w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>メインとバックアップ用をあわせて、2台購入しました。相場8,000円ほどの製品ですが、初売りセール中でしたので、なんと<span class="bold-red"><span class="marker-under">1台6,700円</span></span>で購入することができました。かなりお安く入手することができたと思います。</p>



<p>なぜか箱のセロハンテープが切られていましたが、中身は個別梱包されており、問題ありませんでした。</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="579" src="https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_171622_vHDR_On--1024x579.jpg" alt="" class="wp-image-1045" srcset="https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_171622_vHDR_On--1024x579.jpg 1024w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_171622_vHDR_On--300x170.jpg 300w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_171622_vHDR_On--768x434.jpg 768w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_171622_vHDR_On--1536x869.jpg 1536w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_171622_vHDR_On--120x68.jpg 120w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_171622_vHDR_On--160x90.jpg 160w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_171622_vHDR_On--320x180.jpg 320w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_171622_vHDR_On--374x210.jpg 374w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_171622_vHDR_On-.jpg 2000w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h2 class="wp-block-heading" id="ディスクを交換する">ディスクを交換する</h2>



<p><span class="marker-under"><strong>まずは新しいディスクに交換していきましょう。</strong></span></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="755" src="https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_160427_vHDR_On--1024x755.jpg" alt="" class="wp-image-1049" srcset="https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_160427_vHDR_On--1024x755.jpg 1024w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_160427_vHDR_On--300x221.jpg 300w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_160427_vHDR_On--768x567.jpg 768w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_160427_vHDR_On-.jpg 1533w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>ベースマシンはemachines J4517というモデルです。もともとWindows VistaのPCとして使っていたものですが、現在はCentOS7をインストールし、自宅サーバーとして活用しています。マイナーなメーカーですが、Core2 Duo E7200＋メモリ2GBとLinuxのサーバーとして動作させる上では十分すぎるスペックで、特に不満はありません。</p>



<p>ミニタワー型で内部には3.5インチベイが2つ搭載されています。</p>



<p>仕様表には3.5インチ2本しか搭載できないと記載されていますが、実際はカードリーダー後ろのスペースにも3.5インチのHDDが1本搭載できます。また、5インチベイも2つ用意されているため、マウンタを使えばさらに2本のHDDを搭載できます。トータル5本のハードディスクを搭載できるという、意外と高い拡張性を持っています。</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="579" src="https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_160438_vHDR_On--1024x579.jpg" alt="" class="wp-image-1044" srcset="https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_160438_vHDR_On--1024x579.jpg 1024w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_160438_vHDR_On--300x170.jpg 300w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_160438_vHDR_On--768x434.jpg 768w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_160438_vHDR_On--1536x869.jpg 1536w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_160438_vHDR_On--120x68.jpg 120w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_160438_vHDR_On--160x90.jpg 160w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_160438_vHDR_On--320x180.jpg 320w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_160438_vHDR_On--374x210.jpg 374w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220109_160438_vHDR_On-.jpg 2000w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>今回はもともとカードリーダーの後ろに搭載していたバックアップ用の1TBを取り外し、4TBに交換します。そしてパーティションなどの設定を済ませ、メインで使用していた3.5インチベイの1TBからデータを移行し、正常にデータの移行ができたらもともとの1TBから配線を取り外し、4TBのディスクをメインのデータディスクとして使用できるようにしたいと思います。</p>



<p><span class="marker-under"><strong>SATAケーブルでHDDとマザーボードを接続します。</strong></span></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="579" src="https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220110_090835_vHDR_On--1024x579.jpg" alt="" class="wp-image-1048" srcset="https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220110_090835_vHDR_On--1024x579.jpg 1024w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220110_090835_vHDR_On--300x170.jpg 300w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220110_090835_vHDR_On--768x434.jpg 768w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220110_090835_vHDR_On--1536x869.jpg 1536w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220110_090835_vHDR_On--120x68.jpg 120w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220110_090835_vHDR_On--160x90.jpg 160w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220110_090835_vHDR_On--320x180.jpg 320w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220110_090835_vHDR_On--374x210.jpg 374w, https://takabus.com/tips/wp-content/uploads/2022/02/サーバーHDD増設-P_20220110_090835_vHDR_On-.jpg 2000w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>電源ケーブルも接続します。</p>



<p>なお、接続についてはコネクタの形状が合致するところに接続すればOKです。パソコンのコネクタはフールプルーフに基づいた設計になっており、接続していけないところには最初からコネクタが刺さらないようになっています。しっかり差し込めれば正しく配線されているので、安心してください。</p>



<div class="wp-block-cocoon-blocks-balloon-ex-box-1 speech-wrap sb-id-1 sbs-stn sbp-l sbis-cb cf block-box"><div class="speech-person"><figure class="speech-icon"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/09/man-150x150.png" alt="" class="speech-icon-image"/></figure><div class="speech-name"></div></div><div class="speech-balloon">
<p>ケーブルの本数が足りないときは別途購入しましょう。</p>



<p>ちなみにSATAケーブルや電源関係のケーブルは、ハードオフなどの中古ショップでもかなりお安く入手することが可能です。100円/本くらいで安価に販売されていますので、近くにあるなら一度行ってみることをおすすめします。</p>
</div></div>



<h2 class="wp-block-heading" id="ディスクが認識されているか確認してみる">ディスクが認識されているか確認してみる</h2>



<p>ディスクの設置が完了したら、サーバーの電源を入れます。</p>



<p>Linuxが起動したらコンソールにログインし、ディスクが正常に認識されているか確認します。以降の操作はsudoをつけるか、root権限で行う必要があります。</p>



<p>今回は<span class="marker-under"><strong>ディスクのセットアップに<span class="bold-red">parted</span>を使用していきます。</strong></span></p>



<p>partedはfdiskに次ぐ次世代のディスク管理ツールであり、従来のMBRによるディスク管理のほか、GPTでのディスク管理に対応しています。2TB以上のディスクを使用するにはGPTでのディスク管理が必須となっているため、partedでのセットアップが推奨されています。このため、今回もpartedを使って設定を進めていきます。</p>



<p>partedのマニュアル→<a href="https://linux.die.net/man/8/parted">https://linux.die.net/man/8/parted</a></p>



<p>まず、partedを使ってディスクの一覧を表示してみましょう。</p>



<pre class="wp-block-code"><code>sudo parted -l</code></pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="619" height="547" src="https://takabus.com/tips/wp-content/uploads/2022/01/image-42.png" alt="" class="wp-image-855" srcset="https://takabus.com/tips/wp-content/uploads/2022/01/image-42.png 619w, https://takabus.com/tips/wp-content/uploads/2022/01/image-42-300x265.png 300w" sizes="(max-width: 619px) 100vw, 619px" /></figure>



<p><span class="marker-under">/dev/sda</span>や<span class="marker-under">/dev/sdb</span>というのがディスクの名前です。各種コマンドの実行時などディスクを指定したいときは、これらのパスを指定します。また、各ディスクに表示されている表はそのディスクに存在するパーティションを示しています。パーティションは<strong><span class="bold-red">ディスク名＋番号</span></strong>で表します。例えば、/dev/sdaの1番パーティションになんらかの変更を行いたい場合は、<span class="red"><span class="bold-red">/dev/sda1</span></span>というように指定します。</p>



<p>なお、ディスクのパスは環境により変化することがあります。例えば、マザーボード上でSATAケーブルをつなぐ場所を変更して再起動すると、同じディスクでもパスが変わることがありますので、十分注意する必要があります。</p>



<div class="wp-block-cocoon-blocks-balloon-ex-box-1 speech-wrap sb-id-1 sbs-stn sbp-l sbis-cb cf block-box"><div class="speech-person"><figure class="speech-icon"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/09/man-150x150.png" alt="" class="speech-icon-image"/></figure><div class="speech-name"></div></div><div class="speech-balloon">
<p>間違ったディスクを選択してしまうと、データが飛んでしまいます。正しいディスク（パーティション）に対して操作しているか、こまめに確認しながら作業しましょう。</p>
</div></div>



<p>今回追加したディスクは/dev/sdcとして正常に認識されていることが確認できます。<strong>「ディスクラベルが認識できません」</strong>というエラーが表示されていますが、これはセットアップが済んでいないためであり、現時点では問題ありません。以降、<span class="red">/dev/sdc</span>に対しセットアップを行っていきます。</p>



<h2 class="wp-block-heading" id="パーティションを作成する">パーティションを作成する</h2>



<p>ディスクが認識されていることが確認できたら、いよいよHDDのセットアップを行っていきます。</p>



<p><span class="marker-under"><strong>まず、<span class="bold-red">パーティション</span>を作成していきます。</strong></span>追加したディスクを引数に指定し、partedを起動します。</p>



<pre class="wp-block-code"><code>sudo parted /dev/sdc
#ディスクの情報を表示する
(parted)print</code></pre>



<p>正しいディスクが選択されているか確認するため、一度printコマンドを実行します。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="659" height="452" src="https://takabus.com/tips/wp-content/uploads/2022/02/image-25-2.png" alt="" class="wp-image-1014" srcset="https://takabus.com/tips/wp-content/uploads/2022/02/image-25-2.png 659w, https://takabus.com/tips/wp-content/uploads/2022/02/image-25-2-300x206.png 300w" sizes="(max-width: 659px) 100vw, 659px" /></figure>



<p>問題なければ次へ進みます。</p>



<div class="wp-block-cocoon-blocks-icon-box common-icon-box block-box alert-box">
<p><strong><span class="marker-under">partedでの操作はコマンドが実行されたのと同時に変更が適用されます。</span></strong></p>



<p>fdiskではsaveコマンドを入力するまで変更は適用されませんでしたが、partedでは即座に変更が反映されてしまうため、操作には十分気をつけましょう。とくにディスクの選択を誤ると、ほかのディスクのデータを破壊する可能性もあるため、慎重に操作してください。</p>
</div>



<p><strong>selectコマンド</strong>を実行し、新しく追加したハードディスクを選択します。</p>



<pre class="wp-block-code"><code>(parted) <strong>select</strong>
新しいデバイス?  &#91;/dev/sdc]?
/dev/sdc を使用</code></pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="242" height="79" src="https://takabus.com/tips/wp-content/uploads/2022/01/image-26.png" alt="" class="wp-image-831"/></figure>



<p>もう一度printを実行し、正しく選択されているか確認しましょう。間違ったディスクを選択していると、データが飛んでしまいますので、マメに確認することが重要です。</p>



<pre class="wp-block-code"><code>(parted) <strong>p</strong>
エラー: /dev/sdc: ディスクラベルが認識できません。
モデル: ATA WDC WD40EZAZ-00S (scsi)
ディスク /dev/sdc: 4001GB
セクタサイズ (論理/物理): 512B/4096B
パーティションテーブル: unknown
ディスクフラグ:</code></pre>



<p>正しく認識されています。</p>



<p><strong><span class="marker-under">はじめにラベルを作成します。</span></strong>MBRでは最大2TBしかサポートされないため、GPTでラベルを作成します。2TB以下のディスクであっても、基本的には<span class="bold-red">GPT</span>を選択してください。</p>



<pre class="wp-block-code"><code>(parted) <strong>mklabel gpt</strong></code></pre>



<p>メッセージが表示されなければ、正常にラベルが作成されました。</p>



<p>つづけて、パーティションを作成していきます。</p>



<p>パーティションの作成は<strong><span class="marker-under">mkpartコマンド</span></strong>で行います。対話的に実行できるため、質問に答える形でひとつずつ設定していきます。</p>



<pre class="wp-block-code"><code>(parted) <strong>mkpart</strong>
パーティションの名前?  &#91;]?
ファイルシステムの種類?  &#91;ext2]?<strong> ext4</strong>
開始? <strong>0%</strong>
終了? <strong>100%</strong></code></pre>



<p>まず、<span class="marker-under">パーティションの名前</span>を問われます。名前を設定しておくことで、パーティションテーブルを表示した際にどのパーティションであるか判別しやすくなります。操作ミスを防止するうえでは設定しておいたほうがよさそうですが、今回は１つしかパーティションを作成しないため、ここでは空のままにしておきました。</p>



<p>次に<strong><span class="marker-under">ファイルシステムの種類</span></strong>を問われます。</p>



<p>Linuxではさまざまなファイルシステムを選択することができ、おもにext4かxfsから選択するのが一般的です。ext4はCentOS6で標準採用されていたファイルシステムで、長年の実績があり、安定しているのが特徴です。削除したファイルも比較的容易に復旧することができたりと安心感があります。</p>



<p>一方、xfsはCentOS7で標準採用されるようになったファイルシステムで、読み書きが高速であることが特徴です。しかし、メモリの消費が多かったり、小さいサイズのファイルの読み書きが遅いとも言われています。また、ext4とは異なり、削除してしまったファイルの復旧は困難であるため、万が一の操作ミスにも対応できる厳重なバックアップが要求されます。</p>



<p>ファイルシステムについてはいろいろと悩みましたが、<span class="marker-under"><span class="bold-red">最終的には手堅くext4を選択することにしました。</span></span>個人的にはext4は長くに渡って使用してきた経緯があり、最も信頼を置いているファイルシステムということで、安心をとってext4を選択しました。</p>



<p>最後に開始と終了を入力します。今回はディスク領域すべてを1つのパーティションに割り当てるため、開始は0%、終了は100%としました。</p>



<div class="wp-block-cocoon-blocks-balloon-ex-box-1 speech-wrap sb-id-1 sbs-stn sbp-l sbis-cb cf block-box"><div class="speech-person"><figure class="speech-icon"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/09/man-150x150.png" alt="" class="speech-icon-image"/></figure><div class="speech-name"></div></div><div class="speech-balloon">
<p><span class="marker-under"><strong>このとき</strong></span><span class="red"><span class="marker-under"><strong>「％」も必ず入力してください。</strong></span></span></p>



<p>「％」がないとセクタ単位での指定となってしまいます。</p>
</div></div>



<p>以上でパーティションの作成は終了です。print（またはp）を実行して、パーティションを確認してみます。</p>



<pre class="wp-block-code"><code>(parted) <strong>p</strong>
モデル: ATA WDC WD40EZAZ-00S (scsi)
ディスク /dev/sdc: 4001GB
セクタサイズ (論理/物理): 512B/4096B
パーティションテーブル: gpt
ディスクフラグ:

番号  開始    終了    サイズ  ファイルシステム  名前  フラグ
 1    1049kB  4001GB  4001GB</code></pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="488" height="177" src="https://takabus.com/tips/wp-content/uploads/2022/01/image-27.png" alt="" class="wp-image-833" srcset="https://takabus.com/tips/wp-content/uploads/2022/01/image-27.png 488w, https://takabus.com/tips/wp-content/uploads/2022/01/image-27-300x109.png 300w" sizes="(max-width: 488px) 100vw, 488px" /></figure>



<p>正しくパーティションが作成されています。</p>



<p>なお、この時点ではファイルシステムは空欄で問題ありません。ファイルシステムの作成は次のステップで行います。</p>



<p>以上でpartedでの操作は終了です。qでpartedを終了させましょう。</p>



<pre class="wp-block-code"><code>(parted)q</code></pre>



<h2 class="wp-block-heading" id="ファイルシステムの作成">ファイルシステムの作成</h2>



<p><strong><span class="marker-under">次に<span class="bold-red">ファイルシステム</span>を作成します。</span></strong></p>



<p>ディスクはパーティションを作成しただけでは使用することはできません。実際にファイルを保存できるようにするには、作成したパーティションにファイルシステムを作成する必要があります。</p>



<p>ファイルシステムの作成には<span class="bold-red">mkfsコマンド</span>を使用します。mkfsコマンドには複数の種類があり、今回はext4でファイルシステムを作成するため、<span class="bold-red"><span class="marker-under">mkfs.ext4コマンド</span></span>を使用します。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="501" height="37" src="https://takabus.com/tips/wp-content/uploads/2022/02/image.png" alt="" class="wp-image-957" srcset="https://takabus.com/tips/wp-content/uploads/2022/02/image.png 501w, https://takabus.com/tips/wp-content/uploads/2022/02/image-300x22.png 300w" sizes="(max-width: 501px) 100vw, 501px" /><figcaption>作成したいファイルシステムの種類により、正しいmkfsコマンドを使うようにします。</figcaption></figure>



<p>作成したパーティションを引数に指定し、mkfs.ext4コマンドを実行しましょう。</p>



<pre class="wp-block-code"><code>$mkfs.ext4 /dev/sdc1</code></pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="334" height="96" src="https://takabus.com/tips/wp-content/uploads/2022/01/image-28.png" alt="" class="wp-image-834" srcset="https://takabus.com/tips/wp-content/uploads/2022/01/image-28.png 334w, https://takabus.com/tips/wp-content/uploads/2022/01/image-28-300x86.png 300w" sizes="(max-width: 334px) 100vw, 334px" /></figure>



<p>先述しましたが、指定するのはパーティションですので数字まで含めたパスを指定します。<strong><span class="bold-red"><span class="marker-under">/dev/sdcはディスク名、パーティション名は/dev/sdc<span style="text-decoration: underline;">1</span></span></span></strong>になります。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="659" height="452" src="https://takabus.com/tips/wp-content/uploads/2022/02/image-29.png" alt="" class="wp-image-1015" srcset="https://takabus.com/tips/wp-content/uploads/2022/02/image-29.png 659w, https://takabus.com/tips/wp-content/uploads/2022/02/image-29-300x206.png 300w" sizes="(max-width: 659px) 100vw, 659px" /></figure>



<p>先ほども確認しましたが、もう一度partedでパーティションを確認してみましょう。</p>



<pre class="wp-block-code"><code>$sudo parted -l
モデル: ATA WDC WD40EZAZ-00S (scsi)
ディスク /dev/sdc: 4001GB
セクタサイズ (論理/物理): 512B/4096B
パーティションテーブル: gpt
ディスクフラグ:

番号  開始    終了    サイズ  ファイルシステム  名前  フラグ
 1    1049kB  4001GB  4001GB  <strong>ext4</strong></code></pre>



<p>今度はファイルシステムの欄にext4と表示されています。無事ファイルシステムを作成できたようです。</p>



<p>ここまでくればLinuxでマウントしたり、実際にファイルを保存したりすることが可能です。</p>



<h2 class="wp-block-heading" id="予約領域の削減-重要-忘れがち">予約領域の削減【重要！忘れがち】</h2>



<p>ここまでで終わりにしている方が多いのですが、<span class="marker-under-red"><span class="bold">実はもうひとつやっておくべきことがあります。</span></span>それが<span class="bold-red"><span class="marker-under">予約領域の削減</span></span>です。</p>



<p>予約領域とはディスクの容量が完全に満杯になってしまわないようにあらかじめ用意されている領域のことです。デフォルトではパーティションごとに5%が予約されるようになっています。例えば、今回交換した4TBのHDDであれば、200GBも使用できない領域が生じてしまうことになります。これでは、せっかく大容量のHDDを購入しても、容量をフルに使うことができません。そこで予約領域を削減して、自由に使用できる領域を多くすることにします。</p>



<p>予約領域はtune2fsコマンドで変更可能です。-rオプションとともに予約するブロック数を指定します。</p>



<pre class="wp-block-code"><code>admin@sv01:~ $sudo tune2fs -r 2621440 /dev/sdc1
tune2fs 1.42.9 (28-Dec-2013)
Setting reserved blocks count to 2621440</code></pre>



<p>今回は予約領域を10GBに設定しました。</p>



<p>削減前のdfコマンドの結果です。削減前は空き容量が3.4TBしかありませんでしたが、tune2fsコマンドで予約領域を削減した後は3.6TBまで200GBも空き容量が増加しています。</p>



<pre class="wp-block-code"><code># 削減前
/dev/sdc1        3.6T   89M  <span class="bold-blue">3.4T</span>    1% /mnt/hdd01
# 削減後
/dev/sdc1        3.6T   89M  <span class="bold-red">3.6T</span>    1% /mnt/hdd01</code></pre>



<p>予約領域の設定を忘れていると、せっかく大容量のHDDを導入しても、使える容量が小さいことになってしまいます。忘れがちな設定ですが、必ずやっておくことをおすすめします。</p>



<h2 class="wp-block-heading" id="古いディスクからデータをコピーする">古いディスクからデータをコピーする</h2>



<p>ここまでで、すでにディスクは使用可能な状態になっています。新しいディスクを使用できるようにすればOKという方はここで終了です。お疲れさまでした～(^_^)/~</p>



<p>ここからは<strong><span class="marker-under">今まで使用していたディスクからデータを移行する手順</span></strong>を紹介していきます。</p>



<p>まずはマウントします。</p>



<pre class="wp-block-code"><code>admin@sv01:~ $sudo mkdir /mnt/hdd01
admin@sv01:~ $sudo mount -t ext4 /dev/sdc1 /mnt/hdd01</code></pre>



<p>ここでは<strong>/mnt/hdd01</strong>というマウントポイントを作成し、新しいディスク（パーティション）をマウントしています。/mnt/hdd01にファイルを保存すると、新しいディスクに保存されるようになります。</p>



<p><span class="marker-under"><strong>ファイルのコピーには<span class="bold-red">rsync</span>が便利です。</strong></span>rsyncでファイルをまるごとコピーします。</p>



<pre class="wp-block-code"><code>admin@sv01:/mnt/hdd01 $sudo rsync -avh /sambadata/* /mnt/hdd01</code></pre>



<p><code>-avh</code>というオプションはファイルをそのままの状態でコピー（パーミッションやディレクトリを保持してコピー）し、進捗を表示するという意味です。本当は3つのオプションを組み合わせているのですが、そっくりそのままファイルをコピーしたいときに良く使うため、決まり文句として覚えておくと便利でしょう。</p>



<p>自分のやり方ですが、大量のファイルコピー時はコンソールをもう一つ開いて、dfコマンドをwatchするようにしています。rsyncの進捗と並べて表示させると、コピーの動作状況とコピー済みの容量が同時に把握でき、わかりやすいのでおすすめです。</p>



<pre class="wp-block-code"><code>$watch df -h</code></pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="889" height="449" src="https://takabus.com/tips/wp-content/uploads/2022/01/image-41.png" alt="" class="wp-image-852" srcset="https://takabus.com/tips/wp-content/uploads/2022/01/image-41.png 889w, https://takabus.com/tips/wp-content/uploads/2022/01/image-41-300x152.png 300w, https://takabus.com/tips/wp-content/uploads/2022/01/image-41-768x388.png 768w" sizes="(max-width: 889px) 100vw, 889px" /><figcaption>左はdfコマンドを定期実行、右はrsyncの進捗を表示。何GBコピーされたかが一目瞭然なので、並べておくとわかりやすいです。</figcaption></figure>



<p>自分の場合は900GBのコピーで4時間ほどかかりました。小さいファイルが多い場合は、もっとかかるかもしれませんね。</p>



<h2 class="wp-block-heading" id="自動でマウントするようにする">自動でマウントするようにする</h2>



<p>これも必要な方向けです。起動時にディスクが自動的にマウントされるようにします。</p>



<p>一般的にHDDの自動マウントには/etc/fstabを使用します。今までは教科書どおりfstabによる自動マウントを行っていましたが、HDDを取り外すとブートできなくなる問題があるため、今回の交換を機に他の方法でのマウントに切り替えたいと思います。起動時に自動実行されるバッチファイルを作成し、バッチからマウントするようにしたいと思います。</p>



<p>現在のfstabがこちら。</p>



<pre class="wp-block-code"><code>UUID=e138a963-720a-4922-8195-edbda61b89a8 /                       ext4    defaults        1 1
UUID=b13fe526-67fe-4590-a4a9-5678b9a09649 /home                   ext4    defaults        1 2
UUID=055c727b-c8fb-4957-a7a0-cf570e000620 swap                    swap    defaults        0 0

<strong>UUID=248d74d9-e215-4f4d-8f37-f97ac0adaf74 /sambasnap              ext4    defaults        0 0
UUID=33c969c3-53f9-4e10-bffa-b9115ff414f7 /sambadata              ext4    defaults        0 0</strong></code></pre>



<p>ちなみにUUIDはこちらです。 /dev/sdc1 を/sambadataにマウントするようにします。</p>



<pre class="wp-block-code"><code>admin@sv01:~ $sudo blkid -o list
device     fs_type label    mount point    UUID
--------------------------------------------------------------------------------
/dev/sda1  ext4             /              e138a963-720a-4922-8195-edbda61b89a8
/dev/sda2  ext4             /home          b13fe526-67fe-4590-a4a9-5678b9a09649
/dev/sda3  swap             &lt;swap&gt;         055c727b-c8fb-4957-a7a0-cf570e000620
/dev/sda4  ext4             /sambasnap     248d74d9-e215-4f4d-8f37-f97ac0adaf74
/dev/sdb1  ext4             /sambadata     33c969c3-53f9-4e10-bffa-b9115ff414f7
/dev/sdc1  ext4             (not mounted)  10230133-1a99-40ea-9ada-f3e3893a8e1b</code></pre>



<p>/etc/fstabの下2行がデータディスクとなります。<span class="red">この2つを削除します。</span></p>



<pre class="wp-block-code"><code>$sudo /etc/fstab

UUID=e138a963-720a-4922-8195-edbda61b89a8 /                       ext4    defaults        1 1
UUID=b13fe526-67fe-4590-a4a9-5678b9a09649 /home                   ext4    defaults        1 2
UUID=055c727b-c8fb-4957-a7a0-cf570e000620 swap                    swap    defaults        0 0
・・・以下削除・・・</code></pre>



<p>fstabから削除した代わりに、起動時にディスクをマウントするためのスクリプトを<strong><span class="bold-red">/etc/rc.d/rc.local</span></strong>に記載します。/etc/rc.d/rc/localは起動時に自動的に実行されるスクリプトファイルで、ここにマウントするコマンドを記載しておけば、起動時に自動的にマウントが実行されます。</p>



<pre class="wp-block-code"><code>admin@sv01:~ $vi /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local</code></pre>



<p>最下部にマウントするためのコマンドを記載します。</p>



<pre class="wp-block-code"><code>admin@sv01:~ $sudo vi /etc/rc.d/rc.local
・・・中略・・・
<strong>mount -t ext4 UUID="10230133-1a99-40ea-9ada-f3e3893a8e1b" /sambadata</strong></code></pre>



<p>変更を保存したら、実行権を付与します。</p>



<pre class="wp-block-code"><code>admin@sv01:~ $sudo chmod +x /etc/rc.d/rc.local</code></pre>



<p>デフォルトでは実行権が付与されていないため、実行権を追加しないと実行されません。忘れずに実行権を付与しておきましょう。</p>



<h2 class="wp-block-heading" id="まとめ">まとめ</h2>



<p>Linuxマシンのディスクを交換した後のセットアップについて紹介しました。</p>



<p>破壊的な操作が多くなるため、設定ミスによるデータ損失には十分注意するようにしてください。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://takabus.com/tips/828/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【VirtualBox】仮想マシンのディスク容量を増やすには？サイズ変更からLinuxのパーティション・LVM拡張までの全手順</title>
		<link>https://takabus.com/tips/552/</link>
					<comments>https://takabus.com/tips/552/#respond</comments>
		
		<dc:creator><![CDATA[ばすにっきTips]]></dc:creator>
		<pubDate>Sun, 07 Nov 2021 04:27:42 +0000</pubDate>
				<category><![CDATA[サーバー]]></category>
		<category><![CDATA[VirtualBox]]></category>
		<category><![CDATA[LVM]]></category>
		<category><![CDATA[xfs]]></category>
		<category><![CDATA[parted]]></category>
		<guid isPermaLink="false">https://takabus.com/tips/?p=552</guid>

					<description><![CDATA[VirtualBoxの仮想マシン（Linux）の容量を増やす手順をまとめておきます。仮想ハードディスクファイルのサイズ変更から、Linuxのパーティション変更、LVMについての話まで、網羅的にまとめています。]]></description>
										<content:encoded><![CDATA[
<p>ある日、仮想マシンサーバーのWordPressの動きがおかしいなあと思っていたら、ルートディレクトリの使用率が98%に迫っていました。容量が足りなかったんですね。。</p>



<pre class="wp-block-code"><code>&#91;root@ts02 ~]# df -h
ファイルシス            サイズ  使用  残り 使用% マウント位置
devtmpfs                  908M     0  908M    0% /dev
tmpfs                     920M     0  920M    0% /dev/shm
tmpfs                     920M  8.5M  911M    1% /run
tmpfs                     920M     0  920M    0% /sys/fs/cgroup
/dev/mapper/centos-root   8.0G  7.8G  243M   98% /
/dev/sda1                1014M  178M  837M   18% /boot
tmpfs                     184M     0  184M    0% /run/user/0</code></pre>



<p>こういったとき、<strong><span class="red"><span class="marker-under">VirtualBoxの仮想マシン（Linux）の容量を増やす手順をまとめておきます。</span></span></strong>仮想ハードディスクファイルのサイズ変更から、Linuxのパーティション変更、LVMについての話まで、網羅的にまとめています。</p>



<h2 class="wp-block-heading" id="step1-仮想ハードディスクを大きくする">&lt;STEP1&gt;仮想ハードディスクを大きくする</h2>



<p>まずは、仮想ハードディスクファイルのサイズを拡張しないといけません。</p>



<p>「VirtualBoxマネージャー」から、仮想マシンの起動ディスクファイル名をチェックしておきます。</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/02/image-394.png" alt="スクリーンショット" class="wp-image-4032"/></figure></div>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/02/image-395.png" alt="スクリーンショット" class="wp-image-4033"/></figure></div>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/02/image-396.png" alt="スクリーンショット" class="wp-image-4034"/></figure></div>



<p>次に、「仮想メディアマネージャー」から仮想ディスクを拡張します。</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/02/image-397.png" alt="スクリーンショット" class="wp-image-4035"/></figure></div>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/02/image-398.png" alt="スクリーンショット" class="wp-image-4036"/></figure></div>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/02/image-399.png" alt="スクリーンショット" class="wp-image-4037"/></figure></div>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/02/image-400.png" alt="スクリーンショット" class="wp-image-4038"/></figure></div>



<p>サイズを大きくします。ここでは10GBを20GBまで拡張することにしました。</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/02/image-401.png" alt="" class="wp-image-4039"/></figure></div>



<p>サイズ指定後、[適用]ボタンを押します。</p>



<div class="wp-block-cocoon-blocks-tab-box-1 blank-box bb-tab bb-memo block-box has-border-color has-light-green-border-color">
<p>サイズ変更後、[適用]を押すと、エラーになることがあります。</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/02/image-403.png" alt="スクリーンショット" class="wp-image-4042"/></figure></div>



<p>これは仮想ハードディスクファイルがVDIではないためです。この場合は一旦、仮想ハードディスクファイルを.vdi形式に変換する必要があります。「仮想ディスクマネージャー」にて、[コピー]をクリックし、VDI形式に変換後、再度サイズ変更を実施してみてください。</p>
</div>



<h2 class="wp-block-heading" id="step2-linuxから拡張したディスクを使えるようにしよう">&lt;STEP2&gt;Linuxから拡張したディスクを使えるようにしよう</h2>



<p>サイズを拡張したら、とりあえずマシンを起動して、Linuxでパーティション構成を見てみましょう。</p>



<p>Linuxではpartedというコマンドを使って、パーティションテーブルを確認・操作することが可能です。parted -lでディスクに存在するパーティションを表示できます。</p>



<pre class="wp-block-code"><code>&#91;root@ts02 ~]# parted -l
モデル: ATA VBOX HARDDISK (scsi)
ディスク /dev/sda: 21.5GB
セクタサイズ (論理/物理): 512B/512B
パーティションテーブル: msdos
ディスクフラグ:

番号  開始    終了    サイズ  タイプ   ファイルシステム  フラグ
 1    1049kB  1075MB  1074MB  primary  xfs               boot
 2    1075MB  10.7GB  9663MB  primary                    lvm</code></pre>



<p>2つのパーティションが確認できました。</p>



<p>OSが読み書きできるのは、あくまでパーティションです。ディスク容量を増やしただけでは、パーティションのサイズは変わらず、OSから使用することはできません。拡張した分は、パーティションが割り当てられていない空の領域となってしまいます。</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/02/image-405.png" alt="パーティションの解説" class="wp-image-4044"/></figure></div>



<p>そこで、パーティションのサイズを拡張することが必要となります。</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/02/image-404.png" alt="パーティションの解説" class="wp-image-4043"/></figure></div>



<p>実際の方法ですが、パーティション2の終点をディスク末尾に変更するようにします。</p>



<p>まず、2番のパーティションを削除します。そして、始点をそのままにして、終点のみディスク末尾のセクタに変更し、パーティションを作り直すようにします。これならすでに存在しているデータを破壊せず、後からパーティションを拡張することが可能です。当記事では、この 詳しい手順を紹介します。</p>



<div class="wp-block-cocoon-blocks-tab-box-1 blank-box bb-tab bb-check block-box">
<p>パーティションを削除というところで、<strong>「データ消えちゃうんじゃないの？」</strong>と思った方もいるかも知れませんが、大丈夫です。パーティションというのはあくまで「仕切り」ですので、一度消しても後から同じ位置に戻せば、データが消えることはありません。</p>



<p>とはいえ、間違って操作すると、一瞬でデータが飛びます。<strong>必ずバックアップしてから、以降の操作を行ってください。</strong></p>
</div>



<h3 class="wp-block-heading" id="環境">環境</h3>



<p>改めて、今回の環境をまとめておきます。</p>



<ul><li>マシン（A）<ul><li>Oracle VM VirtualBox 5.2.22 r126460</li><li>OS：CentOS7.4</li></ul></li><li>パーティション変更を行うマシン（B）<ul><li>Oracle VM VirtualBox 5.2.22 r126460</li><li>OS：ubuntu-18.04.5-desktop-amd64.iso</li></ul></li></ul>



<p>パーティションの変更は、対象のディスクがマウントされていない状態で行わなければなりません。よって、用意した別のマシンにディスクを接続し、パーティショニングする必要があります。（つまり、マシンの起動に使用しているディスクをパーティショニングすることはできません。）</p>



<p>当記事では、仮想マシンのディスクを別の仮想マシンからパーティショニングします。UbuntuのISOイメージから別の仮想マシンをもう一つ立ち上げ、仮想ディスクのパーティションを変更するようにしていますが、UbuntuなどのISOを仮想マシンの仮想ディスクドライブにセットして、Liveで立ち上げてもOKです。なお、実機では、UbuntuなどをUSBやCDに焼いてセットし、そこから起動すれば、当記事と同じ手順で実行できます。</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/02/image-423.png" alt="スクリーンショット" class="wp-image-4080"/><figcaption>仮想高額ドライブにUbuntuのISOをセット。</figcaption></figure></div>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/02/image-420.png" alt="スクリーンショット" class="wp-image-4061"/><figcaption>ISOからLiveで立ち上げれば、パーティションの変更が可能となる。</figcaption></figure></div>



<h2 class="wp-block-heading" id="方法">方法</h2>



<p>１つずつ、やり方を紹介していきます。</p>



<div class="wp-block-cocoon-blocks-icon-box common-icon-box block-box alert-box">
<p><strong>コマンドはすべてスーパーユーザー（rootユーザー）で実行してください。</strong>一般ユーザーでは、ディスクが正しく表示されない場合があります。</p>
</div>



<h3 class="wp-block-heading" id="手順1-現在のディスク情報を取得">【手順1】現在のディスク情報を取得</h3>



<p>まず、パーティション変更前のフォーマットを取得し、メモしておきます。</p>



<pre class="wp-block-code"><code>
&#91;root@ts02 ~]# df -Th
ファイルシス            タイプ   サイズ  使用  残り 使用% マウント位置
devtmpfs                devtmpfs   908M     0  908M    0% /dev
tmpfs                   tmpfs      920M     0  920M    0% /dev/shm
tmpfs                   tmpfs      920M  8.6M  911M    1% /run
tmpfs                   tmpfs      920M     0  920M    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs #←チェック！ 8.0G  7.8G  243M   98% /
/dev/sda1               xfs       1014M  178M  837M   18% /boot
tmpfs                   tmpfs      184M     0  184M    0% /run/user/0</code></pre>



<p>Tオプションにより、フォーマットを表示させています。今回はxfsが採用されていることが確認できました。フォーマットは後で必要になるので、覚えておいてください。</p>



<p>次に、partedで拡張したいディスクを開きます。</p>



<pre class="wp-block-code"><code>&#91;root@ts02 ~]# parted /dev/sda
GNU Parted 3.1
/dev/sda を使用
GNU Parted へようこそ！ コマンド一覧を見るには 'help' と入力してください。</code></pre>



<p>partedで開くときは、パーティションではなくデバイスを指定します。/dev/sda1など、末尾に数字がついているものはパーティションです。そうではなくて、<strong>数字なしで/dev/sdaのように指定します。</strong></p>



<p>とりあえずprintしてみます。</p>



<pre class="wp-block-code"><code>(parted) print
モデル: ATA VBOX HARDDISK (scsi)
ディスク /dev/sda: 21.5GB
セクタサイズ (論理/物理): 512B/512B
パーティションテーブル: msdos
ディスクフラグ:

番号  開始    終了    サイズ  タイプ   ファイルシステム  フラグ
 1    1049kB  1075MB  1074MB  primary  xfs               boot
 2    1075MB  10.7GB  9663MB  primary                    lvm</code></pre>



<p>間違ったデバイスを指定していないことを確認したら、<strong>表示単位をセクタに変更</strong>します。</p>



<pre class="wp-block-code"><code>(parted)unit s</code></pre>



<p>セクタ単位にしたら、もう一度printします。この出力も後で必要になります。確実にメモしておいてください。<strong>とくに開始と終了の値は、1文字でも間違うとデータが飛ぶので要注意です。</strong></p>



<pre class="wp-block-code"><code>(parted) print
モデル: ATA VBOX HARDDISK (scsi)
ディスク /dev/sda: 41943040s
セクタサイズ (論理/物理): 512B/512B
パーティションテーブル: msdos
ディスクフラグ:

番号  開始      終了       サイズ     タイプ   ファイルシステム  フラグ
 1    2048s     2099199s   2097152s   primary  xfs               boot
 2    2099200s  20971519s  18872320s  primary                    lvm</code></pre>



<h3 class="wp-block-heading" id="手順2-パーティションテーブルの変更">【手順2】パーティションテーブルの変更</h3>



<p>ここからいよいよパーティションテーブルを変更していきます。</p>



<p>まず、2番めのパーティションを削除します。</p>



<pre class="wp-block-code"><code>(parted)rm 2</code></pre>



<p>以下のエラーが表示された場合は、ディスクが使用中です。マウントされていないか確認してください。</p>



<pre class="wp-block-code"><code>できませんでした。おそらく、使用中だったのが原因だと思われます。そのため、古いパーティション情報がそのまま使われます。さらなる変更をする前に再起動してください。</code></pre>



<div class="wp-block-cocoon-blocks-icon-box common-icon-box block-box alert-box">
<p>使用中のディスクは、パーティショニングできません。必ず別のマシン（もしくはLiveイメージのOS）からパーティショニングする必要があります。</p>
</div>



<p>問題なければ、パーティションを作成します。mkpartコマンドを実行し、プロンプトに従ってください。</p>



<pre class="wp-block-code"><code>(parted) mkpart
Partition type? primary/extended? primary
File system type? &#91;ext2]? xfs
Start? 2099200s
End? 100%</code></pre>



<p>いくつか質問されますので、以下のように回答します。</p>



<ul><li>パーティションタイプはPrimaryを指定します。</li><li>File system typeは確認しておいたフォーマット（ここではxfs）を指定します。</li><li>Startはメモしておいたパーティション2の開始（ここでは2099200s）を指定します。単位付きで指定してください。</li><li>Endはディスクの未割り当て領域すべてをパーティションに割り当てたいので、100%を指定します。</li></ul>



<p>すべてやったら、printします。問題なさそうなら、qと入力してpartedを終了します。</p>



<h3 class="wp-block-heading" id="手順3-ファイルシステムの拡張">【手順3】ファイルシステムの拡張</h3>



<div class="wp-block-cocoon-blocks-icon-box common-icon-box block-box information-box">
<p>ここからは、元のマシン（B）に戻って作業を行ってもOK</p>
</div>



<p>次に、ファイルシステムの拡張を行います。</p>



<p>ファイルシステムの拡張を行うことで、OSから拡張した分のパーティションを使用できるようになります。使用するコマンドは、ディスクのフォーマットによって異なります。</p>



<div class="wp-block-cocoon-blocks-blank-box-1 blank-box block-box has-border-color has-amber-border-color">
<ul><li><strong>XFS以外(ext4など)→resize2fsコマンド</strong></li><li><strong>XFSの場合→xfs_growthfsコマンド</strong></li></ul>
</div>



<p>今回はxfsだったので、xfs_growthfsコマンドを使用します。</p>



<pre class="wp-block-code"><code># xfs_growfs /dev/centos/root</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" src="https://takabus.com/tips/wp-content/uploads/2021/02/image-421.png" alt="スクリーンショット" class="wp-image-4063"/></figure></div>



<p>うまくいけば、下のようなメッセージが表示されます。その場合は拡張成功です。</p>



<pre class="wp-block-code"><code>data blocks changed from 2096128 to 4699136</code></pre>



<p>一方、スクリーンショットのように、拡張に成功した旨のメッセージが表示されない場合は失敗です。次の手順に進んでください。</p>



<h2 class="wp-block-heading" id="うまくいかなかったときは">うまくいかなかったときは？</h2>



<p><strong>今回の手順でうまく行かなかった場合、おそらくLVMが使用されています。</strong></p>



<p>LVMというのは、ディスクを仮想的に認識するシステムです。LVMが使用されているときにパーティションを変更した場合は、その変更をLVMにも認識させる必要があります。</p>



<p>以下に手順を掲載していますので、続けて操作してください。</p>



<h3 class="wp-block-heading" id="手順4-パーティションにlvmフラグを立てる">【手順4】パーティションにLVMフラグを立てる</h3>



<p>はじめに、拡張したパーティションにpartedでLVMフラグを立てます。</p>



<div class="wp-block-cocoon-blocks-icon-box common-icon-box block-box information-box">
<p>ここは別マシンでやる必要はありません。フラグに関しては、使用中のディスクに対しても設定することができます。</p>
</div>



<pre class="wp-block-code"><code>&#91;root@ts02 ~]# parted /dev/sda
GNU Parted 3.1
/dev/sda を使用
GNU Parted へようこそ！ コマンド一覧を見るには 'help' と入力してください。
(parted) set 2 lvm on
(parted) print
モデル: ATA VBOX HARDDISK (scsi)
ディスク /dev/sda: 21.5GB
セクタサイズ (論理/物理): 512B/512B
パーティションテーブル: msdos
ディスクフラグ:

番号  開始    終了    サイズ  タイプ   ファイルシステム  フラグ
 1    1049kB  1075MB  1074MB  primary  xfs               boot
 2    1075MB  21.5GB  20.4GB  primary                    lvm ＃LVMフラグが付いた
(parted)q</code></pre>



<p>LVMフラグが立ちました。</p>



<h3 class="wp-block-heading" id="手順5-lvmの拡張">【手順5】LVMの拡張</h3>



<p>次に、LVMを拡張していきます。まず、PV（物理ボリューム）を拡張します。</p>



<pre class="wp-block-code"><code>&#91;root@ts02 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               &lt;9.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              2303
  Free PE               0
  Allocated PE          2303
  PV UUID               l24sPn-F5Es-m6NA-135o-FIkb-IGGk-FP1E2r

&#91;root@ts02 ~]# pvresize /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized

&#91;root@ts02 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               &lt;19.00 GiB / not usable 2.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              4863
  Free PE               2560
  Allocated PE          2303
  PV UUID               l24sPn-F5Es-m6NA-135o-FIkb-IGGk-FP1E2r</code></pre>



<p>PV Sizeのところに注目すると、ちゃんと容量が増えたことがわかります。</p>



<p>つづいて、LV（論理ボリューム）の拡張を行います。</p>



<p>論理ボリュームの拡張は、<code>lvextend</code>コマンドで行います。partedでパーティションに10GB追加したので、とりあえず10GB追加するようコマンドします。</p>



<pre class="wp-block-code"><code>&#91;root@ts02 ~]# lvextend -L +10G /dev/centos/root
  Insufficient free space: 2560 extents needed, but only 2535 available</code></pre>



<p>エラーとなりました。誤差が生じるので、単純な計算通りにはいきません。追加できる最大値がエラー内に表示されているので、その値を＋の次に指定すればOKです。</p>



<pre class="wp-block-code"><code>&#91;root@ts02 ~]# lvextend -L +2535 /dev/centos/root
  Size of logical volume centos/root changed from 8.09 GiB (2072 extents) to 17.90 GiB (4583 extents).
  Logical volume centos/root successfully resized.</code></pre>



<p>うまくいきました。</p>



<p>最後にファイルシステムを拡張します。</p>



<pre class="wp-block-code"><code>&#91;root@ts02 ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=524032 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2096128, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2096128 to 4699136 # 拡張された！</code></pre>



<h3 class="wp-block-heading" id="手順6-完了">【手順6】完了！</h3>



<p>これでミッション完了です。dfコマンドでチェックしてみると、きちんと容量が追加されたことが確認できます。</p>



<pre class="wp-block-code"><code>&#91;root@ts02 ~]# df -h
ファイルシス            サイズ  使用  残り 使用% マウント位置
devtmpfs                  908M     0  908M    0% /dev
tmpfs                     920M     0  920M    0% /dev/shm
tmpfs                     920M  8.5M  911M    1% /run
tmpfs                     920M     0  920M    0% /sys/fs/cgroup
/dev/mapper/centos-root    18G  7.8G   11G   44% / # ふえた！！
/dev/sda1                1014M  178M  837M   18% /boot
tmpfs                     184M     0  184M    0% /run/user/0</code></pre>



<h2 class="wp-block-heading" id="まとめ">まとめ</h2>



<p>仮想マシンはラフに使えることもあり、気づいたら容量がパンパンになっていた（汗）ということがありがちです。</p>



<p>当記事で紹介した手順通りやれば、かんたんに容量を拡張することができます。空きが足らなくなって困ったときは、参考にしていただければ幸いです。</p>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://takabus.com/tips/552/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
