created: 2023-04-24T00:54:42.863Z

RaspberryPiにて、USBデバイスを初期化してマウントできるようにするまで

現状確認

USBディスクに(sda)に工場出荷時のパーティションがあるのでそれを削除する。

$ sudo lsblk --fs
NAME        FSTYPE FSVER LABEL  UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sda
└─sda1      vfat   FAT32        4576-D805
mmcblk0
├─mmcblk0p1 vfat   FAT32 boot   AE82-4BC1                             204.1M    20% /boot
└─mmcblk0p2 ext4   1.0   rootfs 6d2ff93e-eacd-415c-96d5-4611ad21e05f   11.3G    57% /

パーティションを削除/作成

$ sudo fdisk /dev/sda

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


Command (m for help): p
Disk /dev/sda: 114.6 GiB, 123048296448 bytes, 240328704 sectors
Disk model:  SanDisk 3.2Gen1
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: 0x00000000

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1          32 240328703 240328672 114.6G  c W95 FAT32 (LBA)
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

新規作成、すべてデフォルト(おすすめ)の設定で作成してもらう。

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (1-4, default 1):
First sector (2048-240328703, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-240328703, default 240328703):

Created a new partition 1 of type 'Linux' and of size 114.6 GiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

パーティションにファイルシステムを作成

10秒ほどかかった。

$ sudo mkfs -t ext4 /dev/sda1
mke2fs 1.46.2 (28-Feb-2021)
Creating filesystem with 30040832 4k blocks and 7512064 inodes
Filesystem UUID: 7e95fdaa-0244-411c-b542-baf04cd2f01c
Superblock backups stored on blocks:
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
	4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done
Writing inode tables: done
Creating journal (131072 blocks):
done
Writing superblocks and filesystem accounting information: done

ファイルシステムのUUIDを確認

fstabで管理するためのUUIDを確認しておく。

$ sudo blkid /dev/sda1
/dev/sda1: UUID="7e95fdaa-0244-411c-b542-baf04cd2f01c" BLOCK_SIZE="4096" TYPE="ext4"

マウントできるのを確認

手動でマウントして書き込みまでできることを確認しておく。

$ sudo mkdir /mnt/testmnt
$ sudo mount /dev/sda1 /mnt/testmnt
$ echo "test" | sudo tee -a /mnt/testmnt/test.txt
test
$ cat /mnt/testmnt/test.txt
test
$ sudo rm /mnt/testmnt/test.txt
$ sudo umount /dev/sda1
$ sudo rmdir /mnt/testmnt

実際にfstabを管理するのはansibleで行う。

お金のむこうに人がいる 元ゴールドマン・サックス金利トレーダーが書いた 予備知識のいらない経済新入門
[ad] お金のむこうに人がいる 元ゴールドマン・サックス金利トレーダーが書いた 予備知識のいらない経済新入門
田内学 (単行本(ソフトカバー))