在问题“分隔命令行没有得到相同的结果”中,答案被选择为正确(要创建带有“parted”的IMG文件系统和分区)是:
# parted MyDrive.img \
mklabel msdos \
mkpart primary NTFS 1 1024 \
set 1 lba on \
align-check optimal 1 \
print
Model: (file)
Disk /dev/shm/MyDrive.img: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1074MB 1073MB primary ntfs lbafat32 32/ext4 4也是如此。但是,当我在/dev/循环(sudo losetup loop1 MyDrive.img)中挂载映像时,它不工作(unknown partition)。
所以秩序是不完整的。
有人可以帮助我完成为ext4 4/ntfs/ for 32 (GPT和MSDOS)创建ext4 4/ntfs/for 32(D4和D5)的顺序,以便在循环中安装它(准备工作)
谢谢!
发布于 2018-03-25 21:35:54
我将提供您所要求的方法,如果不需要分区的话,也将提供一个简单得多的方法。我还将只做ext4示例,其他的应该是可能的:
带有分区的图像文件:
#!/bin/sh
FILE=MyDrive.img
# create new 2Gb image file, will overwrite $FILE if it already exists
dd if=/dev/zero of=$FILE bs=1M count=2048
# make two 1Gb partitions and record the offsets
offset1=$(parted $FILE \
mklabel msdos \
mkpart primary ext2 1 1024 \
unit B \
print | awk '$1 == 1 {gsub("B","",$2); print $2}')
offset2=$(parted $FILE \
mkpart primary ext2 1024 2048 \
unit B \
print | awk '$1 == 2 {gsub("B","",$2); print $2}')
# loop mount the partitions and record the device
loop1=$(losetup -o $offset1 -f $FILE --show)
loop2=$(losetup -o $offset2 -f $FILE --show)
# create and mount the filesystems
mkdir -p /tmp/mnt{1,2}
mkfs.ext4 $loop1
mount $loop1 /tmp/mnt1
mkfs.ext4 $loop2
mount $loop2 /tmp/mnt2
# file write test
touch /tmp/mnt1/file_on_partition_1
touch /tmp/mnt2/file_on_partition_2
# cleanup
umount /tmp/mnt1 /tmp/mnt2
losetup -d $loop1 $loop2没有分区的图像文件:
#!/bin/sh
FILE=MyDrive.img
# create new 2Gb image file, will overwrite $FILE if it already exists
dd if=/dev/zero of=$FILE bs=1M count=2048
# create and mount filesystem
mkfs.ext4 -F $FILE
mount $FILE /mnt
# file write test
touch /tmp/mnt/file_in_imagefile
# cleanup
umount /mnt希望这是不言自明的,更容易用shell脚本来表达这个答案。
https://unix.stackexchange.com/questions/433449
复制相似问题