줄기세포

[Linux] 파티셔닝 및 마운트 (fdisk, lvm, mkfs) 본문

Linux

[Linux] 파티셔닝 및 마운트 (fdisk, lvm, mkfs)

줄기세포(Stem_Cell) 2021. 12. 13. 17:36

Linux에 Disk를 확인하고 파티셔닝하여 마운트하는 과정을 설명한다

 

A) 일련의 과정

1. 일반적으로 디스크를 마운트하기 까지 과정은 아래와 같다.

    파티셔닝 → 포맷팅 → 마운트

 

2. LVM이라면 아래와 같을 것이다.

    파티셔닝 (lvm 8e) → PV → VG → LV → 포맷팅 → 마운트

   LVM의 pv, vg, lv는 여기서 다루지 않을 것이다.

   추후 포스팅하면 링크를 걸어두겠다.

 

B) 파티셔닝

Disk를 서버에 연결하였으면 (클라우드던 물리적인 장비던), 조회를 하고 파티셔닝을 해준다.

 

1. fdisk -l 로 Disk와 파티션 정보 조회

[root@hans ~]# fdisk -l

Disk /dev/xvda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a74a5

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *        2048     1953791      975872   83  Linux
/dev/xvda2         1953792    41943039    19994624   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.5 GB, 18471714816 bytes, 36077568 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 2000 MB, 2000683008 bytes, 3907584 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/xvdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/xvdc: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xc31e7457

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdc1            2048   104857599    52427776   83  Linux
You have new mail in /var/spool/mail/root

 

2. fdisk /dev/xvdb 로 파티셔닝 진행

[root@hans ~]# fdisk /dev/xvdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-209715199, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): 
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

 

fdisk -l 로 조회시 다음과 같이 나옵니다.

   System에 Linux로 나오는 것을 확인합니다.

Disk /dev/xvdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x76755307

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdb1            2048   209715199   104856576   83  Linux

 

 

참고로 파티셔닝 삭제하는 방법은 다음과 같습니다.

[root@hans ~]# fdisk /dev/xvdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@hans ~]# fdisk -l

Disk /dev/xvdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x76755307

    Device Boot      Start         End      Blocks   Id  System

 

참고로 LVM을 사용하기 위해서 disk를 파티셔닝할때는 아래와 같이 진행합니다.

[root@hans ~]# fdisk /dev/xvdb
Welcome to fdisk (util-linux 2.23.2).

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
Building a new DOS disklabel with disk identifier 0x76755307.

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n
Partition type: 
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-209715199, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): 
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 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 C:  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 eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.



Disk /dev/xvdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x76755307

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdb1            2048   209715199   104856576   8e  Linux LVM

 

System에 Linux LVM으로 파티셔닝 되었는지 확인합니다.

 

C) 포매팅

1. mkfs 명령어를 통해 포매팅합니다.

xfs와 ext4 이외에도 Linux에는 여러 Disk Type이 존재하는데, 아래 블로그 참조하여 선택하시면 됩니다.

https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=hymne&logNo=220976678541 

 

포스팅에선 xfs로 진행하였습니다.

[root@hans ~]# mkfs.xfs /dev/xvdb1
meta-data=/dev/xvdb1             isize=512    agcount=4, agsize=6553536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=26214144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=12799, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

파티셔닝된 xvdb1가 아니라 xvdb를 포매팅하면 아래와 같은 에러를 리턴합니다.

[root@hans ~]# mkfs.xfs /dev/xvdb
mkfs.xfs: /dev/xvdb appears to contain a partition table (dos).
mkfs.xfs: Use the -f option to force overwrite.

참조 mkfs help

[root@hans ~]# mkfs -h
Usage:
 mkfs [options] [-t <type>] [fs-options] <device> [<size>]

Options:
 -t, --type=<type>  filesystem type; when unspecified, ext2 is used
     fs-options     parameters for the real filesystem builder
     <device>       path to the device to be used
     <size>         number of blocks to be used on the device
 -V, --verbose      explain what is being done;
                      specifying -V more than once will cause a dry-run
 -V, --version      display version information and exit;
                      -V as --version must be the only option
 -h, --help         display this help text and exit

For more information see mkfs(8).

 

D) 마운트

1. mount할 경로를 생성하고, mount 해줍니다.

mkdir /mount_test

[root@hans ~]# mount /dev/xvdb1 /mount-test
[root@hans ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  7.8G     0  7.8G   0% /dev
tmpfs                   tmpfs     7.8G     0  7.8G   0% /dev/shm
tmpfs                   tmpfs     7.8G  185M  7.6G   3% /run
tmpfs                   tmpfs     7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        18G  3.9G   14G  23% /
/dev/xvda1              xfs       950M  196M  755M  21% /boot
tmpfs                   tmpfs     1.6G     0  1.6G   0% /run/user/0
/dev/xvdb1              xfs       100G   33M  100G   1% /mount-test

언마운트는 umount 명령어로 진행합니다.

[root@hans ~]# umount /mount-test
[root@hans ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  7.8G     0  7.8G   0% /dev
tmpfs                   tmpfs     7.8G     0  7.8G   0% /dev/shm
tmpfs                   tmpfs     7.8G  185M  7.6G   3% /run
tmpfs                   tmpfs     7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        18G  3.9G   14G  23% /
/dev/xvda1              xfs       950M  196M  755M  21% /boot
tmpfs                   tmpfs     1.6G     0  1.6G   0% /run/user/0

 

 2. fstab에 등록

    mount 명령어로만 마운트를 진행하면, 서버 재기동 하면 마운트가 풀립니다.

    서버 재기동하여도 마운트 되어 있을 수 있게, /etc/fstab에 등록해줍니다.

[root@hans ~]# vi /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu May 13 17:14:36 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=c8f72fb1-e594-4812-8c93-88361720f629 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

#--------------------------------------------------------------------------------
/dev/xvdb1                     /mount-test    xfs     defaults,nofail 0 2

:wq

fstab에 등록할 때 순서와 띄어쓰기를 잘 지켜줘야 합니다.

[Device]    [Mount Point]    [File System Type]    [Mount Option]    [Backup Operation]    [File System Check Order]

Device: mount할 Device명 or UUID
Mount Point:  mount 할 경로
File System Type xfs, ext4, ext3 등
Option 옵션에 대해서는 아래 블로그 참조
Backup Operation 0: 백업안함
1: dump로 backup
File System Check Order 0: 체크하지 않음
1: root file system 체크 설정
2: 다른 파일 시스템 설정

 

Option 참조 블로그: https://storyerp.tistory.com/41

 

fstab에 작성된대로 mount를 진행하려면 mount 명령어에 옵션 a로 진행합니다.

mount -a

[root@hans ~]# mount -a

 

그 외 참조:

https://blog.dasomoli.org/linux-fstab%EC%9D%98-%EA%B5%AC%EC%A1%B0%EC%99%80-%EC%98%B5%EC%85%98/

Comments