Ramdisk on linux boot

Using the RAM disk block device with Linux¶

The RAM disk driver is a way to use main system memory as a block device. It is required for initrd, an initial filesystem used if you need to load modules in order to access the root filesystem (see Using the initial RAM disk (initrd) ). It can also be used for a temporary filesystem for crypto work, since the contents are erased on reboot.

The RAM disk dynamically grows as more space is required. It does this by using RAM from the buffer cache. The driver marks the buffers it is using as dirty so that the VM subsystem does not try to reclaim them later.

The RAM disk supports up to 16 RAM disks by default, and can be reconfigured to support an unlimited number of RAM disks (at your own risk). Just change the configuration symbol BLK_DEV_RAM_COUNT in the Block drivers config menu and (re)build the kernel.

To use RAM disk support with your system, run ‘./MAKEDEV ram’ from the /dev directory. RAM disks are all major number 1, and start with minor number 0 for /dev/ram0, etc. If used, modern kernels use /dev/ram0 for an initrd.

The new RAM disk also has the ability to load compressed RAM disk images, allowing one to squeeze more programs onto an average installation or rescue floppy disk.

2) Parameters¶

2a) Kernel Command Line Parameters

ramdisk_size=N

Size of the ramdisk.

This parameter tells the RAM disk driver to set up RAM disks of N k size. The default is 4096 (4 MB).

rd_nr

/dev/ramX devices created.

max_part

Maximum partition number.

rd_size

See ramdisk_size.

3) Using «rdev»¶

«rdev» is an obsolete, deprecated, antiquated utility that could be used to set the boot device in a Linux kernel image.

Instead of using rdev, just place the boot device information on the kernel command line and pass it to the kernel from the bootloader.

You can also pass arguments to the kernel by setting FDARGS in arch/x86/boot/Makefile and specify in initrd image by setting FDINITRD in arch/x86/boot/Makefile.

Читайте также:  Linux mark file as executable

Some of the kernel command line boot options that may apply here are:

ramdisk_start=N ramdisk_size=M

If you make a boot disk that has LILO, then for the above, you would use:

append = "ramdisk_start=N ramdisk_size=M"

4) An Example of Creating a Compressed RAM Disk¶

To create a RAM disk image, you will need a spare block device to construct it on. This can be the RAM disk device itself, or an unused disk partition (such as an unmounted swap partition). For this example, we will use the RAM disk device, «/dev/ram0».

Note: This technique should not be done on a machine with less than 8 MB of RAM. If using a spare disk partition instead of /dev/ram0, then this restriction does not apply.

    Decide on the RAM disk size that you want. Say 2 MB for this example. Create it by writing to the RAM disk device. (This step is not currently required, but may be in the future.) It is wise to zero out the area (esp. for disks) so that maximal compression is achieved for the unused blocks of the image that you are about to create:

dd if=/dev/zero of=/dev/ram0 bs=1k count=2048
dd if=/dev/ram0 bs=1k count=2048 | gzip -v9 > /tmp/ram_image.gz
dd if=zImage of=/dev/fd0 bs=1k
dd if=/tmp/ram_image.gz of=/dev/fd0 bs=1k seek=400

That is it. You now have your boot/root compressed RAM disk floppy. Some users may wish to combine steps (d) and (f) by using a pipe.

Changelog:¶

Updated to reflect changes in command line options, remove obsolete references, general cleanup. James Nelson (james4765 @ gmail . com)

Источник

Создаём RAMdisk на Ubuntu 18.04

После чего проверяем правильность создания рамдиска командой:

И видим следующий результат:

Filesystem Size Used Avail Use% Mounted on
udev 2.0G 0 2.0G 0% /dev
tmpfs 395M 620K 394M 1% /run
/dev/vda2 89G 74.5G 580G 6% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
tmpfs 395M 0 395M 0% /run/user/1000
tmpfs 2.0G 0 2.0G 0% /mnt/ramdisk

Теперь вы можете использовать рамдиск как вы используете обычные разделы жесткого диска. Вы даже можете редактировать любые файлы прямо из РАМДИСКА.

Чтобы отключить виртуальный диск (РАМДИСК), выполните следующую команду (все ваши файлы на этом разделе будут моментально удалены):

Подключение RAM-диска на UBUNTU 18.04 автоматически, при загрузке системы

Вы можете использовать файл /etc/fstab для автоматического монтирования виртуального диска при загрузке системы.

Читайте также:  Сервер линукс своими руками

Во-первых, сделайте резервную копию файла /etc/fstab. Можно с помощью следующей команды:

sudo cp -v /etc/fstab /etc/fstab.backup

Резервная опия файла /etc/fstab сохранится в /etc/fstab.backup. Теперь если что-то случится в процессе, вы сможете просто восстановить файл /etc/fstab.

Открываем файл /etc/fstab:

После чего в конце файла добавляем следующую строчку:

tmpfs /mnt/ramdisk tmpfs rw,size=2G 0 0

После чего перезагружаем сервер командой:

После загрузки системы РАМдиск должен быть автоматически смонтирован (нужно проверить):

. 
tmpfs 2.0G 0 2.0G 0% /mnt/ramdisk
.

Сохранение данных RAM-диска:

Далее я покажу, как сохранить данные рамдиска на десткий диск при завершении работы системы и загрузить данные с жесткого диска в RAM-диск при загрузке системы.

Для этого создадим каталог, где диск будет смонтирован и где резервные копии данных будут сохраняться должны иметь одинаковый набор прав доступа к файлам.

Сначала создайте каталог, где данных RAM-диска будет сохранен при выключении системы с помощью следующей команды:

sudo mkdir /mnt/ramdisk_backup

Теперь создайте файл системной службы с помощью следующей команды:

sudo nano /lib/systemd/system/ramdisk-sync.service

Рубрики

При заказе по этой ссылке бесплатно помогу настроить и подготовить сервер к повседневной эксплуатации.

Источник

Using the RAM disk block device with Linux¶

The RAM disk driver is a way to use main system memory as a block device. It is required for initrd, an initial filesystem used if you need to load modules in order to access the root filesystem (see Using the initial RAM disk (initrd) ). It can also be used for a temporary filesystem for crypto work, since the contents are erased on reboot.

The RAM disk dynamically grows as more space is required. It does this by using RAM from the buffer cache. The driver marks the buffers it is using as dirty so that the VM subsystem does not try to reclaim them later.

The RAM disk supports up to 16 RAM disks by default, and can be reconfigured to support an unlimited number of RAM disks (at your own risk). Just change the configuration symbol BLK_DEV_RAM_COUNT in the Block drivers config menu and (re)build the kernel.

To use RAM disk support with your system, run ‘./MAKEDEV ram’ from the /dev directory. RAM disks are all major number 1, and start with minor number 0 for /dev/ram0, etc. If used, modern kernels use /dev/ram0 for an initrd.

The new RAM disk also has the ability to load compressed RAM disk images, allowing one to squeeze more programs onto an average installation or rescue floppy disk.

Читайте также:  Hello world linux mint

2) Parameters¶

2a) Kernel Command Line Parameters

ramdisk_size=N

Size of the ramdisk.

This parameter tells the RAM disk driver to set up RAM disks of N k size. The default is 4096 (4 MB).

rd_nr

/dev/ramX devices created.

max_part

Maximum partition number.

rd_size

See ramdisk_size.

3) Using «rdev»¶

«rdev» is an obsolete, deprecated, antiquated utility that could be used to set the boot device in a Linux kernel image.

Instead of using rdev, just place the boot device information on the kernel command line and pass it to the kernel from the bootloader.

You can also pass arguments to the kernel by setting FDARGS in arch/x86/boot/Makefile and specify in initrd image by setting FDINITRD in arch/x86/boot/Makefile.

Some of the kernel command line boot options that may apply here are:

ramdisk_start=N ramdisk_size=M

If you make a boot disk that has LILO, then for the above, you would use:

append = "ramdisk_start=N ramdisk_size=M"

4) An Example of Creating a Compressed RAM Disk¶

To create a RAM disk image, you will need a spare block device to construct it on. This can be the RAM disk device itself, or an unused disk partition (such as an unmounted swap partition). For this example, we will use the RAM disk device, «/dev/ram0».

Note: This technique should not be done on a machine with less than 8 MB of RAM. If using a spare disk partition instead of /dev/ram0, then this restriction does not apply.

    Decide on the RAM disk size that you want. Say 2 MB for this example. Create it by writing to the RAM disk device. (This step is not currently required, but may be in the future.) It is wise to zero out the area (esp. for disks) so that maximal compression is achieved for the unused blocks of the image that you are about to create:

dd if=/dev/zero of=/dev/ram0 bs=1k count=2048
dd if=/dev/ram0 bs=1k count=2048 | gzip -v9 > /tmp/ram_image.gz
dd if=zImage of=/dev/fd0 bs=1k
dd if=/tmp/ram_image.gz of=/dev/fd0 bs=1k seek=400

That is it. You now have your boot/root compressed RAM disk floppy. Some users may wish to combine steps (d) and (f) by using a pipe.

Changelog:¶

Updated to reflect changes in command line options, remove obsolete references, general cleanup. James Nelson (james4765 @ gmail . com)

Источник

Оцените статью
Adblock
detector