created: 2023-05-01T02:09:50.450Z

買ってきたUSBフラッシュメモリを初期化してext4のパーティションを作成し、ansibleでマウントする

RaspberryPi OS での作業。

初期化

ラズパイは差し込んだだけでマウントしてくれているので、初期化に先駆けてこれを一度アンマウントする。

目当てのデバイスを確認。

$ mount -v | grep /dev/sd
/dev/sda1 on /mnt/usb64gb type ext4 (rw,relatime)
/dev/sdb1 on /media/rpi-first-boot-wizard/76B7-E1DE type vfat (rw,nosuid,nodev,relatime,uid=116,gid=65534,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2)

/media/rpi-first-boot-wizard/76B7-E1DE が目当てのデバイス(がマウントされているもの)なので取り外し。

$ sudo umount /media/rpi-first-boot-wizard/76B7-E1DE
$ mount -v | grep /dev/sd
/dev/sda1 on /mnt/usb64gb type ext4 (rw,relatime)
$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/root       14614072  5739508   8223848  42% /
devtmpfs          340548        0    340548   0% /dev
tmpfs             472132        0    472132   0% /dev/shm
tmpfs             188856     2132    186724   2% /run
tmpfs               5120        4      5116   1% /run/lock
/dev/mmcblk0p1    261108    52122    208986  20% /boot
/dev/sda1       60304252 54322764   2885784  95% /mnt/usb64gb
tmpfs              94424       24     94400   1% /run/user/116
tmpfs              94424       20     94404   1% /run/user/1000

初期化するので、既存のパーティション名を確認。

$ sudo fdisk -l | grep dev/sdb
Disk /dev/sdb: 232.97 GiB, 250148290560 bytes, 488570880 sectors
/dev/sdb1          32 488570879 488570848  233G  c W95 FAT32 (LBA)

fdisk でパーティションを削除/新規作成とやろうとしたら The device contains 'vfat' signature and it will be removed by a write command と言われた。問題ないので気にしない。

$ sudo fdisk /dev/sda1

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.

The device contains 'vfat' signature and it will be removed by a write command. See fdisk(8) man page and --wipe option for more details.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x6d309fb3.

n で新規作成したところ、以下のメッセージが出た(はじめてみた)

The kernel still uses the old partitions. The new table will be used at the next reboot.

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-488570847, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-488570847, default 488570847):

Created a new partition 1 of type 'Linux' and of size 233 GiB.

Command (m for help): w
The partition table has been altered.
Failed to add partition 1 to system: Invalid argument

The kernel still uses the old partitions. The new table will be used at the next reboot.
Syncing disks.

なにか差し障りがあるとアレなので、言われたことを気にして一度再起動する。

$ sudo reboot

再起動後、デバイスを確認。

再起動してデバイスの番号が変わったらしい(sdb => sda)

$ sudo lsblk --fs
NAME        FSTYPE FSVER LABEL  UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sda
└─sda1
sdb
└─sdb1      ext4   1.0          9bf13728-989c-4bde-91d6-41b6fb5b93c9    2.8G    90% /mnt/usb64gb
mmcblk0
├─mmcblk0p1 vfat   FAT32 boot   D386-9DE9                             204.1M    20% /boot
└─mmcblk0p2 ext4   1.0   rootfs 996c1b5f-170b-4f38-a5e0-85eef5acf12c    7.9G    39% /

パーティションを作成。

このときも Found a dos partition と言われてちょっと意外だったが、消して良いので続行。

$ sudo mkfs -t ext4 /dev/sda1
mke2fs 1.46.2 (28-Feb-2021)
Found a dos partition table in /dev/sda1
Proceed anyway? (y,N) y
Creating filesystem with 61071356 4k blocks and 15269888 inodes
Filesystem UUID: b7ac9c34-186d-4ff3-a3b8-46733bdde5b9
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 (262144 blocks):
done
Writing superblocks and filesystem accounting information: done

マウント

ext4のパーティションができたことを確認。

$ sudo lsblk --fs
NAME        FSTYPE FSVER LABEL  UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sda
└─sda1      ext4   1.0          b7ac9c34-186d-4ff3-a3b8-46733bdde5b9
sdb
└─sdb1      ext4   1.0          9bf13728-989c-4bde-91d6-41b6fb5b93c9    2.8G    90% /mnt/usb64gb
mmcblk0
├─mmcblk0p1 vfat   FAT32 boot   D386-9DE9                             204.1M    20% /boot
└─mmcblk0p2 ext4   1.0   rootfs 996c1b5f-170b-4f38-a5e0-85eef5acf12c    7.9G    39% /

fstabはansibleで管理しているので、こんな定義でUUIDを使ってマウントする。

    - name: prepare /mnt/usb256gb
      file:
        path: /mnt/usb256gb
        state: directory
        owner: pi
        group: pi
    - name: mount /mnt/usb256gb
      ansible.posix.mount:
        src: "UUID={{ usb256gb_uuid }}"
        opts: nofail
        path: /mnt/usb256gb
        fstype: ext4
        state: mounted

参考

パシフィック・リム [Blu-ray]
[ad] パシフィック・リム [Blu-ray]
出演 チャーリー・ハナム, イドリス・エルバ (Blu-ray)