LoginSignup
0
0

More than 1 year has passed since last update.

GCEの追加ディスクをパーティション分割する方法

Posted at

はじめに

業務にて、GCEを利用する際に追加ディスクをパーティションする必要があったので備忘録としてこの記事を書きました。

環境

今回サンプルとして用意したGCE環境は下記の通りです。

  • GCE : e2-medium
  • OS : Ubuntu18.04
  • OS Disk : 10GB
  • 追加ディスク : 100GB(今回はこれを50GB*2に分割します)

LVMをインストール

LVM2をインストールします。
今回の環境はUbuntuですが、業務ではRHEL系のOSでも実施した為、両方のコマンドを載せます。

Debian/Ubuntu系の場合

下記コマンドを実行

$ sudo apt update && sudo apt install -y lvm2

RHEL/CentOS系の場合

下記コマンドを実行

$ sudo yum update && sudo yum -y install lvm2

パーティション分割

GCEの場合、コンソール、CLIから追加ディスクを設定し、GCP側で自動でディスクがVMにアタッチされますが、念のため最初に現状のディスク構成を確認しておきましょう。

#パーティション分け状況を確認するコマンド
$ sudo fdisk -l 
=================実行結果===================
Disk /dev/sda: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 56E8A753-9F88-4C34-8A28-FABD3B789010

Device      Start      End  Sectors  Size Type
/dev/sda1  227328 20971486 20744159  9.9G Linux filesystem

#現在利用出来るブロックデバイスを確認するコマンド
$ sudo lsblk
NAME    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda       8:0    0    10G  0 disk 
├─sda1    8:1    0   9.9G  0 part /
├─sda14   8:14   0     4M  0 part 
└─sda15   8:15   0   106M  0 part /boot/efi
sdb       8:16   0   100G  0 disk 

上記実行結果を見ると、追加ディスクの100GBはパーティション分割されておらず、どこにもアタッチされていないことがわかりますね。
また、VM上100GBの追加ディスクはOS上「sdb」という名前で認識されてることが分かります。

では、ここからパーティション分割をしていきましょう。
パーティション分割は下記コマンドを実行します。

$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x04328689.

Command (m for help): m  <=mを入力することでメニューを表示させることが可能

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help): n  <=新しくパーティションを作成するのでnを入力
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p  <=primaryを選択
Partition number (1-4, default 1): 1  <=パーティション番号を選択(基本的に低い数字を選択するのでいいと思います)
First sector (2048-209715199, default 2048):   <=基本的にそのままEnterでいいと思います。
Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199): +50G  <=分割サイズを入力(+50Gと入力することで50GiB分の容量が確保されます)

Created a new partition 1 of type 'Linux' and of size 50 GiB.

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

Command (m for help): t  <=分割したパーティションタイプを変更します
Selected partition 1  <=Partition Numberで指定した番号を入力
Hex code (type L to list all codes): L  <=Lを入力したことでパーティションタイプの一覧を表示させることが出来ます

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE 
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT            
Hex code (type L to list all codes): 8e  <=今回はLVMに設定したいため8eを選択(ここは16進数で入力する必要があります)
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): w  <= パーティション設定が完了となるのでwを入力して保存終了する
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

今回は50GB*2でパーティションを分割したいのでこの操作をもう一度実行します。

実行後、lsblkコマンドにてディスクの状態を確認してみましょう。

$ sudo lsblk
NAME    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda       8:0    0    10G  0 disk 
├─sda1    8:1    0   9.9G  0 part /
├─sda14   8:14   0     4M  0 part 
└─sda15   8:15   0   106M  0 part /boot/efi
sdb       8:16   0   100G  0 disk 
├─sdb1    8:17   0    50G  0 part 
└─sdb2    8:18   0    50G  0 part 

無事分割されていることがわかりますね!
以上が、パーティション分割方法となります。

後は、下記項目を実施することでパーティション分割したディスクを利用することが可能になります。

  • 指定の形式にディスクをフォーマット
  • 指定pathにディスクをマウント
  • GCE起動時の自動マウント設定

長くなってしまったのでここまでにしますが、後続設定については後日投稿しようかなと思います。(気が向いたら)

本記事を読んでいただきありがとうございました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0