Skip to content

Instantly share code, notes, and snippets.

@Dpbm
Last active November 28, 2022 17:34
Show Gist options
  • Save Dpbm/592328416141797019e88c4b2286f4d6 to your computer and use it in GitHub Desktop.
Save Dpbm/592328416141797019e88c4b2286f4d6 to your computer and use it in GitHub Desktop.

Brincando com o comando DD do linux

/dev/zero

    dd if=/dev/zero of=test.iso bs=1M count=20
    xxd test.iso

    00000000: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00000010: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00000020: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00000030: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    ....

    sudo losetup /dev/loop10 test.iso
    sudo mkfs.ext4 /dev/loop10
    sudo mount /dev/loop10 /mnt
    cd /mnt

    ls -la

    drwxr-xr-x 3 root root  1024 Nov 28 12:34 .
    drwxr-xr-x 1 user root    40 Nov 28 12:33 ..
    drwx------ 2 root root 12288 Nov 28 12:34 lost+found

/dev/random

    dd if=/dev/random of=test.iso bs=1M count=20
    xxd test.iso

    00000000: ec72 e7af a998 e1a9 3521 0a78 2bc0 5de2  .r......5!.x+.].
    00000010: bc96 7fb5 b66c d988 e193 5db1 280b 67ee  .....l....].(.g.
    00000020: 1400 abfe b625 2484 ce01 ab95 163e 27e5  .....%$......>'.
    00000030: 50a8 e009 17e1 5986 e186 8bd4 dceb 2377  P.....Y.......#w
    00000040: 17c6 fad5 8e55 1290 461d 9830 a316 c9d6  .....U..F..0....
    ....

    sudo losetup /dev/loop10 test.iso
    sudo mkfs.ext4 /dev/loop10
    sudo mount /dev/loop10 /mnt
    cd /mnt

    ls -la

    drwxr-xr-x 3 root root  1024 Nov 28 12:34 .
    drwxr-xr-x 1 user root    40 Nov 28 12:33 ..

zip

    mkdir data
    cd data

    echo "hello world" >> text.txt
    curl -o image.png https://images.unsplash.com/photo-1649181817796-6d1ec9524754?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=683&q=80
    
    cd ..

    zip data.zip ./data/*

    # ir e voltar com o arquivo
    dd if=data.zip of=data.iso 
    dd if=data.iso of=data2.zip

    
    # criando uma iso com o arquivo text.txt e image.png dentro
    dd if=/dev/zero of=test.iso bs=1M count=20
    sudo losetup /dev/loop10 test.iso
    sudo mkfs.ext4 /dev/loop10
    sudo mount /dev/loop10 /mnt
    cp ./data/* ./mnt
    cd ..
    sudo umount -R ./mnt
    sudo losetup -d /dev/loop

Usando C para gerar esse .iso randômico (como usando o /dev/random)

#make_iso.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main()
{
    FILE *iso_file;
    iso_file = fopen("data.iso", "ab");
    srand(time(NULL));

    if (iso_file == NULL)
    {
        printf("Error NULL File!");
        exit(1);
    }

    // units
    unsigned int M = 1048576;
    unsigned int K = 1024;

    // write 10Kb
    unsigned long total_characters = (K * 10) / sizeof(int);
    unsigned long mapped_bytes = 0;

    unsigned int buffer_size = 512;
    unsigned int data[buffer_size];
    unsigned int i = 0;

    while (mapped_bytes < total_characters)
    {
        data[i] = rand();

        if (i % (buffer_size - 1) == 0 && i != 0)
        {
            printf("writing...\ntotal mapped bytes: %lu\n", mapped_bytes);

            i = 0;

            fwrite(data, 1, sizeof(data), iso_file);
            memset(data, 0, buffer_size);
            sleep(1);
        }
        else
        {
            i++;
        }

        mapped_bytes++;
    }

    fclose(iso_file);

    return 0;
}
    gcc make_iso.c -o iso
    ./iso

    writing...
    total mapped bytes: 511
    writing...
    total mapped bytes: 1023
    writing...
    total mapped bytes: 1535
    writing...
    total mapped bytes: 2047
    writing...
    total mapped bytes: 2559
    writing...
    total mapped bytes: 3071
    ......
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment