Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Nächste Überarbeitung | Vorherige Überarbeitung | ||
lpi1:partitionierung [2018/07/09 09:48] 127.0.0.1 Externe Bearbeitung |
lpi1:partitionierung [2024/05/21 06:59] (aktuell) |
||
---|---|---|---|
Zeile 2: | Zeile 2: | ||
lsblk | lsblk | ||
- | ===== Kernel Partitionsliste aktualisieren ===== | + | ====== Partitionierung ====== |
- | Nach Änderungen an einem Boot-Datenträger. (Im Paket parted) | + | ===== Format der Partitionstabelle ===== |
- | partprobe | + | Das lange unter Linux gebräuchliche MSDOS oder [[wpde>Master Boot Record|MBR]] Format kann man nur bei Festplatten unter 2TB verwenden. Für größere Datenträger, oder wenn man wie bei aktuellen Rechnern üblich eine [[wpde>Unified Extensible Firmware Interface|UEFI]] Firmware an Stelle des [[wpde>BIOS]] vorfindet, sollte man für die Partitionstabelle [[wpde>GUID Partition Table|GPT]] verwenden. |
- | ====== Format der Partitionstabelle ====== | + | |
- | Das lange unter Linux gebräuchliche MSDOS oder [[wpde>Master Boot Record|MBR]] Format sollte man nur bei Festplatten unter 2TB verwenden. Für größere Festplatten, und wenn man wie bei aktuellen Rechnern üblich eine [[wpde>Unified Extensible Firmware Interface|UEFI]] Firmware an Stelle des [[wpde>BIOS]] vorfindet, sollte man für die Partitionstabelle [[wpde>GUID Partition Table|GPT]] verwenden. | + | |
- | ====== Übung: default-Werte für Mount-Optionen herausfinden ====== | + | Tools: |
- | Test-Image erstellen und mounten: | + | * fdisk - MBR und GPT Partitionstabellen |
- | dd if=/dev/zero of=/tmp/zero.img bs=1M count=500 | + | * gdisk - Spezialist für GPT Partitionstabellen |
- | mkfs.ext4 /tmp/zero.img | + | * parted - MBR und GPT Partitionstabellen |
- | mkdir /mnt/img | + | |
- | mount /tmp/zero.img /mnt/img | + | |
- | ''exec'': | + | ===== Beispiel: neue Partition anlegen ===== |
- | cp -a /usr/bin/tail /mnt/img | + | ==== mit fdisk ==== |
- | Ist ''/mnt/img/tail'' ausführbar? | + | === Benötigte Pakete === |
+ | * Debian: ''util-linux'' | ||
+ | === Wichtige fdisk Befehle === | ||
+ | | m | Hilfe | | ||
+ | | d | Partition löschen | | ||
+ | | n | neue Partition erstellen | | ||
+ | | t | Typ der Partition setzen | | ||
+ | | p | Partitionstabelle anzeigen | | ||
+ | | w | geänderte Partitionstabelle speichern | | ||
+ | | q | fdisk verlassen ohne zu speichern | | ||
- | ''suid'': | + | === Partition erstellen === |
- | chmod u+s /mnt/img/tail | + | 2 Gigibyte große Partition auf Festplatte ''/dev/sda'' anlegen: |
- | Als Nutzer: | + | fdisk /dev/sda |
- | /mnt/img/tail /etc/shadow | + | Aktuelle Partitionstabelle anzeigen: |
- | Ist das SUID-Bit wirksam? | + | p |
+ | Neue Partition erstellen: | ||
+ | n | ||
+ | Partition als logische Partition erstellen (//nur bei MSDOS/MBR//): | ||
+ | l | ||
+ | Startblock auswählen: //Bei der ersten Partition sollte hier Sektor 2048 oder ein Vielfaches davon stehen. Wenn die Sektorgröße 512 Bytes ist, dann liegt der Partitionsbeginn bei 1048576 Bytes ([[wpde>Mebibyte|=1 MiB]]).// | ||
- | ''dev'': | + | Größe angeben: |
- | mknod /mnt/img/sda b 8 0 | + | +2G |
- | fdisk -l /mnt/img/sda | + | geänderte Partitionstabelle speichern: |
- | Ist der Zugriff auf das Device ''/mnt/img/sda'' möglich? | + | w |
- | ====== Übung: virtuelle Festplatte partitionieren ====== | + | === Prüfen, ob der Kernel die geänderte Partitionstabelle kennt == |
- | Erstellen einer virtuellen Festplatte und vorbereiten für den Einsatz in einer VM | + | cat /proc/partitions |
+ | Wenn nicht, Kernel Partitionstabelle neu einlesen lassen: | ||
+ | partprobe | ||
+ | ((oder | ||
+ | blockdev --rereadpt /dev/sda | ||
+ | )) | ||
- | Pakete installieren: | + | ==== Mit parted ==== |
- | * CentOS 7: ''qemu-img'' | + | === Benötigte Pakete === |
- | * Debian 8: ''qemu-utils'' | + | * Debian: ''parted'' |
- | * openSUSE 42.2: ''qemu-tools'' | + | |
- | ===== Beispiel: GPT mit parted ===== | + | === Wichtige parted Befehle === |
+ | | help | Hilfe | | ||
+ | | rm | Partition löschen | | ||
+ | | mklabel | neue Partitionstabelle erstellen | | ||
+ | | mkpart | neue Partition erstellen | | ||
+ | | set | Typ der Partition setzen | | ||
+ | | print | Partitionstabelle anzeigen | | ||
+ | | quit | fdisk verlassen ohne zu speichern | | ||
- | Virtuelle Festplatte erstellen: | + | === Partition erstellen === |
- | qemu-img create /srv/vmdisk1.img 2G | + | 2 Gibibyte große Partition auf Festplatte ''/dev/sda'' anlegen: |
- | + | parted /dev/sda | |
- | === Partitionieren: === | + | Aktuelle Partitionierung ausgeben: |
- | parted /srv/vmdisk1.img | + | |
- | + | Neue Partition erstellen: | |
- | GPT-Partitionstabelle anlegen: (In der ''parted'' Kommandozeile) | + | mkpart |
- | mklabel gpt | + | Partition type? primary/logical? |
- | + | l | |
- | [[wpde>Mebibyte|100 MiB]] Swap-Partition anlegen: | + | File system type? |
- | mkpart linux-swap 1MiB 100MiB | + | ext3 |
- | + | Start? | |
- | Daten-Partition anlegen: | + | xxx |
- | mkpart ext4 100MiB 100% | + | End? |
- | + | xxx | |
- | ''parted'' verlassen: | + | Parted verlassen: |
quit | quit | ||
- | |||
- | === formatieren === | ||
- | Partitionen aus der Image-Datei im System sichtbar machen: | ||
- | losetup --show -f -P /srv/vmdisk1.img | ||
- | |||
- | Swap-Partition formatieren: | ||
- | mkswap /dev/loop0p1 | ||
- | |||
- | System/Datenpartition mit XFS für VM mit 2 vCPUs formatieren: | ||
- | mkfs.xfs -d agcount=2 /dev/loop0p2 | ||
- | |||
- | ==== CentOS 7 ins Image installieren ==== | ||
- | yumdownloader centos-release.x86_64 | ||
- | rpm --root /mnt/loop -ihv centos-release-7-*.rpm | ||
- | install -b --suffix=.orig /etc/yum.repos.d/CentOS-Base.repo /mnt/loop/etc/yum.repos.d/CentOS-Base.repo | ||
- | yum -y --nogpg --releasever=7 --installroot=/mnt/loop install systemd passwd yum procps-ng iproute tmux grub | ||
- | grub2-install --root-directory=./ /dev/loop0 | ||
- | |||
- | | ||
- | TODO: formatieren, vergrößern | ||
====== Partitionstabelle sichern und wiederherstellen ====== | ====== Partitionstabelle sichern und wiederherstellen ====== | ||
==== sfdisk ==== | ==== sfdisk ==== | ||
- | für MBR-partitionierte Blockgeräte | ||
=== sichern === | === sichern === | ||
sfdisk -d /dev/sda > part-table.txt | sfdisk -d /dev/sda > part-table.txt | ||
Zeile 87: | Zeile 87: | ||
==== sgdisk ==== | ==== sgdisk ==== | ||
- | für GPT Partitionen | + | nur für GPT Partitionen |
=== sichern === | === sichern === | ||
sgdisk --backup=sda.gpt.bkup /dev/sda | sgdisk --backup=sda.gpt.bkup /dev/sda | ||
Zeile 99: | Zeile 99: | ||
=== wiederherstellen === | === wiederherstellen === | ||
dd if=part-table.img of=/dev/sda bs=1 count=64 skip=446 seek=446 | dd if=part-table.img of=/dev/sda bs=1 count=64 skip=446 seek=446 | ||
+ | |||
==== fdisk ==== | ==== fdisk ==== | ||
=== sichern === | === sichern === | ||
Zeile 110: | Zeile 111: | ||
=== wiederherstellen === | === wiederherstellen === | ||
geht nur von Hand | geht nur von Hand | ||
- | |||
==== parted ==== | ==== parted ==== | ||
Zeile 118: | Zeile 118: | ||
geht nur von Hand | geht nur von Hand | ||
- | Leere Partitionstabelle erstellen: | + | ====== Übung: default-Werte für mount-Optionen herausfinden ====== |
- | parted /dev/sda mklabel | + | Test-Image erstellen und mounten: |
- | msdos | + | fallocate -l 500M /tmp/zero.img |
+ | (( | ||
oder | oder | ||
- | gpt | + | dd if=/dev/zero of=/tmp/zero.img bs=1M seek=500 count=0 |
- | Partitionierung [[partitionierung#beispielneue_partition_anlegen| von Hand anlegen]] | + | oder |
+ | dd if=/dev/zero of=/tmp/zero.img bs=1M 500 | ||
+ | )) | ||
+ | mkfs.ext4 /tmp/zero.img | ||
+ | mkdir /mnt/img | ||
+ | mount /tmp/zero.img /mnt/img | ||
- | ====== Partitionierung ====== | + | === Ist die mount-Option suid aktiv? === |
- | ===== fdisk/gdisk ===== | + | cp -a /usr/bin/tail /mnt/img |
- | fdisk - ausschließlich MBR-Partitionierte Blockgeräte\\ | + | chmod u+s /mnt/img/tail |
- | gdisk - GPT Blockgeräte | + | Als Nutzer: |
- | ==== Benötigte Pakete ==== | + | /mnt/img/tail /etc/shadow |
- | Debian: | + | Ist das SUID-Bit wirksam? |
- | util-linux | + | |
- | === Wichtige fdisk Befehle === | + | |
- | | m | Hilfe | | + | |
- | | d | Partition löschen | | + | |
- | | n | neue Partition erstellen | | + | |
- | | t | Typ der Partition setzen | | + | |
- | | p | Partitionstabelle anzeigen | | + | |
- | | w | geänderte Partitionstabelle speichern | | + | |
- | | q | fdisk verlassen ohne zu speichern | | + | |
- | ==== Beispiel: neue Partition anlegen ==== | + | === Ist die mount-Option exec aktiv? === |
- | 2100 MByte große Partition auf Festplatte ''/dev/sda'' anlegen: | + | cp -a /usr/bin/tail /mnt/img |
- | fdisk /dev/sda | + | /mnt/img/tail /etc/passwd |
- | Neue Partition erstellen: | + | Ist ''/mnt/img/tail'' ausführbar? |
- | n | + | |
- | Partition als logische Partition erstellen: | + | === Ist die mount-Option dev aktiv? === |
- | l | + | mknod /mnt/img/sda b 8 0 |
- | Startblock auswählen: | + | fdisk -l /mnt/img/sda |
+ | Ist der Zugriff auf das Device ''/mnt/img/sda'' möglich? | ||
+ | |||
+ | ====== Übung: virtuelle Festplatte partitionieren ====== | ||
+ | Erstellen einer virtuellen Festplatte und vorbereiten für den Einsatz in einer VM | ||
+ | |||
+ | Pakete installieren: | ||
+ | * CentOS 7: ''parted qemu-img'' | ||
+ | * Debian 8: ''parted qemu-utils'' | ||
+ | * openSUSE 42.2: ''parted qemu-tools'' | ||
+ | |||
+ | ===== Beispiel: Loop-Devices mit Sparse-Files ===== | ||
+ | Nicht in der Praxis ;-) | ||
+ | |||
+ | == Sparse-File erzeugen == | ||
+ | dd if=/dev/zero of=disc1 bs=1K count=0 seek=200M | ||
+ | ls -lsh disc* | ||
+ | losetup -f disc1 --show | ||
+ | losetup -a | ||
- | //Bei der ersten Partition sollte hier Sektor 2048 oder ein Vielfaches davon stehen. Wenn die Sektorgröße 512 Bytes ist, dann liegt der Partitionsbeginn bei 1048576 Bytes ([[wpde>Mebibyte|=1 MiB]]). Bei virtuellen Maschinen kann man hier auch 0 angeben.// | + | == Loop-Devices partitionieren == |
+ | lsblk | ||
+ | {fdisk,gdisk,parted} /dev/loopxx | ||
- | Größe angeben: | + | == Partitionen sichtbar machen == |
- | +2100M | + | |
- | geänderte Partitionstabelle speichern: | + | |
- | w | + | |
- | Alte Kerneltabelle ansehen: | + | |
- | cat /proc/partitions | + | |
- | Kernel Partitionstabelle neu einlesen lassen: | + | |
partprobe | partprobe | ||
- | ((oder | + | partprobe /dev/loopxx |
- | blockdev --rereadpt /dev/sda | + | |
- | )) | + | |
- | Neue Kerneltabelle prüfen: | + | ===== Beispiel: GPT mit parted ===== |
- | cat /proc/partitions | + | |
- | ===== Mit parted ===== | + | Virtuelle Festplatte erstellen: |
- | ==== Benötigte Pakete ==== | + | qemu-img create /srv/vmdisk1.img 2G |
- | Debian: | + | |
- | parted | + | |
- | === Wichtige parted Befehle === | + | === Partitionieren: === |
- | | help | Hilfe | | + | parted /srv/vmdisk1.img |
- | | rm | Partition löschen | | + | |
- | | mklabel | neue Partitionstabelle erstellen | | + | |
- | | mkpart | neue Partition erstellen | | + | |
- | | set | Typ der Partition setzen | | + | |
- | | print | Partitionstabelle anzeigen | | + | |
- | | quit | fdisk verlassen ohne zu speichern | | + | |
- | ==== Beispiel: neue Partition anlegen ==== | + | GPT-Partitionstabelle anlegen: (In der ''parted'' Kommandozeile) |
- | 2100 MByte große Partition auf Festplatte ''/dev/sda'' anlegen: | + | mklabel gpt |
- | parted /dev/sda | + | |
- | Aktuelle Partitionierung ausgeben: | + | |
- | + | ||
- | Neue Partition erstellen: | + | |
- | mkpart | + | |
- | Partition type? primary/logical? | + | |
- | l | + | |
- | File system type? | + | |
- | ext3 | + | |
- | Start? | + | |
- | 16500 | + | |
- | End? | + | |
- | 19077 | + | |
- | Parted verlassen: | + | |
- | quit | + | |
- | === Fehlermeldung: The resulting partition is not properly aligned for best performance === | + | [[wpde>Mebibyte|100 MiB - 1MiB]] (parted arbeitet mit festen Endwerten) Swap-Partition anlegen: |
- | Lösung: http://support.hp.com/us-en/document/c03479326 | + | mkpart linux-swap 1MiB 100MiB |
- | ==== Beispiel: Partition im laufenden Betrieb anlegen ==== | + | Daten-Partition anlegen: |
- | Partition anlegen: | + | mkpart ext4 100MiB 100% |
- | parted /dev/sda | + | |
- | + | GRUB BIOS Partition aktivieren: | |
- | mkpart | + | set 1 bios_grub on |
+ | |||
+ | ''parted'' verlassen: | ||
quit | quit | ||
- | Mountpoint anlegen und temporär mounten | ||
- | mkfs.ext3 /dev/sda6 | ||
- | mkdir /mnt/sda6 | ||
- | mount /dev/sda6 /mnt/sda6 | ||
- | Daten in die neue Partition kopieren: | ||
- | cp -a /usr/bin/. /mnt/sda6 | ||
- | Kopie überprüfen: | ||
- | ( cd /usr/bin; tar c . ) | ( cd /mnt/sda6; tar d . ) | ||
- | Partition wieder aushängen | ||
- | umount /mnt/sda6 | ||
- | Partition endgültig mounten: | ||
- | mount /dev/sda6 /usr/bin | ||
- | tail -n 1 /etc/mtab >> /etc/fstab | ||
- | Daten auf Orginalpartition löschen: | ||
- | mkdir /mnt/original | ||
- | mount --bind / /mnt/original | ||
- | shopt -s dotglob | ||
- | rm /mnt/original/usr/bin/* | ||
- | ls -la /mnt/original/usr/bin | ||
- | umount /mnt/original | ||
+ | === formatieren === | ||
+ | Partitionen aus der Image-Datei im System sichtbar machen: | ||
+ | losetup --show -f -P /srv/vmdisk1.img | ||
+ | |||
+ | Swap-Partition formatieren: | ||
+ | mkswap /dev/loop0p1 | ||
+ | |||
+ | System/Datenpartition mit XFS für VM mit 2 vCPUs formatieren: | ||
+ | mkfs.xfs -d agcount=2 /dev/loop0p2 | ||
+ | |||
+ | ==== CentOS 7 ins Image installieren ==== | ||
+ | yumdownloader centos-release.x86_64 | ||
+ | rpm -ihv --nodeps --root /mnt/loop centos-release-7-*.rpm | ||
+ | install -b --suffix=.orig /etc/yum.repos.d/CentOS-Base.repo /mnt/loop/etc/yum.repos.d/CentOS-Base.repo | ||
+ | yum -y --nogpg --releasever=7 --installroot=/mnt/loop install grub2 iproute tmux passwd procps-ng systemd yum | ||
+ | grub2-install --root-directory=./ /dev/loop0 | ||
+ | |||
+ | | ||
+ | TODO: formatieren, vergrößern | ||