Zvetseni ext4 oddilu ze skriptu

Tentokrat si ukazeme rychle expandovani EXT4 oddilu na SD karte a to ze skriptu, neni potreba zadny interaktivni uzivateluv vstup.

Nejcasteji je potreba expandovat filesystem na obrazech pro Raspberry Pi a ostatni SBC. Vetsinou maji ke stazeni 4GB image SD karty, ktery jde ale bez problemu nahrat treba na 16GB kartu. Oddil je vasak stale 4GB velky a z anim je volne misto. Zpusobu zvetseni je hodne. Ukazeme si relativne malo znamy program sfdisk.

Jak rika manual: „sfdisk is a script-oriented tool for partitioning any block device„. Umi sposutu veci, viz treba example, a jeho syntaxe je dosti zvlastni.

Nyni uz konecne ukazka zvetseni. Mame 16GB SD kartu, kterou system vidi jako /dev/sdb. Karta ma dva oddily, kde ext4 oddil je druhy, tedy /dev/sdb2. Jeho pocatecni velikost je 3,6GB. Na konci bude roztazeny do konce karty.
Postup:
  1. Pomoci fdisk -l overime, zda pracujeme se spravnym diskem. Neni zde zadne tlacitko zpet…
  2. Pote provedeme fsck daneho oddilu.
  3. Odpojime vsechny oddily SD karty.
  4. Zvetsime oddil.
  5. Zvetsime ext4.
  6. A zkontrolujeme vysledek.

Vse samozrejmne delame pod rootem. Ke zvetseni slouzi prikaz echo „, +“ | sfdisk -N 2 /dev/sdb. Kde parametr N udava druhy oddil. Text v uvozovkach popisuje operaci.
+ zvetsi oddil na maximalni moznou velikost. Pokud bychom chteli zvetsit treba o 50MB, pouzili bychom echo „, +50M“ | sfdisk -N 2 /dev/sdb

root@martin:~# fdisk -l /dev/sdb  
Disk /dev/sdb: 14.5 GiB, 15523119104 bytes, 30318592 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
Disklabel type: dos
Disk identifier: 0x9ed9cab6

Device     Boot  Start     End Sectors  Size Id Type
/dev/sdb1  *      2048  133119  131072   64M  c W95 FAT32 (LBA)
/dev/sdb2       133120 7679999 7546880  3.6G 83 Linux
root@martin:~# 

root@martin:~# df -hT | grep sdb2
/dev/sdb2      ext4      3.5G  1.1G  2.4G  31% /media/martin/PI_ROOT
root@martin:~# 

root@martin:~# fsck /dev/sdb2
fsck from util-linux 2.26.2
e2fsck 1.42.12 (29-Aug-2014)
PI_ROOT: clean, 45798/236176 files, 310243/943360 blocks
root@martin:~# 

root@martin:~# umount /dev/sdb1
root@martin:~# 

root@martin:~# umount /dev/sdb2                     
root@martin:~# 

root@martin:~# echo ", +" | sfdisk -N 2 /dev/sdb
Checking that no-one is using this disk right now ... OK

Disk /dev/sdb: 14.5 GiB, 15523119104 bytes, 30318592 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
Disklabel type: dos
Disk identifier: 0x9ed9cab6

Old situation:

Device     Boot  Start     End Sectors  Size Id Type
/dev/sdb1  *      2048  133119  131072   64M  c W95 FAT32 (LBA)
/dev/sdb2       133120 7679999 7546880  3.6G 83 Linux

/dev/sdb2: 
New situation:

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sdb1  *      2048   133119   131072   64M  c W95 FAT32 (LBA)
/dev/sdb2       133120 30318591 30185472 14.4G 83 Linux

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
root@martin:~# 

root@martin:~# fdisk -l /dev/sdb
Disk /dev/sdb: 14.5 GiB, 15523119104 bytes, 30318592 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
Disklabel type: dos
Disk identifier: 0x9ed9cab6

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sdb1  *      2048   133119   131072   64M  c W95 FAT32 (LBA)
/dev/sdb2       133120 30318591 30185472 14.4G 83 Linux
root@martin:~# 

root@martin:~# e2fsck -f /dev/sdb2
e2fsck 1.42.12 (29-Aug-2014)
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
PI_ROOT: 45798/236176 files (0.1% non-contiguous), 310243/943360 blocks
root@martin:~#

root@martin:~# resize2fs /dev/sdb2 
resize2fs 1.42.12 (29-Aug-2014)
Resizing the filesystem on /dev/sdb2 to 3773184 (4k) blocks.
The filesystem on /dev/sdb2 is now 3773184 (4k) blocks long.
root@martin:~# 

# ... mount ...

root@martin:~# df -hT | grep sdb2
/dev/sdb2      ext4       15G  1.1G   14G   8% /media/martin/PI_ROOT
root@martin:~# 

 
 
 

Napsat komentář

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *