如何在Linux中获得/dev/one的等效项

ycggw6v2  于 2023-01-12  发布在  Linux
关注(0)|答案(6)|浏览(193)

您可以使用

dd if=/dev/zero of=file count=1024 bs=1024

将文件归零
而不是我想一个填充一个文件。我该怎么做呢?
没有**/dev/one文件,那么我如何通过bash shell来模拟这种效果呢?

wfveoks0

wfveoks01#

tr '\0' '\377' < /dev/zero | dd bs=64K of=/dev/sdx

这应该是快得多。选择您的块大小(或添加计数)像你需要在。写入一个SSD磁盘,直到充满了99 M的块大小给了我350 M/s的写入性能。

ff29svar

ff29svar2#

试试这个:

dd if=<(yes $'\01' | tr -d "\n") of=file count=1024 bs=1024

如果希望所有位都为1,则替换为$'\377'$'\xFF'
MacOS tr可能会抱怨"非法字节序列"。设置LC_CTYPE=C可以防止这种情况。此版本也可以在Linux中使用:

dd if=<(yes $'\01' | LC_CTYPE=C tr -d "\n") of=file count=1024 bs=1024
jogvjijk

jogvjijk3#

你可以这么做

dd if=/dev/zero count=1024 bs=1024 |
  tr '\000' '\001' > file
unguejic

unguejic4#

pv /dev/zero |tr \\000 \\377 >targetfile

...其中\377255的八进制表示(所有位都设置为1的字节)。为什么tr只适用于八进制数,我不知道--但注意不要下意识地将其翻译为3FF
使用tr的语法很容易出错。我建议验证它是否正在进行所需的翻译...

cat /dev/zero |tr \\000 \\377 |hexdump -C

注:pv是一个很好的实用程序,它取代了cat,并添加了进度/速率显示。

fwzugrvs

fwzugrvs5#

我在我的github中创建了一个设备驱动程序。安装它会创建一个文件/dev/one,该文件只写入设置为1的位。
名为one.c的c文件(唯一有趣的部分在device_file_read中):

// File Driver to create a devince /dev/one like the /dev/zero

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>

MODULE_LICENSE("GPL");

static int device_file_major_number = 0;
static const char device_name[] = "one";

static ssize_t device_file_read(
        struct file *file_ptr,
        char __user *user_buffer,
        size_t count,
        loff_t *position) {
    printk( KERN_NOTICE "One: Device file is read at offset = %i, read bytes count = %u\n" , (int)*position , (unsigned int)count );

    // Allocate Kernel buffer
    char* ptr = (char*) vmalloc(count);

    // Fill it with one, byte per byte
    // -- Note that byte is the smallest accesible data unit
    memset(ptr, 0xFF, count);

    char res = copy_to_user(user_buffer, ptr, count);
    if (res != 0){ return -EFAULT; }

    // Return number of byte read
    return count;
}

static struct file_operations simple_driver_fops = {
    .owner   = THIS_MODULE,
    .read    = device_file_read,
};

int register_device(void) {
    int res = 0;
    printk( KERN_NOTICE "One: register_device() is called.\n" );
    res = register_chrdev( 0, device_name, &simple_driver_fops );
    if( res < 0 ) {
        printk( KERN_WARNING "One:  can\'t register character device with error code = %i\n", res );
        return res;
    }
    device_file_major_number = res;
    printk( KERN_NOTICE "One: registered character device with major number = %i and minor numbers 0...255\n", device_file_major_number );
    return 0;
}

void unregister_device(void) {
    printk( KERN_NOTICE "One: unregister_device() is called\n" );
    if(device_file_major_number != 0) {
        unregister_chrdev(device_file_major_number, device_name);
    }
}

static int my_init(void) {
    register_device();
    return 0;
}

static void my_exit(void) {
    unregister_device();
    return;
}

// Declare register and unregister command
module_init(my_init);
module_exit(my_exit);

生成文件

TARGET_MODULE:=one

BUILDSYSTEM_DIR:=/lib/modules/$(shell uname -r)/build
PWD:=$(shell pwd)
obj-m := $(TARGET_MODULE).o
# See: https://stackoverflow.com/questions/15910064/how-to-compile-a-linux-kernel-module-using-std-gnu99
ccflags-y := -std=gnu99 -Wno-declaration-after-statement

build:
    # run kernel build system to make module
    $(MAKE) -C $(BUILDSYSTEM_DIR) M=$(PWD) modules

clean:
    # run kernel build system to cleanup in current directory
    $(MAKE) -C $(BUILDSYSTEM_DIR) M=$(PWD) clean
    rm -f MOK.priv MOK*.der

key:
    echo "Creating key"
    openssl req -new -x509 -newkey rsa:2048 -days 36500 -keyout MOK.priv -outform DER -out MOK.der -nodes -subj "/CN=TinmarinoUnsafe/"
    #
    echo "\e[31;1mPlease enter a password you will be asked for on reboot:\e[0m"
    mokutil --import MOK.der
    echo "\e[31;1mNow you must: 1/ reboot, 2/ Select Unroll MOK, 3/ Enter password you previously gave\e[0m"

sign:
    cp one.ko one.ko.bck
    /usr/src/linux-headers-$(shell uname -r)/scripts/sign-file sha256 MOK.priv MOK.der one.ko

load:
    insmod ./$(TARGET_MODULE).ko

unload:
    rmmod ./$(TARGET_MODULE).ko

create:
    mknod /dev/one c $(shell cat /proc/devices | grep one$ | cut -d ' ' -f1) 0

delete:
    rm /dev/one

test:
    [ "$(shell xxd -p -l 10 /dev/one)" = "ffffffffffffffffffff" ] \
        && echo "\e[32mSUCCESS\e[0m" \
        || echo "\e[31mFAILED\e[0m"

由于驱动程序签名强制,安装时间很长(3分钟)。如果您在UEFI中禁用了这一部分,请忘记它。

  1. git clone https://github.com/tinmarino/dev_one.git DevOne && cd DevOne # Download
  2. make build # Compile
  3. make key # Generate key for signing
  4. sudo make sign # Sign driver module to permit MOK enforcement (security)
  5. sudo reboot now # Reboot and enable Mok
    1.将出现蓝屏(MOK管理器
    1.选择“注册MOK”
    1.选择“继续”
    1.选择“是”(当询问“注册密钥”时)
    1.输入您在make sign处提供的密码
    1.选择“重新启动”(再次)
  6. sudo make load # Load
  7. sudo make device # Create /dev/one
  8. make test # Test if all is ok
ukdjmx9f

ukdjmx9f6#

您可以使用FIFO + yes来模拟/dev/one,而无需特殊设备:

mkfifo ddfifo
dd if=ddfifo of=<file> iflag=fullblock count=1024 bs=1024 status=progress & yes "" | tr '\n' '\1' > ddfifo

tee可用于使吞吐量加倍:

mkfifo ddfifo
dd if=ddfifo of=<file> iflag=fullblock count=1024 bs=1024 status=progress & yes "" | tr '\n' '\1' | tee ddfifo > ddfifo

如果您希望字节的所有位都设置为1,请将'\1'替换为'\377'

相关问题