Verkleinern einer Partition

Im folgenden Beispiel ist /dev/sdb eine mit ext4 formatierte 16GB SD-Card mit zwei Partitionen. Der Speicherplatz der gesamten Karte ist partitioniert, effektiv sind jedoch weniger als 3GB mit Daten belegt.
Die Speicherkarte soll neu partitioniert werden, so dass, abgesehen von der Boot-Partition, lediglich eine weitere Partition mit 4GB existiert.
Wer einen Raspberry- Pi besitzt dem wird das Partitionierungsschema bekannt vorkommen...
Das beschriebene Vorgehen funktioniert praktisch bei allen Laufwerken. Voraussetzung ist allerdings, dass das verwendete Dateisystem ein Verkleinern auch unterstützt.
Im ersten Schritt werden die Daten der derzeitige Partitionierung benötigt.
root@NC10SA:~# e2fsck -f /dev/sdb2
e2fsck 1.42 (29-Nov-2011)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb2: 77205/321920 files (0.2% non-contiguous), 593647/1310720 blocks
Nun folgt die Überprüfung des enthaltenen Dateisystems auf etwaige Fehler.
root@NC10SA:~# e2fsck -f /dev/sdb2
e2fsck 1.42 (29-Nov-2011)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb2: 77205/321920 files (0.2% non-contiguous), 593647/1310720 blocks
Da bekannt ist, dass weniger als 3GB Daten auf dem Laufwerk vorhanden sind, wird im nächsten Schritt das Dateisystem auf 3GB verkleinert.
root@NC10SA:~# resize2fs /dev/sdb2 3G
resize2fs 1.42 (29-Nov-2011)
Resizing the filesystem on /dev/sdb2 to 786432 (4k) blocks.
The filesystem on /dev/sdb2 is now 786432 blocks long.
Das Dateisystem ist nun 3GB groß.
Der nächste Schritt ist das Verkleinern der Partition.
Hierzu wird die Partition gelöscht und mit dem gleichen Startsektor neu angelegt. Lediglich das Ende der Partition wird auf +4GB festgelegt. Durch dieses Vorgehen bleiben die Daten auf dem Laufwerk erhalten.
root@NC10SA:~# fdisk /dev/sdb
 
Command (m for help): p
 
Disk /dev/sdb: 15.9 GB, 15931539456 bytes
64 heads, 32 sectors/track, 15193 cylinders, total 31116288 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
Disk identifier: 0x000981cb
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            8192      122879       57344    c  W95 FAT32 (LBA)
/dev/sdb2          122880    31116287    15496704   83  Linux
 
Command (m for help): d
Partition number (1-4): 2
 
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (1-4, default 2): 2
First sector (2048-31116287, default 2048): 122880
Last sector, +sectors or +size{K,M,G} (122880-31116287, default 31116287): +4G
 
Command (m for help): p
 
Disk /dev/sdb: 15.9 GB, 15931539456 bytes
64 heads, 32 sectors/track, 15193 cylinders, total 31116288 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
Disk identifier: 0x000981cb
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            8192      122879       57344    c  W95 FAT32 (LBA)
/dev/sdb2          122880     8511487     4194304   83  Linux
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
Die Partition ist nun 4GB groß.
Jetzt kann als letzter Schritt das Dateisystem wieder auf die vollen 4GB vergrössert werden.
root@NC10SA:~# e2fsck -f /dev/sdb2
e2fsck 1.42 (29-Nov-2011)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb2: 77205/193152 files (0.2% non-contiguous), 585219/786432 blocks
root@NC10SA:~# resize2fs /dev/sdb2
resize2fs 1.42 (29-Nov-2011)
Resizing the filesystem on /dev/sdb2 to 1048576 (4k) blocks.
The filesystem on /dev/sdb2 is now 1048576 blocks long.
Nach dem Mounten der Partition sieht man, dass diese nun tatsächlich nur noch 4GB groß ist.
root@NC10SA:~# mount /dev/sdb2 /mnt/
root@NC10SA:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       146G   23G  116G  17% /
udev            489M  4.0K  489M   1% /dev
tmpfs           199M  840K  198M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            496M  152K  496M   1% /run/shm
/dev/sdb2       4.0G  2.2G  1.6G  58% /mnt