メニュー
Amazon Elastic Compute Cloud
Linux インスタンス用ユーザーガイド

Linux パーティションを拡張する

一部の Amazon EC2 ルートボリュームやスナップショットから復元されたボリュームには、ファイルシステムおよびデータを実際に保持するパーティションが含まれています。ボリュームをコンテナと考えれば、パーティションはボリューム内のもう 1 つのコンテナであり、データはパーティション上に存在します。ボリュームのサイズを拡大しても、パーティションは拡大されません。拡大したボリュームを活用するには、パーティションを新しいサイズに拡張する必要があります。

注記

スナップショットから復元したすべてのボリュームがパーティション分割されるわけではないので、この手順は、お使いのボリュームに適用されないことがあります。ボリュームのファイルシステムのサイズを使用できる最大の容量に変更する必要があります。サイズを変更する必要があるパーティションがボリューム内にあるかどうか不明な場合は、「To check if your volume partition needs resizing」で詳細を参照してください。

拡張するパーティションがルートパーティションではない場合、単にこのパーティションをアンマウントし、インスタンス自体からパーティションのサイズを変更することができます。サイズを変更する必要のあるパーティションがインスタンスのルートパーティションである場合、実行中のインスタンスのルートパーティションをアンマウントすることはできないため、プロセスはより複雑になります。セカンダリインスタンスと呼ばれる別のインスタンスに対して、以下の手順を実行する必要があります。

重要

以下の手順は Amazon Linux に関する説明であり、Amazon Linux でテスト済みです。その他のツールセットやツールバージョンによるディストリビューションは、異なる動作をする場合があります。

拡張するために Linux ルートパーティションを準備する

インスタンスのルートパーティションを拡張するために、次のいくつかのステップを実行する必要があります。拡張する必要があるパーティションがルートパーティションではない場合、この手順を行う必要はありません。

To prepare a Linux root partition for expansion

  1. If your primary instance is running, stop it. You cannot perform the rest of this procedure on a running instance. For more information, see インスタンスの停止と起動.

  2. Check the integrity of your volume. File-system corruption that is picked up by the snapshot may render a restored root volume unbootable.

  3. Take a snapshot of your volume. It can be easy to corrupt or lose your data in the following procedures. If you have a fresh snapshot, you can always start over in case of a mistake and your data will still be safe. For more information, see Amazon EBS スナップショットの作成.

  4. Record the device name that the volume is attached to. You can find this information on the Root device field of the instance's details pane. The value is likely /dev/sda1 or /dev/xvda.

  5. Detach the volume from the primary instance. For more information, see インスタンスからの Amazon EBS ボリュームのデタッチ.

  6. Attach the volume to another (secondary) instance in the same Availability Zone. For more information, see インスタンスへの Amazon EBS ボリュームのアタッチ. If your EBS volume is encrypted, you must use a secondary instance that supports Amazon EBS 暗号化; otherwise, you can use a t2.micro instance for this procedure. For more information, see サポートされるインスタンスタイプ. If you do not already have a secondary instance, you will need to launch one. For more information, see インスタンスの作成.

    重要

    The secondary instance must be running when you attach the volume, and you should not reboot the secondary instance while multiple root volumes are attached; booting an instance with multiple root volumes attached could cause the instance to boot to the wrong volume.

  7. Log in to the secondary instance with SSH. For more information, see Linux インスタンスへの接続. Continue with the next procedure.

parted を使用して Linux パーティションを拡張する

parted ユーティリティは、ほとんどの Linux ディストリビューションで使用できるパーティション編集ツールです。MBR パーティションテーブルと GPT パーティションテーブルの両方を作成し編集することができます。parted の一部のバージョン (バージョン 2.1 より前) は GPT パーティションテーブルのサポートが限定的で、parted のそのバージョンを使用してブートボリュームを変更した場合、ブート時に問題が発生することがあります。parted のバージョンは、parted --version コマンドを使用して確認できます。

GPT パーティション形式のデバイスにあるパーティションを拡張する場合は、代わりに gdisk ユーティリティを使用してください。ボリュームで使用されているディスクラベルタイプが不明な場合は、sudo fdisk -l コマンドを使用して確認できます。詳細については、「To expand a Linux partition using gdisk」を参照してください。

To expand a Linux partition using parted

If the partition you need to expand is the root partition, be sure to follow the steps in To prepare a Linux root partition for expansion first.

  1. Identify the device that contains the partition that you want to expand. Use the lsblk command to list all devices and partitions attached to the instance.

    Copy
    [ec2-user ~]$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvdf 202:80 0 100G 0 disk └─xvdf1 202:81 0 8G 0 part /mnt xvda1 202:1 0 30G 0 disk /

    In this example, the xvdf device has 100 GiB of available storage and it contains an 8 GiB partition.

  2. Unmount the partition if it is mounted. Run the umount command with the value of MOUNTPOINT from the lsblk command. In this example, the MOUNTPOINT value for the partition is /mnt.

    Copy
    [ec2-user ~]$ sudo umount /mnt
  3. Take a snapshot of your volume (unless you just took one in the previous procedure). It can be easy to corrupt or lose your data in the following procedures. If you have a fresh snapshot, you can always start over in case of a mistake and your data will still be safe. For more information, see Amazon EBS スナップショットの作成.

  4. Run the parted command on the device (and not the partition on the device). Remember to add the /dev/ prefix to the name that lsblk outputs.

    Copy
    [ec2-user ~]$ sudo parted /dev/xvdf GNU Parted 2.1 Using /dev/xvdf Welcome to GNU Parted! Type 'help' to view a list of commands.
  5. Change the parted units of measure to sectors.

    Copy
    (parted) unit s
  6. Run the print command to list the partitions on the device. For certain partition table types, you might be prompted to repair the partition table for the larger volume size. Answer 'Ignore' to any questions about fixing the existing partition table; you will create a new table later.

    Copy
    (parted) print
    1. If you receive the following message, enter 'Ignore' to prevent the backup GPT location from changing.

      Error: The backup GPT table is not at the end of the disk, as it should be.  This might mean that another operating
      system believes the disk is smaller.  Fix, by moving the backup to the end (and removing the old backup)?
      Fix/Ignore/Cancel? Ignore
    2. If you receive the following message, enter 'Ignore' again to keep the space on the drive the same.

      Warning: Not all of the space available to /dev/xvdf appears to be used, you can fix the GPT to use all of the
      space (an extra 46137344 blocks) or continue with the current setting?
      Fix/Ignore? Ignore
  7. Examine the output for the total size of the disk, the partition table type, the number of the partition, the start point of the partition, and any flags, such as boot. For gpt partition tables, note the name of the partition; for msdos partition tables, note the Type field (primary or extended). These values are used in the upcoming steps.

    The following is a gpt partition table example.

    Model: Xen Virtual Block Device (xvd)
    Disk /dev/xvdf: 209715200s
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    
    Number  Start  End        Size       File system  Name                 Flags
    128     2048s  4095s      2048s                   BIOS Boot Partition  bios_grub
     1      4096s  16777182s  16773087s  ext4         Linux

    The following is an msdos partition table example.

    Model: Xen Virtual Block Device (xvd)
    Disk /dev/xvdg: 104857600s
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    
    Number  Start  End        Size       Type     File system  Flags
     1      2048s  35649535s  35647488s  primary  ext3
  8. Delete the partition entry for the partition using the number (1) from the previous step.

    Copy
    (parted) rm 1
  9. Create a new partition that extends to the end of the volume.

    (For the gpt partition table example) Note the start point and name of partition 1 above. For the gpt example, there is a start point of 4096s, and the name Linux. Run the mkpart command with the start point of partition 1, the name, and 100% to use all of the available space.

    Copy
    (parted) mkpart Linux 4096s 100%

    (For the msdos partition table example) Note the start point and the partition type of partition 1 above. For the msdos example, there is a start point of 2048s and a partition type of primary. Run the mkpart command with a primary partition type, the start point of partition 1, and 100% to use all of the available space.

    Copy
    (parted) mkpart primary 2048s 100%
  10. Run the print command again to verify your partition.

    (For the gpt partition table example)

    Copy
    (parted) print Model: Xen Virtual Block Device (xvd) Disk /dev/xvdf: 209715200s Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags 128 2048s 4095s 2048s BIOS Boot Partition bios_grub 1 4096s 209713151s 209709056s ext4 Linux

    (For the msdos partition table example)

    Copy
    (parted) print Model: Xen Virtual Block Device (xvd) Disk /dev/xvdg: 104857600s Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 2048s 104857599s 104855552s primary ext3
  11. Check to see that any flags that were present earlier are still present for the partition that you expanded. In some cases the boot flag may be lost. If a flag was dropped from the partition when it was expanded, add the flag with the following command, substituting your partition number and the flag name. For example, the following command adds the boot flag to partition 1.

    Copy
    (parted) set 1 boot on

    You can run the print command again to verify your change.

  12. Run the quit command to exit parted.

    Copy
    (parted) quit

    注記

    Because you removed a partition and added a partition, parted may warn that you may need to update /etc/fstab. This is only required if the partition number changes.

  13. Check the file system to make sure there are no errors (this is required before you may extend the file system). Note the file system type from the previous print commands. Choose one of the commands below based on your file system type; if you are using a different file system, consult the documentation for that file system to determine the correct check command.

    (For ext3 or ext4 file systems)

    Copy
    [ec2-user ~]$ sudo e2fsck -f /dev/xvdf1 e2fsck 1.42.3 (14-May-2012) 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 /: 31568/524288 files (0.4% non-contiguous), 266685/2096635 blocks

    (For xfs file systems)

    Copy
    [ec2-user ~]$ sudo xfs_repair /dev/xvdf1 Phase 1 - find and verify superblock... Phase 2 - using internal log - zero log... - scan filesystem freespace and inode maps... - found root inode chunk Phase 3 - for each AG... - scan and clear agi unlinked lists... - process known inodes and perform inode discovery... - agno = 0 - agno = 1 - agno = 2 - agno = 3 - process newly discovered inodes... Phase 4 - check for duplicate blocks... - setting up duplicate extent list... - check for inodes claiming duplicate blocks... - agno = 0 - agno = 1 - agno = 2 - agno = 3 Phase 5 - rebuild AG headers and trees... - reset superblock... Phase 6 - check inode connectivity... - resetting contents of realtime bitmap and summary inodes - traversing filesystem ... - traversal finished ... - moving disconnected inodes to lost+found ... Phase 7 - verify and correct link counts... done
  14. The next steps differ depending on whether the expanded partition belongs on the current instance or if it is the root partition for another instance.

gdisk を使用して Linux パーティションを拡張する

gdisk ユーティリティ (GPT fdisk と呼ばれることもあります) は、パーティションテーブルをメニュー形式で作成し編集するテキストベースのツールです。一部のディストリビューションでは、parted コマンドよりも GPT パーティションテーブルのサポートが優れています。一般的な多くの Linux ディストリビューション (Amazon Linux や Ubuntu など) には、デフォルトで gdisk が用意されています。使用しているディストリビューションに gdisk コマンドが用意されていない場合は、Obtaining GPT fdisk で入手方法を知ることができます。多くの場合、Amazon Linux インスタンスを起動してセカンダリインスタンスとして使用する方が、gdisk コマンドが既に用意されているため簡単です。

To expand a Linux partition using gdisk

If the partition you need to expand is the root partition, be sure to follow the steps in To prepare a Linux root partition for expansion first.

  1. Identify the device that contains the partition that you want to expand. Use the lsblk command to list all devices and partitions attached to the instance.

    Copy
    [ec2-user ~]$ lsblk NAME MAJ:MIN RM SIZE RO MOUNTPOINT xvdf 202:80 0 100G 0 xvdf1 202:81 0 9.9G 0 /mnt xvda1 202:1 0 30G 0 /

    In this example, the xvdf device has 100 GiB of available storage and it contains a 9.9 GiB partition.

  2. Unmount the partition if it is mounted. Run the umount command with the value of MOUNTPOINT from the lsblk command. In this example, the MOUNTPOINT value for the partition is /mnt.

    Copy
    [ec2-user ~]$ sudo umount /mnt
  3. Take a snapshot of your volume (unless you just took one in the previous procedure). It can be easy to corrupt or lose your data in the following procedures. If you have a fresh snapshot, you can always start over in case of a mistake and your data will still be safe. For more information, see Amazon EBS スナップショットの作成.

  4. Run the gdisk command on the device (and not the partition on the device). Remember to add the /dev/ prefix to the name that lsblk outputs.

    Copy
    [ec2-user ~]$ sudo gdisk /dev/xvdf gdisk /dev/xvdf GPT fdisk (gdisk) version 0.8.10 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT.
  5. Run the p command to print the partition table for the device.

  6. Examine the output for the disk identifier, partition number, starting sector, code for the partition, and name of the partition. If your volume has multiple partitions, take note of each one.

    Command (? for help): p
    Disk /dev/xvdf: 209715200 sectors, 100.0 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): 947F4655-F3BF-4A1F-8203-000000000000
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 20705246
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 2108 sectors (1.0 MiB)
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048        20705152   9.9 GiB     EF00  lxroot

    In the above example the disk identifier is 947F4655-F3BF-4A1F-8203-000000000000, the partition number is 1, the starting sector is 2048, the code is EF00, and the name is lxroot. Your values will vary.

  7. Because the existing partition table was originally created for a smaller volume, you need to create a new partition table for the larger volume. Run the o command to create a new, empty partition table.

    Command (? for help): o
    This option deletes all partitions and creates a new protective MBR.
    Proceed? (Y/N): Y
  8. Use the n command to create a new partition entry for each partition on the device.

    • If your volume has only one partition, at each prompt, enter the values that you recorded earlier. For the last sector value, use the default value to expand to the entire volume size.

      Command (? for help): n
      Partition number (1-128, default 1): 1
      First sector (34-209715166, default = 2048) or {+-}size{KMGTP}: 2048
      Last sector (2048-209715166, default = 209715166) or {+-}size{KMGTP}: 209715166
      Current type is 'Linux filesystem'
      Hex code or GUID (L to show codes, Enter = 8300): EF00
      Changed type of partition to 'EFI System'
    • If your volume has more than one partition, there is likely a BIOS boot partition, and a main data partition. Create a new partition entry for the BIOS boot partition using the values that you recorded earlier. Create another new partition entry for the main data partition using the values that you recorded earlier, but for the last sector value, use the default value to expand to the entire volume size.

      Command (? for help): n
      Partition number (1-128, default 1): 1
      First sector (34-209715166, default = 2048) or {+-}size{KMGTP}:  2048
      Last sector (2048-209715166, default = 209715166) or {+-}size{KMGTP}: 4095
      Current type is 'Linux filesystem'
      Hex code or GUID (L to show codes, Enter = 8300): EF02
      Changed type of partition to 'BIOS boot partition'
      
      Command (? for help): n
      Partition number (2-128, default 2): 2
      First sector (34-209715166, default = 4096) or {+-}size{KMGTP}: 4096
      Last sector (4096-209715166, default = 209715166) or {+-}size{KMGTP}: 209715166
      Current type is 'Linux filesystem'
      Hex code or GUID (L to show codes, Enter = 8300): 0700
      Changed type of partition to 'Microsoft basic data'
  9. Use the c command to change the name of each partition to the name of the previous partition. If your partition did not have a name, simply type Enter.

    Command (? for help): c
    Using 1
    Enter name: lxroot
  10. Use the x command to enter the expert command menu.

  11. Use the g command to change the disk identifier to the original value.

    Expert command (? for help): g
    Enter the disk's unique GUID ('R' to randomize): 947F4655-F3BF-4A1F-8203-A7B30C2A4425
    The new disk GUID is 947F4655-F3BF-4A1F-8203-A7B30C2A4425
  12. Use the w command to write the changes to the device and exit.

    Expert command (? for help): w
    
    Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
    PARTITIONS!!
    
    Do you want to proceed? (Y/N): Y
    OK; writing new GUID partition table (GPT) to /dev/xvdf.
    The operation has completed successfully.
  13. Check the file system to make sure there are no errors (this is required before you may extend the file system).

    1. Find the file system type with the following command, substituting the partition you just expanded (this may be /dev/xvdf2 if your volume had multiple partitions).

      Copy
      [ec2-user ~]$ sudo file -sL /dev/xvdf1
    2. Choose one of the commands below based on your file system type; if you are using a different file system, consult the documentation for that file system to determine the correct check command.

      (For ext3 or ext4 file systems)

      Copy
      [ec2-user ~]$ sudo e2fsck -f /dev/xvdf1 e2fsck 1.42.3 (14-May-2012) 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 /: 31568/524288 files (0.4% non-contiguous), 266685/2096635 blocks

      (For xfs file systems)

      注記

      You may need to install the xfsprogs package to work with XFS file systems. Use the following command to add XFS support to your Amazon Linux instance.

      Copy
      [ec2-user ~]$ sudo yum install -y xfsprogs
      Copy
      [ec2-user ~]$ sudo xfs_repair /dev/xvdf1 Phase 1 - find and verify superblock... Phase 2 - using internal log - zero log... - scan filesystem freespace and inode maps... - found root inode chunk Phase 3 - for each AG... - scan and clear agi unlinked lists... - process known inodes and perform inode discovery... - agno = 0 - agno = 1 - agno = 2 - agno = 3 - process newly discovered inodes... Phase 4 - check for duplicate blocks... - setting up duplicate extent list... - check for inodes claiming duplicate blocks... - agno = 0 - agno = 1 - agno = 2 - agno = 3 Phase 5 - rebuild AG headers and trees... - reset superblock... Phase 6 - check inode connectivity... - resetting contents of realtime bitmap and summary inodes - traversing filesystem ... - traversal finished ... - moving disconnected inodes to lost+found ... Phase 7 - verify and correct link counts... done
  14. The next steps differ depending on whether the expanded partition belongs on the current instance or if it is the root partition for another instance.

拡張したパーティションを元のインスタンスに戻す

別のインスタンスのルートパーティションを拡張した場合は、この手順に従って元のインスタンスにボリュームを戻します。

To return an expanded root partition to its original instance

  1. Detach the expanded partition from its secondary instance. For more information, see インスタンスからの Amazon EBS ボリュームのデタッチ.

  2. Reattach the volume to the primary instance using the device name that you identified in ステップ 4 of the preparation procedure. For more information, see インスタンスへの Amazon EBS ボリュームのアタッチ.

  3. Start the primary instance. For more information, see インスタンスの停止と起動.

  4. (Optional) If you launched a secondary instance for the sole purpose of expanding the partition, you can terminate the instance to stop incurring charges. For more information, see インスタンスの終了.

  5. Connect to your primary instance and extend the file system to use the newly available space by following the procedures in ボリュームサイズ変更後の Linux ファイルシステムの拡張.

このファイルシステムの拡張が終了したら、インスタンスから、目的のパーティションサイズを持つ新しいインスタンスを起動するために使用できる AMI を作成できます。詳細については、「Amazon マシンイメージ (AMI) 」を参照してください。