LoginSignup
7
4

More than 5 years have passed since last update.

ディスクイメージのサイズを大きくする

Last updated at Posted at 2017-11-14

この記事は

ラズパイのSDイメージを弄っていたら、もとのイメージの空き領域が足りなくなったときのためのTipsです。
といっても、ほぼ先人の記事のディスクイメージのサイズを大きくする。ここでは3GB増やしてみる。の焼き直しです(sayounaraarigatouさん、ありがとうございます!)。

手順

not_enough.imgのrootfsパーティションを1GiB増やして、enough.imgを作ります。

# 空イメージファイルの作成
$ truncate -s 1GiB empty.img

# イメージファイルの結合
$ cat not_enough.img empty.img > enough.img

# 空きループバックデバイスの確認
$ losetup -f
/dev/loop0

$ sudo losetup /dev/loop0 enough.img

# ループバックデバイスの設定状況の確認
$ losetup -a
/dev/loop0: []: (enough.img)

# パーティションテーブルの確認
$ sudo fdisk -lu /dev/loop0
Disk /dev/loop0: 2.9 GiB, 3054501888 bytes, 5965824 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
Disklabel type: dos
Disk identifier: 0xd8cd040f

デバイス     起動  Start 最後から セクタ  Size Id タイプ
/dev/loop0p1        2048   133119  131072   64M  c W95 FAT32 (LBA)
/dev/loop0p2      133120  5965823 5832704  2.8G 83 Linux


# パーティション設定
$ sudo fdisk  /dev/loop0

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


コマンド (m でヘルプ): d
パーティション番号 (1,2, default 2): 2

Partition 2 has been deleted.

コマンド (m でヘルプ): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
パーティション番号 (2-4, default 2): 2
First sector (133120-5965823, default 133120): 
Last sector, +sectors or +size{K,M,G,T,P} (133120-5965823, default 5965823): 

Created a new partition 2 of type 'Linux' and of size 2.8 GiB.

コマンド (m でヘルプ): t
パーティション番号 (1,2, default 2): 2
Partition type (type L to list all types): 83

Changed type of partition 'Linux' to 'Linux'.

コマンド (m でヘルプ): p
Disk /dev/loop0: 2.9 GiB, 3054501888 bytes, 5965824 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
Disklabel type: dos
Disk identifier: 0xd8cd040f

デバイス     起動  Start 最後から セクタ  Size Id タイプ
/dev/loop0p1        2048   133119  131072   64M  c W95 FAT32 (LBA)
/dev/loop0p2      133120  5965823 5832704  2.8G 83 Linux

コマンド (m でヘルプ): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: 無効な引数です

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).


# ループバックデバイスの削除
$ sudo losetup -d /dev/loop0

# マウントして確認するとまだ増えていない
$ sudo mount -o loop,offset=68157440 enough.img work/mnt
$ df
/dev/loop0                    1805620    1789236          0 100% work/mnt

# resize2fsで増える
$ sudo resize2fs /dev/loop0
$ df
/dev/loop0                    2838548    1789236     901532  67% work/mnt

参考ページ

7
4
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
7
4