Agrandir la partition swap sous FreeBSD

Le disque est plein et nous n'avons pas assez d'espace d'échange (swap) pour compiler certains logiciels. Nous allons alors tenter de déshabiller / pour habiller swap.

Notre disque contient trois partitions GPT que nous allons afficher avec la commande gpart.

$ gpart list ada0
Geom name: ada0
modified: false
state: OK
fwheads: 16
fwsectors: 63
last: 156301447
first: 40
entries: 128
scheme: GPT
Providers:
1. Name: ada0p1
   Mediasize: 524288 (512K)
   Sectorsize: 512
   Stripesize: 0
   Stripeoffset: 20480
   Mode: r0w0e0
   efimedia: HD(1,GPT,49418782-8f3f-11e7-95cd-00c09ff0b94b,0x28,0x400)
   rawuuid: 49418782-8f3f-11e7-95cd-00c09ff0b94b
   rawtype: 83bd6b9d-7f41-11dc-be0b-001560b84f0f
   label: fbsdboot
   length: 524288
   offset: 20480
   type: freebsd-boot
   index: 1
   end: 1063
   start: 40
2. Name: ada0p2
   Mediasize: 76235145216 (71G)
   Sectorsize: 512
   Stripesize: 0
   Stripeoffset: 544768
   Mode: r1w1e1
   efimedia: HD(2,GPT,4966755b-8f3f-11e7-95cd-00c09ff0b94b,0x428,0x8dffc00)
   rawuuid: 4966755b-8f3f-11e7-95cd-00c09ff0b94b
   rawtype: 516e7cb6-6ecf-11d6-8ff8-00022d09712b
   label: fbsddata
   length: 76235145216
   offset: 544768
   type: freebsd-ufs
   index: 2
   end: 148897831
   start: 1064
3. Name: ada0p3
   Mediasize: 3790650880 (3.5G)
   Sectorsize: 512
   Stripesize: 0
   Stripeoffset: 3221245952
   Mode: r1w1e0
   efimedia: HD(3,GPT,4970d6d3-8f3f-11e7-95cd-00c09ff0b94b,0x8e00028,0x70f85f)
   rawuuid: 4970d6d3-8f3f-11e7-95cd-00c09ff0b94b
   rawtype: 516e7cb5-6ecf-11d6-8ff8-00022d09712b
   label: fbsdswap
   length: 3790650880
   offset: 76235689984
   type: freebsd-swap
   index: 3
   end: 156301446
   start: 148897832
Consumers:
1. Name: ada0
   Mediasize: 80026361856 (75G)
   Sectorsize: 512
   Mode: r2w2e3

La partition de swap fait 3,5 Gio (3790650880 o) mais nous préférerions qu'il fasse 8 Gio (8589934592 o). Il faut donc réduire la partition ada0p2 de 4799283712 o ce qui la fera passer de 76235145216 o à 71435861504 o. Nous allons tenter de réduire le système de fichier avec la commande growfs.

# growfs -N -s 71435861504b /dev/ada0p2
growfs: requested size 67GB is not larger than the current filesystem size 71GB

Bon, ça aurait été trop beau... Nous allons donc utiliser un disque intermédiaire pour sauvegarder le contenu de notre partition. Puis nous la détruirons pour la reconstruire avec la taille adéquate.

Nous branchons un disque dur vide et nous y créons une table des partitions GPT. Puis nous créons une partition UFS qui occupera l'intégralité du disque. Nous ajoutons ensuite un système de fichier sur cette partition. Enfin, nous montons ce nouveau systèmes de fichier dans notre arborescence afin de pouvoir y avoir accès.

# gpart create -s gpt da0
# gpart add -t freebsd-ufs da0
# newfs -L backup da0p1
/dev/da0p1: 476940.0MB (976773088 sectors) block size 32768, fragment size 4096
        using 762 cylinder groups of 626.09MB, 20035 blks, 80256 inodes.
super-block backups (for fsck_ffs -b #) at:
 192, 1282432, 2564672, 3846912, 5129152, 6411392, 7693632, 8975872, 10258112, 11540352, 12822592, 14104832, 15387072, 16669312, 17951552, 19233792, 20516032, 21798272, 23080512, 24362752, 25644992, 26927232, 28209472, 29491712, 30773952, 32056192, 33338432, 34620672,
 35902912, 37185152, 38467392, 39749632, 41031872, 42314112, 43596352, 44878592, 46160832, 47443072, 48725312, 50007552, 51289792, 52572032, 53854272, 55136512, 56418752, 57700992, 58983232, 60265472, 61547712, 62829952, 64112192, 65394432, 66676672, 67958912, 69241152,
 70523392, 71805632, 73087872, 74370112, 75652352, 76934592, 78216832, 79499072, 80781312, 82063552, 83345792, 84628032, 85910272, 87192512, 88474752, 89756992, 91039232, 92321472, 93603712, 94885952, 96168192, 97450432, 98732672, 100014912, 101297152, 102579392,
 103861632, 105143872, 106426112, 107708352, 108990592, 110272832, 111555072, 112837312, 114119552, 115401792, 116684032, 117966272, 119248512, 120530752, 121812992, 123095232, 124377472, 125659712, 126941952, 128224192, 129506432, 130788672, 132070912, 133353152,
 134635392, 135917632, 137199872, 138482112, 139764352, 141046592, 142328832, 143611072, 144893312, 146175552, 147457792, 148740032, 150022272, 151304512, 152586752, 153868992, 155151232, 156433472, 157715712, 158997952, 160280192, 161562432, 162844672, 164126912,
 165409152, 166691392, 167973632, 169255872, 170538112, 171820352, 173102592, 174384832, 175667072, 176949312, 178231552, 179513792, 180796032, 182078272, 183360512, 184642752, 185924992, 187207232, 188489472, 189771712, 191053952, 192336192, 193618432, 194900672,
 196182912, 197465152, 198747392, 200029632, 201311872, 202594112, 203876352, 205158592, 206440832, 207723072, 209005312, 210287552, 211569792, 212852032, 214134272, 215416512, 216698752, 217980992, 219263232, 220545472, 221827712, 223109952, 224392192, 225674432,
 226956672, 228238912, 229521152, 230803392, 232085632, 233367872, 234650112, 235932352, 237214592, 238496832, 239779072, 241061312, 242343552, 243625792, 244908032, 246190272, 247472512, 248754752, 250036992, 251319232, 252601472, 253883712, 255165952, 256448192,
 257730432, 259012672, 260294912, 261577152, 262859392, 264141632, 265423872, 266706112, 267988352, 269270592, 270552832, 271835072, 273117312, 274399552, 275681792, 276964032, 278246272, 279528512, 280810752, 282092992, 283375232, 284657472, 285939712, 287221952,
 288504192, 289786432, 291068672, 292350912, 293633152, 294915392, 296197632, 297479872, 298762112, 300044352, 301326592, 302608832, 303891072, 305173312, 306455552, 307737792, 309020032, 310302272, 311584512, 312866752, 314148992, 315431232, 316713472, 317995712,
 319277952, 320560192, 321842432, 323124672, 324406912, 325689152, 326971392, 328253632, 329535872, 330818112, 332100352, 333382592, 334664832, 335947072, 337229312, 338511552, 339793792, 341076032, 342358272, 343640512, 344922752, 346204992, 347487232, 348769472,
 350051712, 351333952, 352616192, 353898432, 355180672, 356462912, 357745152, 359027392, 360309632, 361591872, 362874112, 364156352, 365438592, 366720832, 368003072, 369285312, 370567552, 371849792, 373132032, 374414272, 375696512, 376978752, 378260992, 379543232,
 380825472, 382107712, 383389952, 384672192, 385954432, 387236672, 388518912, 389801152, 391083392, 392365632, 393647872, 394930112, 396212352, 397494592, 398776832, 400059072, 401341312, 402623552, 403905792, 405188032, 406470272, 407752512, 409034752, 410316992,
 411599232, 412881472, 414163712, 415445952, 416728192, 418010432, 419292672, 420574912, 421857152, 423139392, 424421632, 425703872, 426986112, 428268352, 429550592, 430832832, 432115072, 433397312, 434679552, 435961792, 437244032, 438526272, 439808512, 441090752,
 442372992, 443655232, 444937472, 446219712, 447501952, 448784192, 450066432, 451348672, 452630912, 453913152, 455195392, 456477632, 457759872, 459042112, 460324352, 461606592, 462888832, 464171072, 465453312, 466735552, 468017792, 469300032, 470582272, 471864512,
 473146752, 474428992, 475711232, 476993472, 478275712, 479557952, 480840192, 482122432, 483404672, 484686912, 485969152, 487251392, 488533632, 489815872, 491098112, 492380352, 493662592, 494944832, 496227072, 497509312, 498791552, 500073792, 501356032, 502638272,
 503920512, 505202752, 506484992, 507767232, 509049472, 510331712, 511613952, 512896192, 514178432, 515460672, 516742912, 518025152, 519307392, 520589632, 521871872, 523154112, 524436352, 525718592, 527000832, 528283072, 529565312, 530847552, 532129792, 533412032,
 534694272, 535976512, 537258752, 538540992, 539823232, 541105472, 542387712, 543669952, 544952192, 546234432, 547516672, 548798912, 550081152, 551363392, 552645632, 553927872, 555210112, 556492352, 557774592, 559056832, 560339072, 561621312, 562903552, 564185792,
 565468032, 566750272, 568032512, 569314752, 570596992, 571879232, 573161472, 574443712, 575725952, 577008192, 578290432, 579572672, 580854912, 582137152, 583419392, 584701632, 585983872, 587266112, 588548352, 589830592, 591112832, 592395072, 593677312, 594959552,
 596241792, 597524032, 598806272, 600088512, 601370752, 602652992, 603935232, 605217472, 606499712, 607781952, 609064192, 610346432, 611628672, 612910912, 614193152, 615475392, 616757632, 618039872, 619322112, 620604352, 621886592, 623168832, 624451072, 625733312,
 627015552, 628297792, 629580032, 630862272, 632144512, 633426752, 634708992, 635991232, 637273472, 638555712, 639837952, 641120192, 642402432, 643684672, 644966912, 646249152, 647531392, 648813632, 650095872, 651378112, 652660352, 653942592, 655224832, 656507072,
 657789312, 659071552, 660353792, 661636032, 662918272, 664200512, 665482752, 666764992, 668047232, 669329472, 670611712, 671893952, 673176192, 674458432, 675740672, 677022912, 678305152, 679587392, 680869632, 682151872, 683434112, 684716352, 685998592, 687280832,
 688563072, 689845312, 691127552, 692409792, 693692032, 694974272, 696256512, 697538752, 698820992, 700103232, 701385472, 702667712, 703949952, 705232192, 706514432, 707796672, 709078912, 710361152, 711643392, 712925632, 714207872, 715490112, 716772352, 718054592,
 719336832, 720619072, 721901312, 723183552, 724465792, 725748032, 727030272, 728312512, 729594752, 730876992, 732159232, 733441472, 734723712, 736005952, 737288192, 738570432, 739852672, 741134912, 742417152, 743699392, 744981632, 746263872, 747546112, 748828352,
 750110592, 751392832, 752675072, 753957312, 755239552, 756521792, 757804032, 759086272, 760368512, 761650752, 762932992, 764215232, 765497472, 766779712, 768061952, 769344192, 770626432, 771908672, 773190912, 774473152, 775755392, 777037632, 778319872, 779602112,
 780884352, 782166592, 783448832, 784731072, 786013312, 787295552, 788577792, 789860032, 791142272, 792424512, 793706752, 794988992, 796271232, 797553472, 798835712, 800117952, 801400192, 802682432, 803964672, 805246912, 806529152, 807811392, 809093632, 810375872,
 811658112, 812940352, 814222592, 815504832, 816787072, 818069312, 819351552, 820633792, 821916032, 823198272, 824480512, 825762752, 827044992, 828327232, 829609472, 830891712, 832173952, 833456192, 834738432, 836020672, 837302912, 838585152, 839867392, 841149632,
 842431872, 843714112, 844996352, 846278592, 847560832, 848843072, 850125312, 851407552, 852689792, 853972032, 855254272, 856536512, 857818752, 859100992, 860383232, 861665472, 862947712, 864229952, 865512192, 866794432, 868076672, 869358912, 870641152, 871923392,
 873205632, 874487872, 875770112, 877052352, 878334592, 879616832, 880899072, 882181312, 883463552, 884745792, 886028032, 887310272, 888592512, 889874752, 891156992, 892439232, 893721472, 895003712, 896285952, 897568192, 898850432, 900132672, 901414912, 902697152,
 903979392, 905261632, 906543872, 907826112, 909108352, 910390592, 911672832, 912955072, 914237312, 915519552, 916801792, 918084032, 919366272, 920648512, 921930752, 923212992, 924495232, 925777472, 927059712, 928341952, 929624192, 930906432, 932188672, 933470912,
 934753152, 936035392, 937317632, 938599872, 939882112, 941164352, 942446592, 943728832, 945011072, 946293312, 947575552, 948857792, 950140032, 951422272, 952704512, 953986752, 955268992, 956551232, 957833472, 959115712, 960397952, 961680192, 962962432, 964244672,
 965526912, 966809152, 968091392, 969373632, 970655872, 971938112, 973220352, 974502592, 975784832
# mkdir -p /mnt/da0
# mount /dev/da0p1 /mnt/da0

Si nous ne voulons pas copier récursivement le contenu du répertoire /mnt, nous devons l'exclure de notre commande de copie. Ne sachant pas utiliser csh, nous allons écrire un script bash.

# for d in /* /.* ; do if [ $d != "/." -a $d != "/.." -a $d != "/mnt" ] ; then cp -av $d /mnt/da0 ; fi ; done

Une fois les données de notre partition racine sauvegardées sur un disque externe, nous allons créer une clef USB amorçable pour pour démarrer la machine, modifier la configuration de son disque et recopier les données stockées sauvegardées.

$ curl -# -o FreeBSD-12.1-RELEASE-amd64-memstick.img.xz ftp://ftp.freebsd.org/pub/FreeBSD/releases/amd64/amd64/ISO-IMAGES/12.1/FreeBSD-12.1-RELEASE-amd64-memstick.img.xz
################################################################################################################################## 100,0%
$ xz -d FreeBSD-12.1-RELEASE-amd64-memstick.img.xz
# dd if=/home/triaxx/FreeBSD-12.1-RELEASE-amd64-memstick.img of=/dev/da1 bs=40m status=progress
  964689920 bytes (965 MB, 920 MiB) transferred 230.016s, 4194 kB/s
23+1 records in
23+1 records out
995009024 bytes transferred in 240.330850 secs (4140164 bytes/sec)

Maintenant que la clef USB amorçable est prête, nous redémarrons notre machine pour charger le système qui y est installé. Bien évidemment, le BIOS ne nous permet pas de lancer directement le système depuis la clef et charge directement le programme d'amorçage de FreeBSD. Heureusement, nous pourrons lancer le système de la clef depuis ce dernier.

Type '?' for a list of commands. 'help' for more detailed help.
OK lsdev
disk devices:
    disk0:   BIOS drive C (156301488 X 512):
      disk0p1: FreeBSD boot
      disk0p2: FreeBSD UFS
      disk0p3: FreeBSD swap
    disk1:   BIOS drive D (7829504 X 512):
      disk1s1: Unknown
      disk1s2: FreeBSD
        disk1s2a: FreeBSD UFS
OK set currdev=disk1s2a
OK boot
Loading kernel..

Nous choisissons de lancer le Live CD et nous nous connectons comme utilisateur root. Comme il est plus simple de rester assis à son bureau que de pianoter debout (la machine est empêtrée sur une étagère), nous configurerons un accès SSH. Il faudra pour

# passwd
Changing local password for root
New Password:
Retype New Password:
# sed -i -e "s,#PermitRootLogin no,PermitRootLogin yes," /etc/ssh/sshd_config
# dhclient rl0
DHCPREQUEST on rl0 to 255.255.255.255 port 67
DHCPACK from 192.168.0.254
bound to 192.168.0.9 -- renewal in 21600 seconds.
# service sshd onestart
Performing sanity check on sshd configuration.
Starting sshd.

Nous pouvons maintenant modifier notre disque interne pour recréer une partition de swap plus grande.

# gpart show ada0
=>       40  156301408  ada0  GPT  (75G)
         40       1024     1  freebsd-boot  (512K)
       1064  148896768     2  freebsd-ufs  (71G)
  148897832    7403615     3  freebsd-swap  (3.5G)
  156301447          1        - free -  (512B)

# gpart delete -i 3 ada0
ada0p3 deleted
# gpart delete -i 2 ada0
ada0p2 deleted
# gpart add -t freebsd-ufs -s 71435861504b -l fbsddata ada0
ada0p2 added
# gpart add -t freebsd-swap -l fbsdswap ada0
ada0p3 added
root@:~ # gpart show ada0
=>       40  156301408  ada0  GPT  (75G)
         40       1024     1  freebsd-boot  (512K)
       1064  139523167     2  freebsd-ufs  (67G)
  139524231   16777217     3  freebsd-swap  (8.0G)

Nous créons maintenant le système de fichiers pour notre partition racine afin d'y copier les fichiers que nous avions précédemment sauvegardés. Attention, comme nous avons démarrer le système avec la clef amorçable puis branché le disque externe, la clef est attachée au périphérique da0 alors que le disque l'est au périphérique da1 (c'était l'inverse avant le redémarrage sur la clef). Encore une fois, il faut exclure de la copie les répertoires . et ... Le bash n'est pas installé sur le système de la clef amorçable et il va donc falloir écrire ça en csh. Bon, ce n'est pas si compliqué finalement...

# newfs -L fbsddata /dev/ada0p2
/dev/ada0p2: 68126.5MB (139523160 sectors) block size 32768, fragment size 4096
	using 109 cylinder groups of 626.09MB, 20035 blks, 80256 inodes.
super-block backups (for fsck_ffs -b #) at:
 192, 1282432, 2564672, 3846912, 5129152, 6411392, 7693632, 8975872, 10258112, 11540352, 12822592, 14104832, 15387072, 16669312, 17951552, 19233792, 20516032, 21798272, 23080512, 24362752, 25644992, 26927232, 28209472, 29491712, 30773952, 32056192, 33338432, 34620672,
 35902912, 37185152, 38467392, 39749632, 41031872, 42314112, 43596352, 44878592, 46160832, 47443072, 48725312, 50007552, 51289792, 52572032, 53854272, 55136512, 56418752, 57700992, 58983232, 60265472, 61547712, 62829952, 64112192, 65394432, 66676672, 67958912, 69241152,
 70523392, 71805632, 73087872, 74370112, 75652352, 76934592, 78216832, 79499072, 80781312, 82063552, 83345792, 84628032, 85910272, 87192512, 88474752, 89756992, 91039232, 92321472, 93603712, 94885952, 96168192, 97450432, 98732672, 100014912, 101297152, 102579392,
 103861632, 105143872, 106426112, 107708352, 108990592, 110272832, 111555072, 112837312, 114119552, 115401792, 116684032, 117966272, 119248512, 120530752, 121812992, 123095232, 124377472, 125659712, 126941952, 128224192, 129506432, 130788672, 132070912, 133353152,
 134635392, 135917632, 137199872, 138482112
# mkdir -p /mnt/da1 /mnt/ada0
# mount /dev/da1p1 /mnt/da1
# mount /dev/ada0p2 /mnt/ada0
# foreach d ( /mnt/da1/* /mnt/da1/.* )
foreach? if ( $d != "/mnt/da1/." && $d != "/mnt/da1/.." ) then
foreach? cp -av $d /mnt/ada0
foreach? endif
foreach? end

Une fois la copie terminée, il ne nous reste plus qu'à démonter les disques attachés et à redémarrer la machine.

 hlfs shell
# umount /mnt/da1
# umount /mnt/ada0
# reboot
# gpart show ada0
=>       40  156301408  ada0  GPT  (75G)
         40       1024     1  freebsd-boot  (512K)
       1064  139523167     2  freebsd-ufs  (67G)
  139524231   16777217     3  freebsd-swap  (8.0G)

Page top