2024. április 27., szombat

Gyorskeresés

Útvonal

Cikkek » Számtech rovat

Ramdisk Linux alatt

Ha nem futja SSD-re, vagy óvnánk a kicsikét, adjunk kis kakaót a RAM-nak.

[ ÚJ TESZT ]

A /tmp mappa áthelyezése

Az ideiglenes fájlokat a Linux a /tmp mappába teszi. Ezek következő indításra eltűnnek, de ez a mappa a háttértáron található.

Ha át szeretnénk helyezni ramdisk-be, meggyűlhet a bajunk. Ha rosszkor helyezzük át, búcsút mondhatunk minden grafikai cukiságnak (ideértve a tty-ket is). Megtörtént, hogy nem volt kép.

Írtam három pici szkriptet, amik megoldják a mappa áthelyezését.
Az első fájl a ramtemp_create.sh nevet kapta. Ez hozza létre az ideiglenes mappát, és állítja helyre az eredetit (hogy a fentebb leírt esemény ne következzen be).

#!/bin/bash
# This script creates the ramdisk tmp directory

ramdisk=/mnt/ram # ramdisk address
ramtmp=tmp # Ramdisk temp directory name
tempdir=/tmp # Temp directory address

# If the ramdisk temp directory is missing, make it and set mode, owner
if [ ! -r $ramdisk/$ramtmp ]; then
echo -e '\E[0;31m'"\033[1m[RAMDISK TEMP DIRECOTRY ($ramdisk/$ramtmp) NOT FOUND]\033[0m"
echo -e '\E[0;33m'"\033[1mCreating ramdisk temp directory\033[0m"
mkdir $ramdisk/$ramtmp
fi

chown root:root $ramdisk/$ramtmp
chmod 777 $ramdisk/$ramtmp
chmod +t $ramdisk/$ramtmp

echo "This is a mounted RAMdisk. The contents of this directory is stored entirely in the RAM." > $ramdisk/README
echo "Please be sure not to store important things here." >> $ramdisk/README
echo "When the computer shuts down or the power cuts off, things here will be purged." >> $ramdisk/README

# If the temp directory is in ramdisk at the execution of this script,
# remove the link and make an ordinary /tmp directory
if [ "$(readlink $tempdir)" == "$ramdisk/$ramtmp" ]; then
echo -e '\E[0;31m'"\033[1m[TEMP DIRECTORY IS RAMDISK BEFORE BOOT]\033[0m"
echo -e '\E[0;32m'"\033[1mThis can cause boot failure\033[0m"

# Remove old tmp file (symlink)
echo -e '\E[0;33m'"\033[1mRemoving symbolic link\033[0m"
rm $tempdir

echo -e '\E[0;33m'"\033[1mCreating ordinary /tmp directory\033[0m"
mkdir $tempdir
fi

# If the "ordinary" /tmp directory is missing create it and set mode, owner
if [ ! -r $tempdir ]; then
echo -e '\E[0;31m'"\033[1m[TEMP DIRECTORY IS MISSING]\033[0m"
echo -e '\E[0;32m'"\033[1mThis can cause boot failure\033[0m"

echo -e '\E[0;33m'"\033[1mCreating ordinary /tmp directory\033[0m"
mkdir $tempdir
fi

# Remove old tmp file (symlink)
echo -e '\E[0;33m'"\033[1mRemoving symbolic link\033[0m"
rm $tempdir

echo -e '\E[0;33m'"\033[1mCreating ordinary /tmp directory\033[0m"
mkdir $tempdir

chown root:root $tempdir
chmod 777 $tempdir
chmod +t $tempdir

A második fájl a ramtemp_move.sh nevet kapta. Ez kapásból az első után fut le, a tartalom áthelyezéséről és a szimbolikus link beállításáról gondoskodik. Szimbolikus linket írtam, mivel, az áthelyezés után az eredeti /tmp mappa egy link lesz a ramdiskben található mappára.

#!/bin/bash
# This script move the tmp directory to ramdisk

ramdisk=/mnt/ram # ramdisk address
ramtmp=tmp # Ramdisk temp directory name
tempdir=/tmp # Temp directory address

# If the ramdisk temp directory is missing, make it and set mode, owner
if [ ! -r $ramdisk/$ramtmp ]; then
echo -e '\E[0;31m'"\033[1m[RAMDISK TEMP DIRECOTRY ($ramdisk/$ramtmp) NOT FOUND]\033[0m"
echo -e '\E[0;33m'"\033[1mCreating ramdisk temp directory\033[0m"
mkdir $ramdisk/$ramtmp
fi

chown root:root $ramdisk/$ramtmp
chmod 777 $ramdisk/$ramtmp
chmod +t $ramdisk/$ramtmp

# If the temp directory is in ramdisk at the execution of this script,
# remove the link and make an ordinary /tmp directory

# If the temp directory is not a symbolic link already to the
# ramdisk directory (intended :) ), make it a symlink
if [ "$(readlink $tempdir)" != "$ramdisk/$ramtmp" ]; then
echo -e '\E[0;31m'"\033[1m[TEMP DIRECTORY IS NOT SYMBOLIC LINK]\033[0m"
echo -e '\E[0;32m'"\033[1mThis is intended at this point\033[0m"

echo -e '\E[0;33m'"\033[1mMoving $tempdir ---> $ramdisk/$ramtmp\033[0m"

# Copy the files
sleep 1
rsync -av $tempdir/ $ramdisk/$ramtmp/

# Remove the original tmp directory
rm -rf $tempdir

# Create symbolic link
ln -s $ramdisk/$ramtmp $tempdir
fi

echo -e '\E[0;32m'"\033[1m[DONE]\033[0m"

A harmadik fájl leállításkor/újraindításkor fut le, ez végzi el a takarítást, és a helyreállítást. Neve ramtemp_restore.sh.

#!/bin/bash
# This script restores the /tmp directory's normal status

ramdisk=/mnt/ram # ramdisk address
ramtmp=tmp # Ramdisk temp directory name
tempdir=/tmp # Temp directory address

# Remove old tmp file (symlink)
echo -e '\E[0;33m'"\033[1mRemoving symbolic link\033[0m"
rm $tempdir

echo -e '\E[0;33m'"\033[1mCreating ordinary /tmp directory\033[0m"
mkdir $tempdir

chown root:root $tempdir
chmod 777 $tempdir
chmod +t $tempdir

Mindegyik szkript tartalmaz hibaellenőrzést, és konzolos kimenetet, ha esetleg kézzel futtatva szeretnénk egy aprócska állapotjelentést.

Ha nem szeretnétek copy-pastelni, a szkriptek letölthető formátumban is elérhetőek. ramtemp.tar.gz

Telepítés
Mentsük le ezt a három szkriptet a /etc mappába. ($ sudo cp ramtemp_create.sh /etc/ramtemp_create.sh, $ sudo cp ramtemp_move.sh /etc/ramtemp_move.sh, $ sudo cp ramtemp_restore.sh /etc/ramtemp_restore.sh)

A szkriptek első pár sorában található három változó. Ezeket igény szerint módosítsuk, ha a konfigurációnk eltér a cikkben szereplőtől. A három változó rendre:
ramdisk: a ramdisk elérési útvonala (alapesetben /mnt/ram)
ramtmp: a ramdiskben található tmp almappa neve (alapesetben tmp)
tempdir: a "hagyományos" tmp mappa elérési útvonala (alapesetben /tmp)

Jól látszik, hogy miről is beszélünk. Az eredeti /tmp mappánk ezentúl /mnt/ram/tmp-ként lesz ismert.

Nyissuk meg szerkesztésre az /etc/rc.local fájlt. Ez a fájl rendszerjoggal (root) futtatja le a bele írt dolgokat, még rendszerindításkor. Erre lesz most szükségünk. ($ sudo nano /etc/rc.local)

Egyszerűen írjuk bele a két fájl nevét, majd mentsük el.
A fájl végének valahogy így kell kinéznie:
/etc/ramtemp_create.sh
/etc/ramtemp_move.sh
exit 0

Az exit 0 fontos, ne távolítsuk el!

Mivel az rc.local alapból nem fut le, muszáj futtathatóvá tennünk. Írjuk be a következő parancsot: $ sudo chmod +x /etc/rc.local. Ez futtathatóvá teszi, következő indításkor már működni is fog a létrehozás.

A helyreállító szkript telepítéséhez két parancsot kell lefuttatnunk. Létre kell hoznunk két szimbolikus linket két mappában:
$ sudo ln -s ../ramtemp_restore.sh /etc/rc0.d/K10ramtemp_restore.sh
$ sudo ln -s ../ramtemp_restore.sh /etc/rc6.d/K10ramtemp_restore.sh

Előbbi (rc0.d) akkor végzi el a helyreállítást, ha a gépet leállításra utasítjuk, utóbbi (rc6.d) pedig akkor, ha újraindításra.

A mappa áthelyezése ezennel megtörtént.

A cikk még nem ért véget, kérlek, lapozz!

Hirdetés

Copyright © 2000-2024 PROHARDVER Informatikai Kft.