Shrinking images on Linux
When creating images from existing ISOs you often need to allocate a number of MB for the image to at least fit the files that are in the ISO. Predicting the exact size of the image is hard, even for a program. In this case you will create an image that is larger than actually needed: the image is much larger than the files on the image are combined.
This post will show how to shrink an existing image to a more optimal size. We will do this on Linux, since all required tools are available there: GParted, fdisk and truncate.
Requirements
- A Linux PC
- Some knowledge how the terminal works will helps
- The unoptimal image (myimage.img in this example)
Creating loopback device
GParted is a great application that can handle partition tables and filesystems quite well. In this tutorial we will use GParted to shrink the filesystem (and its accompaning partition in the partition table).
GParted operates on devices, not simple files like images. This is why we first need to create a device for the image. We do this using the loopback-functionality of Linux.
First we will enable loopback if it wasn't already enabled:
$ sudo modprobe loop
Now we can request a new (free) loopback device:
$ sudo losetup -f
This will return the path to a free loopback device. In this example this is /dev/loop0.
Next we create a device of the image:
$ sudo losetup /dev/loop0 myimage.img
Now we have a device /dev/loop0 that represents myimage.img. We want to access the partitions that are on the image, so we need to ask the kernel to load those too:
$ sudo partprobe /dev/loop0
This should give us the device /dev/loop0p1, which represents the first partition in myimage.img. We do not need this device directly, but GParted requires it.
Resize partition using GParted
Next we can load the device using GParted:
$ sudo gparted /dev/loop0
This should show a window similar to the following:

Now notice a few things:
- There is one partition.
- The partition allocates the entire disk/device/image.
- The partition is filled partly.
We want to resize this partition so that is fits it content, but not more than that.
Select the partition and click Resize/Move. A window similar to the following will popup:

Drag the right bar to the left as much as possible.
Note that sometimes GParted will need a few MB extra to place some filesystem-related data. You can press the up-arrow at the New size-box a few times to do so. For example, I pressed it 10 times (=10MiB) for FAT32 to work. For NTFS you might not need to at all.
Finally press Resize/Move. You will return to the GParted window. This time it will look similar to the following:

Notice that there is a part of the disk unallocated. This part of the disk will not be used by the partition, so we can shave this part off of the image later. GParted is a tool for disks, so it doesn't shrink images, only partitions, we have to do the shrinking of the image ourselves.
Press Apply in GParted. It will now move files and finally shrink the partition, so it can take a minute or two, most of the time it finishes quickly. Afterwards close GParted.
Now we don't need the loopback-device anymore, so unload it:
$ sudo losetup -d /dev/loop0
Shaving the image
Now that we have all the important data at the beginning of the image it is time to shave of that unallocated part. We will first need to know where our partition ends and where the unallocated part begins. We do this using fdisk:
$ fdisk -l myimage.img
Here we will see an output similar to the following:
Disk myimage.img: MB, bytes, sectors
Units = sectors of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x000ea37d Device Boot Start End Blocks Id System
myimage.img1 b W95 FAT32
Note two things in the output:
- The partition ends on block 9181183 (shown under End)
- The block-size is 512 bytes (shown as sectors of 1 * 512)
We will use these numbers in the rest of the example. The block-size (512) is often the same, but the ending block (9181183) will differ for you. The numbers mean that the parition ends on byte 9181183*512 of the file. After that byte comes the unallocated-part. Only the first 9181183*512 bytes will be useful for our image.
Next we shrink the image-file to a size that can just contain the partition. For this we will use the truncate command (thanks uggla!). With the truncate command need to supply the size of the file in bytes. The last block was 9181183 and block-numbers start at 0. That means we need (9181183+1)*512 bytes. This is important, else the partition will not fit the image. So now we use truncate with the calculations:
$ truncate --size=$[(+)*] myimage.img
Now copy the new image over to your phone, where it should act exactly the same as the old/big image.
[1]https://softwarebakery.com//shrinking-images-on-linux
Shrinking images on Linux的更多相关文章
- Linux 驱动开发
linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...
- Linux Process Virtual Memory
目录 . 简介 . 进程虚拟地址空间 . 内存映射的原理 . 数据结构 . 对区域的操作 . 地址空间 . 内存映射 . 反向映射 .堆的管理 . 缺页异常的处理 . 用户空间缺页异常的校正 . 内核 ...
- linux hugepage
The intent of this file is to give a brief summary of hugetlbpage support inthe Linux kernel. This ...
- Linux下的Libsvm使用历程录
原文:http://blog.csdn.net/meredith_leaf/article/details/6714144 Linux下的Libsvm使用历程录 首先下载Libsvm.Python和G ...
- linux内核分析之进程地址空间管理
1.struct task_struct 进程内核栈是操作系统为管理每一个进程而分配的一个4k或者8k内存大小的一片内存区域,里面存放了一个进程的所有信息,它能够完整的描述一个正在执行的程序:它打开的 ...
- Linux内存管理 (8)malloc
专题:Linux内存管理专题 关键词:malloc.brk.VMA.VM_LOCK.normal page.special page. 每章问答: malloc()函数是C函数库封装的一个核心函数,对 ...
- Kernel parameters for Db2 database server installation (Linux and UNIX)
Db2 11.1 For root installations, the database manager uses a formula to automatically adjust kernel ...
- linux 逻辑卷管理 /dev/mapper/VolGroup-lv_root 100%调整分区大小
1.解决过程 # df -h // 查看分区 # umount /home // 取消挂载 # e2fsck -f /dev/mapper/VolGroup-lv_home // 分区检测 ...
- Linux进程虚拟地址空间管理2
2017-04-12 前篇文章对Linux进程地址空间的布局以及各个部分的功能做了简要介绍,本文主要对各个部分的具体使用做下简要分析,主要涉及三个方面:1.MMAP文件的映射过程 2.用户 内存的动态 ...
随机推荐
- Python的numpy库中rand(),randn(),randint(),random_integers()的使用
1.numpy.random.rand() 用法是:numpy.random.rand(d0,d1,…dn) 以给定的形状创建一个数组,并在数组中加入在[0,1]之间均匀分布的随机样本. 用法及实现: ...
- node.js官方文档解析 01—assert 断言
assert-------断言 new assert.AssertionError(options) Error 的一个子类,表明断言的失败. options(选项)有下列对象 message < ...
- JavaScript浏览器解析原理
首先,JavaScript的特点是: 1. 跨平台 可以再不同的操作系统上运行. 2. 弱类型 与之相对的是强类型 强类型:在定义变量的时候,需要将变量的数据类型表明.例如:Java 弱类型:定义变量 ...
- Module(CP343-1)Advanced system error SDB generation Error in rule file or rule file not found
报这个故障的原因是因为安装目录下缺少三个 err文件.只要从好的目录下拷贝过来即可. C:\Program Files (x86)\SIEMENS\Step7\s7wbx\rul
- __x__(4)0905第二天__软件架构
软件架构 C/S 架构,客户端/服务器,用户通过客户端使用软件. 一般的应用软件都是 C/S 架构,如 QQ,360 等等. C 为 Client,用户电脑使用的软件. S 为 Server,服务器, ...
- Linux 中进程的管理
Linux 的进程信号 1 HUP 挂起 2 INT 中断 3 QUIT 结束运行 9 KILL 无条件终止 11 SEGV 段错误 15 TERM 尽可能终止 17 STOP 无条件终止运 ...
- Spring Boot2.1.3全局跨域
/** * 配置跨域访问 * * @author Terwer */ @Bean public WebMvcConfigurer corsConfigurer() { return new WebMv ...
- HMAC-SHA256 签名方法各个语音的实现方式之前端JavaScriptes6
sha256和16进制输出,网上很多种后端的验证方法,几乎没有前端的,所以自己写了个,希望给类似需求的人一个帮助,适用场景 腾讯云接口鉴权 v3签名 npm install sha256npm ins ...
- Express全系列教程之(四):获取Post参数的两种方式
一.关于POST请求 post方法作为http请求很重要的一部分,几乎所有的网站都有用到它,与get不同,post请求更像是在服务器上做修改操作,它一般用于数据资源的更新.相比于get请求,post所 ...
- centos 7安装mysql 执行./scripts/mysql_install_db --user=mysql 报错 FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db: Data::Dumper
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql FATAL ERROR: please install the fol ...