Intro

Partition이라는게 애초의 할당된 용량이 초과한다거나, 혹은 img 파일을 restore하는 바람에 가용 용량을 다 사용하지 못하는 경우가 발생됩니다. 이러한 경우, Resize가 필요한데 이게 생각보다 쉽지 않습니다. 그래서 오늘은, 대표적인 명령어인 parted와 fdisk를 이용하여 파티션을 늘리기를 소개합니다.

오늘 같이 살펴볼 예는 다음과 같습니다.

microSD card(32G)을 img(4G)파일로 덮어 썼습니다. img파일을 제작할 당시에, 사용된 partition 정보가 SD카드에도 그대로 전달되기 때문에 4G로 잡히고 28G는 사용할 수 없게 됩니다.

이런 경우, 어떻게 resize를 할지 알아보겠습니다.

df -h , fdisk -l

먼저, 디스크의 상태를 알 필요가 있습니다. 용량이 어떻게 할당되었는지 알기 위해서는, 다음과 같은 명령어를 이용합니다.

[root@ambari ~]# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/mapper/vg_ambari-lv_root  396G   13G  363G   4% /
tmpfs                          7.8G     0  7.8G   0% /dev/shm
/dev/sda1                      485M   40M  420M   9% /boot
/dev/mapper/vg_ambari-lv_home   55G  180M   52G   1% /home

-h 옵션은 human readable format으로, 읽기 쉽게 표현한 형태로 출력하게 됩니다. 현재, linux에 잡혀있는 모든 block storage에 대한 정보를 보여줍니다. 애석하게도 df는 mounted된 block device만 표시합니다. 따라서, umount 상태라면, fdisk를 이용해야 합니다.

[root@ambari ~]# fdisk -l

Disk /dev/sdc: 31.4 GB, 31440502784 bytes
64 heads, 32 sectors/track, 29984 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b5098

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               5          60       57344    c  W95 FAT32 (LBA)
Partition 1 does not end on cylinder boundary.
/dev/sdc2              61        2825     2831360   83  Linux
Partition 2 does not end on cylinder boundary.

오늘 resize를 진행할 parition은 /dev/sdc2입니다. human-readable이 아니기에, mount하여 df로 다시 살펴 보면 다음과 같습니다.

[root@ambari ~]# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/mapper/vg_ambari-lv_root  396G   13G  363G   4% /
tmpfs                          7.8G     0  7.8G   0% /dev/shm
/dev/sda1                      485M   40M  420M   9% /boot
/dev/mapper/vg_ambari-lv_home   55G  180M   52G   1% /home
/dev/sdc2                      2.7G  2.1G  468M  82% /mnt

32G짜리 MicroSD가 단 3G로 나타나고 있습니다. 이를 어찌한다...

parted

가장, 간편하게 사용할 수 있습니다. 기본적으로 다음과 같이 접근합니다.

[root@ambari ~] parted /dev/sdc # 각자 해당하는 장치 이름을 사용합니다.
GNU Parted 2.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p # 해당 디바이스의 파티션 정보를 표시합니다(print) 
Model: Generic STORAGE DEVICE (scsi)
Disk /dev/sdc: 31.4GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      4194kB  62.9MB  58.7MB  primary  fat16        lba
 2      62.9MB  2962MB  2899MB  primary  ext4

(parted) resize 2 # 변경할 파티션을 선택합니다.
WARNING: you are attempting to use parted to operate on (resize) a file system.
parted's file system manipulation code is not as robust as what you'll find in
dedicated, file-system-specific packages like e2fsprogs.  We recommend
you use parted only to manipulate partition tables, whenever possible.
Support for performing most operations on most types of file systems
will be removed in an upcoming release.
Start?  [62.9MB]? # 사이즈를 알아서 잡아줍니다.
End?  [2962MB]? 
No Implementation: Support for opening ext4 file systems is not implemented yet.

(parted) q # quit, 종료합니다.

간략하게 사용할 수 있지만, 마지막 2번째 줄을 살펴보면 ext4가 지원되지 않음을 알 수 있습니다. ext4는 parted를 사용할 수 없습니다. 그럼 어떻게 해야 할까요?

fdisk

fdisk를 사용할 차례가 왔습니다. parted 보다 약간 복잡할 뿐이지만, 차근차근 진행하시면 쉽게 사용할 수 있습니다. 다음과 같은 순서로 진행합니다.

  1. fdisk로 partition 정보 변경하기
    1. 기존의 Partition 삭제하기
    2. 새로 Partition 정의하기
    3. 저장하고 종료
  2. e2fsck을 이용하여 file system check하기
  3. resize2fs로 실제 영역 확장하기

적다 보니, 복잡해 보이지만 대충 후루륵 끝낼만한 간단한 일입니다.

[root@ambari ~] fdisk /dev/sdc

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p # print, 파티션 정보를 출력

Disk /dev/sdc: 31.4 GB, 31440502784 bytes
64 heads, 32 sectors/track, 29984 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b5098

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               5          60       57344    c  W95 FAT32 (LBA)
Partition 1 does not end on cylinder boundary.
/dev/sdc2              61        2825     2831360   83  Linux
Partition 2 does not end on cylinder boundary.

Command (m for help): d # 조절할 파티션을 삭제합니다.
Partition number (1-4): 2 # 순서에 따라 2번이 되겠네요.

Command (m for help): n # 새로운 파티션을 추가합니다.
Command action
   e   extended
   p   primary partition (1-4)
p # primary partition으로 생성합니다.
Partition number (1-4): 2 # 삭제했던 2번으로 설정하고
First cylinder (1-29984, default 1): 61 # 시작 위치는 원래의 위치와 동일하게 줍니다.
Last cylinder, +cylinders or +size{K,M,G} (61-29984, default 29984): # default가 남은 공간 전체
Using default value 29984

Command (m for help): w # 저장하기
The partition table has been altered!

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

파티션이 변경되었기 때문에, 파일시스템의 이상 유무를 점검해야 합니다. 그리고 실제 영역을 반영해야 합니다. 이는 다음의 명령어로 진행합니다.

[root@ambari ~] e2fsck -f /dev/sdc2 # 파일 시스템을 점검합니다. 
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdc2: 76726/177056 files (0.2% non-contiguous), 552880/707840 blocks
[root@ambari ~]# resize2fs /dev/sdc2 # 영역을 늘릴 차례입니다.
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/sdc2 to 7660544 (4k) blocks.
The filesystem on /dev/sdc2 is now 7660544 blocks long.

이로써, resize가 끝났습니다. 정말, 간단하죠.

잘 되었는지 확인할 필요가 있겠죠. mount하여 df를 이용해서 Size를 체크해 보겠습니다.

[root@ambari ~] mount /dev/sdc2 /mnt # 일단, 마운트를 시킵니다.
[root@ambari ~] df -h # 보기 편하게 출력해 보면,
Filesystem                     Size  Used Avail Use% Mounted on
/dev/mapper/vg_ambari-lv_root  396G   13G  363G   4% /
tmpfs                          7.8G     0  7.8G   0% /dev/shm
/dev/sda1                      485M   40M  420M   9% /boot
/dev/mapper/vg_ambari-lv_home   55G  180M   52G   1% /home
/dev/sdc2                       29G  2.1G   26G   8% /mnt

29G로 정상적으로 resize가 되었습니다.

Closing

간단한 내용이지만, 계속 잊어 먹고 다시 찾는 일이 많아서 정리해 보았습니다. 생각보다 어렵지 않은데, runtime에서 변경하는 일은 어렵습니다. 생각해 보시면, Mount된 상태에서 resize가 되겠어요? live-cd나 다른 컴퓨터에 물려서 변경하는 것이 정신 건강에 좋습니다.