问题原文
原帖链接 · Leszek · CC BY-SA 3.0
One of my day-to-day tasks is to create a bootable image of a USB pendrive with our software on it. The image must consist of two partitions, first one formatted with FAT32 and second with EXT3. On the first we put Linux kernel and a small startup script that the bootloader on the device we are going to boot with reads on startup.
On the second partition we put our rootfs.
Now, here's how I do it:
insert a USB pendrive into my Linux comp. Figure out its device file (here let's assume /dev/sdx)
zero out the first 600 MB of it: dd if=/dev/zero of=/dev/sdx bs=1M count=600
Create the two partitions, first 10 MB and second 500 MB: sfdisk /dev/sdx --unit S << EOF ,20000,c ,1000000,83 EOF
mkfs.vfat /dev/sdx1 mkfs.ext3 /dev/sdx2
(that's assuming sector size is 512 bytes!)
- copy the kernel and startup script to /dev/sdx1
- Unpack pre-compiled RootFS to /dev/sdx2
- Copy the ready-made image from USB pendrive back to our HDD: dd if=/dev/sdx of=usb.img bs=1M count=520
- Compress it and send to the people who test this software.
- the people then uncompress, flash their USB disks with dd if=usb.img of=/dev/sdx insert into their devices and test.
Now, all of this works. The obvious improvement is to create a script that would do this all automatically. This can also be done, but one manual step will still remain: insert the USB pendrive.
I am thinking instead of the actual USB disk we could use a block device in RAM. This not only would eliminate the manual step of inserting the USB disk, but would also be faster.
So what I need is a plain block device in RAM. One that can be split into partitions just like a physical USB pendrive can.
I just researched this topic and seems like the only options are
- tmpfs and ramfs, which obviously don't suit me
- ramdisk and the 'brd' kernel module, which, when loaded, creates a number of 'partitions': /dev/ram[0-15]
and each of them can individually be formatted and used, but like I said - I want a plain block device in RAM which I would then split into two partitions of 10MB and 500MB myself, and from where I would be then able to dd an image to a file on my HDD.
What are my options?
社区高分解答
原帖链接 · Hi-Angel · CC BY-SA 4.0
This is an analog to Fox's answer, but ram disk is used instead of tmpfs. In my tests with vdbench, ram disk shows higher IOPS than tmpfs (33k IOPS vs 29k IOPS on my machine in three consecutive tests, i.e. 6 tests overall), and it is easier to setup too.
# the following creates 1 device /dev/ram0 of size 600M. More elaborate parameters
# description you can see with `modinfo brd | grep parm`, ATM there's just three.
modprobe brd rd_nr=1 rd_size=614400
# Partition the image file. The following creates two GPT partitions: one of size
# 300M, and the other one takes all space that's left
sgdisk -Z --new 1::+300M --new 2 /dev/ram0
# Create filesystems
mkfs.vfat /dev/ram0p1
mkfs.ext4 /dev/ram0p2
# Now you can mount ram0p1 and ram0p2 as you would with a usual block device and copy
# your stuff there. After you `umount` it, you get a partitioned `/dev/ram0` block
# device with your files. You can make an image of it with `dd if=/dev/ram0 of=img.bin`
# for example. After you're done, to remove the ram device execute `rmmod brd`.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
