首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分离创建准备工作的.img

分离创建准备工作的.img
EN

Unix & Linux用户
提问于 2018-03-25 17:49:31
回答 1查看 1.2K关注 0票数 1

在问题“分隔命令行没有得到相同的结果”中,答案被选择为正确(要创建带有“parted”的IMG文件系统和分区)是:

代码语言:javascript
复制
# 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         lba

fat32 32/ext4 4也是如此。但是,当我在/dev/循环(sudo losetup loop1 MyDrive.img)中挂载映像时,它不工作(unknown partition)。

所以秩序是不完整的。

有人可以帮助我完成为ext4 4/ntfs/ for 32 (GPTMSDOS)创建ext4 4/ntfs/for 32(D4D5)的顺序,以便在循环中安装它(准备工作)

谢谢!

EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2018-03-25 21:35:54

我将提供您所要求的方法,如果不需要分区的话,也将提供一个简单得多的方法。我还将只做ext4示例,其他的应该是可能的:

带有分区的图像文件:

代码语言:javascript
复制
#!/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

没有分区的图像文件:

代码语言:javascript
复制
#!/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脚本来表达这个答案。

票数 2
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/433449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档