Installiert man Raspberry OS wie in Raspberry – die Grundinstallation beschrieben, und hat das System 1x auf dem neuen Rechner gestartet, hat man 2 Partitionen auf der SD Karte: eine kleine Boot Partition im FAT Format, und die eigentliche OS Partition im ext4 Format, die dann den restlichen Platz der SD Karte einnimmt. Folgendes kann man aber auch schon machen, wenn man die Grundinstallation unter Linux macht, und die neu installierten Partitionen noch im Ursprungszustand sind.
Also, möchte man eine andere Einteilung, kann man das ändern. Ein Grund wäre z.B., man will die neu entstehenden Daten auf eine Extra Partition schreiben, damit ein Überlauf der Daten Partition nicht das primäre System auch lahm legt, oder …
Anlass für mich war jedoch die Notwendigkeit, das System absturzsicher zu machen, also das System read-only zu betreiben. Aber wohin dann mit den dynamischen Daten, die das System produziert? Als muss noch eine weitere Partition her, die nicht R/O ist, sondern R/W.
Leider gibt es da ein paar Klippen, man kann nicht einfach eine Partition online verkleinern, insbesondere hatte ich das Problem, dass je nach Reihenfolge bei Vorgehen das System nicht mehr ins WLAN kam – für mich nicht ersichtlich, warum. Hier nun ein Weg, der mal bei mir funktioniert hat.
Vorgehen 1: Sofort nach der Grundinstallation, d.h. noch auf dem Generierrechner – falls der auch ein Linux System ist – dann ist das neue Linux System noch ganz klein.
parted /dev/sdb
(parted) print --> check - Partition 2 nur 1590MB groß
(parted) resizepart 2 6000MB
(parted) print --> check - Partition 2 nun 5727MB groß
(parted) mkpart primary ext4 6001BMMB 62,5GB - Anlegen einer dritten Partition für den Rest (Endgröße von print genommen)
Parted macht auch die Erweiterung des Filesystems auf Partition 2. Für die neue Partition 3 muss jedoch noch ein Filesystem gemacht werden.
mkfs -t ext4 /dev/sdb3
Dies ist der Sprung zu den letzten Arbeiten
Vorgehen 2: Das System läuft auf dem neuen Rechner erfolgreich. Dann Karte ausbauen, an einem anderen Raspberry System mittels Kartenleser wieder anschließen. Falls das System diese SD Karte automatisch mounted, wieder dismounten:
root@rasp6berrypi:~# umount /dev/sdb1
root@rasp6berrypi:~# umount /dev/sdb2
root@rasp6berrypi:~# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.33.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/sdb: 29,9 GiB, 32044482560 bytes, 62586880 sectors
Disk model: FCR-HS219/1
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: 0x5315105e
Device Boot Start End Sectors Size Id Type
/dev/sdb1 8192 532479 524288 256M c W95 FAT32 (LBA)
/dev/sdb2 532480 62586879 62054400 29,6G 83 Linux
Command (m for help): q
Nun die Aufgabe, das Filesystem auf die gewünschte Größe zu reduzieren. Ist der Platz wirklich so weit frei?
Der Wert von 6000MB kann natürlich weniger sein, zur Zeit brauche ich auf dieser Patition nur 2,4 GB, aber es sollte natürlich noch etwas Platz da sein für weitere Installationen.
root@rasp6berrypi:~# resize2fs /dev/sdb2 6000M
resize2fs 1.44.5 (15-Dec-2018)
Resizing the filesystem on /dev/sdb2 to 1536000 (4k) blocks.
The filesystem on /dev/sdb2 is now 1536000 (4k) blocks long.
Falls das schiefgeht, mehr geht mit:
e2fsck -f /dev/sdb2
resize2fs -P /dev/sdb2 --> wie weit kann man verkleinern
resize2fs -M /dev/sdb2 --> System versucht, es so klein wie möglich zu machen.
e4defrag --> wie der Name sagt, ob es was bringt?
Jetzt ist das Filesystem klein, aber die Partition noch groß, also:
root@rasp6berrypi:~# parted /dev/sdb
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: Kingston FCR-HS219/1 (scsi)
Disk /dev/sdb: 32,0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 4194kB 273MB 268MB primary fat32 lba
2 273MB 32,0GB 31,8GB primary ext4
(parted) resizepart 2 6273M
Warning: Shrinking a partition can cause data loss, are you sure you want to
continue?
Yes/No? y
(parted) print
Model: Kingston FCR-HS219/1 (scsi)
Disk /dev/sdb: 32,0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 4194kB 273MB 268MB primary fat32 lba
2 273MB 6273MB 6000MB primary ext4
(parted)
Information: You may need to update /etc/fstab.
root@rasp6berrypi:~# resize2fs /dev/sdb2
resize2fs 1.44.5 (15-Dec-2018)
Resizing the filesystem on /dev/sdb2 to 1464934 (4k) blocks.
The filesystem on /dev/sdb2 is now 1464934 (4k) blocks long.
root@rasp6berrypi:~# parted /dev/sdb
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: Kingston FCR-HS219/1 (scsi)
Disk /dev/sdb: 32,0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 4194kB 273MB 268MB primary fat32 lba
2 273MB 6273MB 6000MB primary ext4
Nun können wir die neue, dritte Partition anlegen.
root@rasp6berrypi:~# parted /dev/sdb
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkpart
Partition type? primary/extended? p
File system type? [ext2]? ext4
Start? 6273MB
End? 32,0GB
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I
(parted) print
Model: SD USDU1 (sd/mmc)
Disk /dev/mmcblk0: 32.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 4194kB 273MB 268MB primary fat32 lba
2 273MB 6273MB 6000MB primary ext4
3 6273MB 32.0GB 25.8GB primary ext4
(parted) q
Information: You may need to update /etc/fstab.
Jetzt noch die neue Partition formatieren (warum Parted das nicht macht, wo es doch nach dem Format fragt?).
root@rasp6berrypi:~# mkfs -t ext4 /dev/sdb3
mke2fs 1.44.5 (15-Dec-2018)
Creating filesystem with 6291456 4k blocks and 1575936 inodes
Filesystem UUID: d3bcda63-9a66-4edc-a3a2-f7441bf0a21a
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
Weiter Arbeiten am Zielrechner
Karte wieder am Zielrechner einstecken und einloggen:
mkdir /mnt/home
mount /dev/mmcblk0p3 /mnt/home
Diese muss noch in /etc/fstab eingetragen werden, damit das auch nach Neustart wieder automatisch da ist:
/dev/mmcblk0p3 /mnt/home ext4 defaults,noatime 0 1
Dann hat man nun eine extra Partition im System, und kann die 2. Partition, die OS Partition zu r/o machen. Dazu siehe Kapitel https://www.irmnik.de/2023/03/23/raspberry-read-only-root-partition/