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.用户 内存的动态 ...
随机推荐
- 【Codeforces】【图论】【数量】【哈密顿路径】Fake bullions (CodeForces - 804F)
题意 有n个黑帮(gang),每个黑帮有siz[i]个人,黑帮与黑帮之间有有向边,并形成了一个竞赛完全图(即去除方向后正好为一个无向完全图).在很多年前,有一些人参与了一次大型抢劫,参与抢劫的人都获得 ...
- 动态规划——Burst Ballons
题意:给定n个气球.每次你可以打破一个,打破第i个,那么你会获得nums[left] * nums[i] * nums[right]个积分. (nums[-1] = nums[n] = 1)求你可以获 ...
- Faster数据库研习,一
什么是Faster Faster 是一个很屌的嵌入式KeyValue 数据库项目 我简单的把 微软官网怎么吹的给大家翻译一下: Faster:一个为状态管理而生的嵌入式并发KeyValue ...
- Python-读文件
用python读一个文件,我们一般采用 open('文件名字')这里的文件名可以说完整路径也可以是相对路径(要读取的文件和和代码放在一起) f = open('data.txt')此时我们只是打开了 ...
- windows与linux之间文件的传输
这边记录一下如何在windows与linux之间进行文件的传输,下面是具体的网址. 原文地址::http://blog.csdn.net/shufac/article/details/51966276 ...
- vue_组件间通信:自定义事件、消息发布与订阅、槽
自定义事件 只能用于 子组件 向 父组件 发送数据 可以取代函数类型的 props 在父组件: 给子组件@add-todo-event="addTodo" 在子组件: 相关方法中, ...
- 24 GISer必备知识(一) 坐标系
对于经常使用ArcMap的童鞋,肯定用过属性表中的计算几何的功能,但是有时候会提示面积计算与长度计算禁用 但是选择的明明是 Xian 1980坐标系,这是为什么呢?下面就来讲一讲这些个经常让人“头大” ...
- PHP 简易聊天室 利用redis的订阅发布功能
demo:http://www.200ok.fun:8083/api/chat/list 前言:这个种方式太耗redis连接数,每次订阅都会新起一个进程,仅供练手使用,切勿用于生产环境. 原理:1.P ...
- 在Fastreport里使用的CRC函数
如标题, 是在Fastreport的脚本里运行的CRC计算函数, 包括CRC-16/CRC-32 基本是从网上找的代码, 然后改出来的 至于为什么要在FR的脚本里运行....呵呵 不要在意这些细节(找 ...
- 微信小程序--地图组件与api-模拟器上返回的scale 与真机上不同--bindregionchange触发图标一直闪现问题
场景:根据地理定位获取不同地区的充电桩位置,要求 1.平移的时候,跟随坐标变化展示不同区域的坐标点信息 2.不同的缩放等级,14以下,展示聚合点数据,14以上,展示真正的站点信息: 3.点击聚合点的时 ...