LoginSignup
0
0

More than 1 year has passed since last update.

Cloud9で「fatal: unable to write new index file」というエラーが起きた時の解決策

Posted at

エラーにいたる経緯

私の場合は、Railsチュートリアルを実施していた際に以下のコマンドでエラーが発生しました。

$ git co -b [branch名]
fatal: unable to write new index file

試したこと

調べたところディスクの容量が不足しているようなので、ディスク容量を確認しました。

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            476M     0  476M   0% /dev
tmpfs            98M   11M   88M  11% /run
/dev/xvda1      9.7G  9.7G     0 100% /
tmpfs           490M     0  490M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           490M     0  490M   0% /sys/fs/cgroup
/dev/loop0       33M   33M     0 100% /snap/snapd/11841
/dev/loop1       56M   56M     0 100% /snap/core18/1997
/dev/loop2       99M   99M     0 100% /snap/core/11081
/dev/loop3       56M   56M     0 100% /snap/core18/2066
/dev/loop4      100M  100M     0 100% /snap/core/10958
/dev/loop5       34M   34M     0 100% /snap/amazon-ssm-agent/3552
/dev/loop6       33M   33M     0 100% /snap/snapd/11588
tmpfs            98M     0   98M   0% /run/user/1000

4行目の「/dev/xvda1」のAvialが0になっている。
ここが今回の原因だった。

ちなみに
ここからこの容量を消すことを考え試行錯誤したが解決しなかったので、別の方法を試すことにした。

解決策

今回は、Cloud9のディスク容量を増やすことによって、問題を解決した。
以下のサイトを参考にしました。
Cloud9のディスクサイズを拡張してみた

まず最初に「/resize.sh」を新規作成しました。

resize.sh
#!/bin/bash

# Specify the desired volume size in GiB as a command-line argument. If not specified, default to 20 GiB.
SIZE=${1:-20}

# Get the ID of the environment host Amazon EC2 instance.
INSTANCEID=$(curl http://169.254.169.254/latest/meta-data/instance-id)

# Get the ID of the Amazon EBS volume associated with the instance.
VOLUMEID=$(aws ec2 describe-instances \
  --instance-id $INSTANCEID \
  --query "Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId" \
  --output text)

# Resize the EBS volume.
aws ec2 modify-volume --volume-id $VOLUMEID --size $SIZE

# Wait for the resize to finish.
while [ \
  "$(aws ec2 describe-volumes-modifications \
    --volume-id $VOLUMEID \
    --filters Name=modification-state,Values="optimizing","completed" \
    --query "length(VolumesModifications)"\
    --output text)" != "1" ]; do
sleep 1
done

#Check if we're on an NVMe filesystem
if [ $(readlink -f /dev/xvda) = "/dev/xvda" ]
then
  # Rewrite the partition table so that the partition takes up all the space that it can.
  sudo growpart /dev/xvda 1

  # Expand the size of the file system.
  # Check if we are on AL2
  STR=$(cat /etc/os-release)
  SUB="VERSION_ID=\"2\""
  if [[ "$STR" == *"$SUB"* ]]
  then
    sudo xfs_growfs -d /
  else
    sudo resize2fs /dev/xvda1
  fi

else
  # Rewrite the partition table so that the partition takes up all the space that it can.
  sudo growpart /dev/nvme0n1 1

  # Expand the size of the file system.
  # Check if we're on AL2
  STR=$(cat /etc/os-release)
  SUB="VERSION_ID=\"2\""
  if [[ "$STR" == *"$SUB"* ]]
  then
    sudo xfs_growfs -d /
  else
    sudo resize2fs /dev/nvme0n1p1
  fi
fi

作成後、次のコマンドを実行しました。
何GBまで増やすか設定できるようですが、今回は参考に乗っ取り、30GBまで増やしてみます。

$ sh resize.sh 30
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    19  100    19    0     0   1461      0 --:--:-- --:--:-- --:--:--  1461
{
    "VolumeModification": {
        "VolumeId": "vol-0c596006687908c7b",
        "ModificationState": "modifying",
        "TargetSize": 30,
        "TargetIops": 100,
        "TargetVolumeType": "gp2",
        "TargetMultiAttachEnabled": false,
        "OriginalSize": 10,
        "OriginalIops": 100,
        "OriginalVolumeType": "gp2",
        "OriginalMultiAttachEnabled": false,
        "Progress": 0,
        "StartTime": "2021-05-21T03:09:30.000Z"
    }
}
CHANGED: partition=1 start=2048 old: size=20969439 end=20971487 new: size=62912479,end=62914527
resize.sh: 38: resize.sh: [[: not found
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/xvda1 is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 4
The filesystem on /dev/xvda1 is now 7864059 (4k) blocks long.

再度、ディスク容量を確認してみよう。

~/environment $ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            476M     0  476M   0% /dev
tmpfs            98M  772K   98M   1% /run
/dev/xvda1       30G  9.3G   20G  32% /
tmpfs           490M     0  490M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           490M     0  490M   0% /sys/fs/cgroup
/dev/loop0       33M   33M     0 100% /snap/snapd/11841
/dev/loop1       56M   56M     0 100% /snap/core18/1997
/dev/loop2       99M   99M     0 100% /snap/core/11081
/dev/loop3       56M   56M     0 100% /snap/core18/2066
/dev/loop4      100M  100M     0 100% /snap/core/10958
/dev/loop5       34M   34M     0 100% /snap/amazon-ssm-agent/3552
/dev/loop6       33M   33M     0 100% /snap/snapd/11588
tmpfs            98M     0   98M   0% /run/user/1000

ディスクが拡大されて、今度は問題なく最初のコマンドが実行できた。

終わりに

Railsチュートリアルで同じように問題が起きたときには、ぜひ参考にしていただきたいです。
今回の問題の解決には以下のサイトの力が大きいので感謝いたします。

Cloud9のディスクサイズを拡張してみた

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