Linux迷+Python粉 - 树莓派//blog.pythonwood.com/2017-12-13T22:20:00+08:00树莓派安装kodi神器和红外遥控装置变身多媒体中心2017-12-13T22:20:00+08:002017-12-13T22:20:00+08:00pythonwoodtag:blog.pythonwood.com,2017-12-13:/2017/12/树莓派安装kodi神器和红外遥控装置变身多媒体中心/<h3 id="_1">总述<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3> <p>在大学里通过<span class="caps">IPV6</span>看过<span class="caps">IPTV</span>,最近一两年直播非常火,电视盒子也火了一把。于是想用kodi看直播电视。</p> <p>kodi的低耗,无噪音,便宜的优点我觉得很适合做家庭多媒体服务中心。</p> <p>第二次用树莓派安装kodi,没有上一次那么迷茫了。把一些有价值的东西记录下来。</p> <p><img alt="遥控树莓派kodi.gif" src="//blog.pythonwood.com/uploads/2017/遥控树莓派kodi.gif" title="遥控树莓派kodi"></p> <h3 id="kodikodi-pvr-iptvsimple">一、安装Kodi(请务必一同安装kodi-pvr-iptvsimple)<a class="headerlink" href="#kodikodi-pvr-iptvsimple" title="Permanent link">&para;</a></h3> <div class="highlight"><pre><span></span>sudo apt-get install …</pre></div><h3 id="_1">总述<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3> <p>在大学里通过<span class="caps">IPV6</span>看过<span class="caps">IPTV</span>,最近一两年直播非常火,电视盒子也火了一把。于是想用kodi看直播电视。</p> <p>kodi的低耗,无噪音,便宜的优点我觉得很适合做家庭多媒体服务中心。</p> <p>第二次用树莓派安装kodi,没有上一次那么迷茫了。把一些有价值的东西记录下来。</p> <p><img alt="遥控树莓派kodi.gif" src="//blog.pythonwood.com/uploads/2017/遥控树莓派kodi.gif" title="遥控树莓派kodi"></p> <h3 id="kodikodi-pvr-iptvsimple">一、安装Kodi(请务必一同安装kodi-pvr-iptvsimple)<a class="headerlink" href="#kodikodi-pvr-iptvsimple" title="Permanent link">&para;</a></h3> <div class="highlight"><pre><span></span>sudo apt-get install kodi kodi-pvr-iptvsimple </pre></div> <p>并不是raspbian安装kodi后就能看直播/电视了。如果没有安装kodi-pvr-iptvsimple,kodi侧栏的电视功能无响应,直播/<span class="caps">TV</span>的相关插件也会有问题。我花了不少时间才找到了原因。</p> <p>如果出现kodi的插件里没有&rdquo;<span class="caps">PVR</span>客户端&rdquo;选项可设置,或者搜索不到<span class="caps">PVR</span> <span class="caps">IPTV</span> Simple&nbsp;Client相关插件,或者设置时出现错误提示信息Error,通过安装kodi-pvr-iptvsimple可解决。</p> <h3 id="kodi">二、设置kodi在无需桌面环境下随机自动运行<a class="headerlink" href="#kodi" title="Permanent link">&para;</a></h3> <p>kodi可以通过非<span class="caps">GUI</span>的命令行启动,无需先启动桌面环境,非常省资源。</p> <p>我做过实测树莓派3启动到命令行只需<span class="caps">50MB</span>内存,启动kodi播放则再增加<span class="caps">100MB</span>内存。总共只需<span class="caps">150MB</span>。如果启动桌面环境,桌面环境单独占用<span class="caps">70MB</span>内存。</p> <p>先用<code>sudo raspi-config</code>命令设置boot选项,启动后自动登录到命令行模式(auto boot to&nbsp;cli)。</p> <p>然后在pi用户的.bashrc命令中追加一行</p> <div class="highlight"><pre><span></span>( cd /tmp; ps -ef | grep -v grep | grep kodi || DISPLAY=:0.0 nohup kodi ) 2&gt;&amp;1 &gt;/dev/null &amp; </pre></div> <p>这样树莓派每次都会启动到kodi,但不会多次运行kodi。</p> <h3 id="kodi_1">三、树莓派添加红外模块后实现电视遥控器控制kodi<a class="headerlink" href="#kodi_1" title="Permanent link">&para;</a></h3> <p>虽然安装快控(kodi remote control)App或者yaste&nbsp;App可以在wifi环境下通过App远程控制树莓派运行的kodi。</p> <p>然而我觉得每次先解锁手机,打开软件,然后只为点几下键体验不是很好。</p> <p>所以,在老旧已坏的遥控飞机上拆下红外模块(对讲机,其他遥控玩具都会有这个模块,支持废物利用),结合家里旧的遥控器实现红外遥控树莓派。</p> <p><img alt="遥控飞机红外接受模块" src="http://www.dgpufei.com/Public/Uploads/Article/20170115/587b2a331b16a.jpg" title="遥控飞机红外接受模块"></p> <p><strong>我跟着这个教程<a href="https://blog.newnius.com/raspberry-3b-setup-lirc.html" title="树莓派---红外遥控设置">树莓派&mdash;红外遥控设置</a>设置成功</strong>。</p> <h4 id="kodi_2">树莓派能识别红外遥控器后,就是树莓派kodi学习(映射)按键与指令对应关系的过程。<a class="headerlink" href="#kodi_2" title="Permanent link">&para;</a></h4> <p>kodi配置键名(比如KEY_OK)和kodi动作的映射关系文件路径是/usr/share/kodi/system/Lircmap.xml。</p> <p>以下面mediacenter类型的遥控设备为例,按下该设备某个键,对应lircd接受到ok指令,那么kodi执行select动作。</p> <p>为了省事,我把其中键名最简洁<remote device="mediacenter">的xml段复制后</p> <div class="highlight"><pre><span></span><span class="nt">&lt;remote</span> <span class="na">device=</span><span class="s">&quot;mediacenter&quot;</span><span class="nt">&gt;</span> ... <span class="nt">&lt;up&gt;</span>up<span class="nt">&lt;/up&gt;</span> <span class="nt">&lt;down&gt;</span>down<span class="nt">&lt;/down&gt;</span> <span class="nt">&lt;select&gt;</span>ok<span class="nt">&lt;/select&gt;</span> <span class="nt">&lt;one&gt;</span>1<span class="nt">&lt;/one&gt;</span> <span class="nt">&lt;two&gt;</span>2<span class="nt">&lt;/two&gt;</span> ... <span class="nt">&lt;/remote&gt;</span> </pre></div> <p>粘贴并修改device值</p> <div class="highlight"><pre><span></span><span class="nt">&lt;remote</span> <span class="na">device=</span><span class="s">&quot;guangdianyaokong&quot;</span><span class="nt">&gt;</span> ... <span class="nt">&lt;up&gt;</span>up<span class="nt">&lt;/up&gt;</span> <span class="nt">&lt;down&gt;</span>down<span class="nt">&lt;/down&gt;</span> <span class="nt">&lt;select&gt;</span>ok<span class="nt">&lt;/select&gt;</span> <span class="nt">&lt;one&gt;</span>1<span class="nt">&lt;/one&gt;</span> <span class="nt">&lt;two&gt;</span>2<span class="nt">&lt;/two&gt;</span> ... <span class="nt">&lt;/remote&gt;</span> </pre></div> <p>guangdianyaokong是我在/etc/lirc/lircd.conf文件的name字段名字,可自由修改。只要保持对应一致就可以了。</p> <p>我录制irrecord命令如下,省事的原因是录键名的时候对应取up,down,1,ok等简单词。</p> <div class="highlight"><pre><span></span>irrecord -d /dev/lirc0 guangdianyaokong --disable-namespace </pre></div> <h3 id="kodi_3">四、接下来自由发挥,对kodi自行研究,安装各种插件。<a class="headerlink" href="#kodi_3" title="Permanent link">&para;</a></h3> <p>按照插件库然后从插件库安装插件,或者直接按照目标插件的zip包。</p> <p><img alt="树莓派插件库列表截图" src="//blog.pythonwood.com/uploads/2017/树莓派插件库列表截图.jpg" title="树莓派插件库列表截图"></p> <p>搜集m3u8直播列表,灌给<span class="caps">PVR</span> <span class="caps">IPTV</span>客户端使用。也可以使用<a href="http://blog.sina.com.cn/s/blog_4ae08cc00102wz42.html" title="用Kodi观看网络直播电视:新插件IPTV Tool "><span class="caps">IPTV</span> Tool</a>插件来托管<span class="caps">PVR</span> <span class="caps">IPTV</span>客户端</p> <h3 id="m3u8url">五、直播m3u8列表的url存活测试<a class="headerlink" href="#m3u8url" title="Permanent link">&para;</a></h3> <h4 id="_2">导入播放器中手动测试<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h4> <p>smplayer或者vlc都能打开m3u8文件进行网络播放。所以也能来测试是否存活。只是方法简单直接容易累。</p> <p>可打开播放器界面中打开m3u8文件;命令行则可用smplayer <m3u8_list>, 或者 vlc <m3u8_list>打开。</p> <h4 id="_3">脚本批量快速检测<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h4> <p>(todo&hellip;)</p> <h3 id="_4">提醒<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h3> <p>熟悉Linux环境的话,完成一台树莓派环境配置之后,可随时备份,其它树莓派克隆环境就可以了。具体方法参考另一篇随笔。 <a href="//blog.pythonwood.com/2017/11/树莓派SD卡Linux系统盘复制,10分钟克隆新机/">树莓派<span class="caps">SD</span>卡Linux系统盘复制,10分钟克隆新机</a></p> <h3 id="_5">参考<a class="headerlink" href="#_5" title="Permanent link">&para;</a></h3> <ol> <li> <p>Kodi 开源多媒体播放器 - 神器&nbsp;http://www.cnblogs.com/XieSir/articles/6287387.html</p> </li> <li> <p>6款树莓派kodi插件推荐&nbsp;https://www.htpcbeginner.com/raspberry-pi-kodi-addons/</p> </li> <li> <p>移动网络电视和<span class="caps">IPTV</span>的区别在哪里?&nbsp;http://www.shafa.com/articles/zJmvNRrYFs6uzjhH.html</p> </li> </ol>树莓派初始化环境搭建的一些事2017-12-05T13:46:00+08:002017-12-05T13:46:00+08:00pythonwoodtag:blog.pythonwood.com,2017-12-05:/2017/12/树莓派初始化环境搭建的一些事/<p>第二波研究树莓派功能了,上次主要是当作linux系统来研究,这次当作只能设备来研究。是奔着遥控式家庭影音的方案来研究的,树莓派的基础环境非常重要,所以从新理一理,记录下来。</p> <h3 id="_1">一、奔着遥控式家庭影音的购买方案<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3> <p>经比较,树莓派在京东是挺贵的,鉴于是标准化带电子产品,去天猫买<a href="https://s.click.taobao.com/t?e=m%3D2%26s%3Di1ZaWZT35VIcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67pR2%2B%2F9Hp9mCHGUKWrwhgPms9V29auOuBOBgn3DwXo5fTavSczFePlBPv%2F0P2woSTIiNFfNqYeZfZqRIxUL2qmOWt8ptzziuV9VDE9N5gf5%2FRemoLTADB2DGJe8N%2FwNpGw%3D%3D&amp;pvid=10_120.239.178.0_551_1512529291425" title="树莓派天猫链接">树莓派</a>可以了。我没买<span class="caps">SD</span>卡,将旧的class10的16G的<span class="caps">SD …</span></p><p>第二波研究树莓派功能了,上次主要是当作linux系统来研究,这次当作只能设备来研究。是奔着遥控式家庭影音的方案来研究的,树莓派的基础环境非常重要,所以从新理一理,记录下来。</p> <h3 id="_1">一、奔着遥控式家庭影音的购买方案<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3> <p>经比较,树莓派在京东是挺贵的,鉴于是标准化带电子产品,去天猫买<a href="https://s.click.taobao.com/t?e=m%3D2%26s%3Di1ZaWZT35VIcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67pR2%2B%2F9Hp9mCHGUKWrwhgPms9V29auOuBOBgn3DwXo5fTavSczFePlBPv%2F0P2woSTIiNFfNqYeZfZqRIxUL2qmOWt8ptzziuV9VDE9N5gf5%2FRemoLTADB2DGJe8N%2FwNpGw%3D%3D&amp;pvid=10_120.239.178.0_551_1512529291425" title="树莓派天猫链接">树莓派</a>可以了。我没买<span class="caps">SD</span>卡,将旧的class10的16G的<span class="caps">SD</span>利用上(class10意思是写入速度约<span class="caps">10MB</span>/s)。</p> <ol> <li> <p><strong>我购买并用上的:</strong> <a href="https://s.click.taobao.com/t?e=m%3D2%26s%3DjB5CtrQXlAccQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67rCnyOxJms%2BVtTN3K9waqqis9V29auOuBOBgn3DwXo5fTavSczFePlBPv%2F0P2woSTBN3oW5I7sw9oXcuyoAGSVzy%2Flc8YI1OgQvABqsOAyUlBEellxPfI1OiZ%2BQMlGz6FQ%3D%3D&amp;pvid=10_120.239.178.0_533_1512539397311" title="公牛usb插座天猫链接">公牛usb魔方插座</a>,<a href="https://s.click.taobao.com/t?e=m%3D2%26s%3Di1ZaWZT35VIcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67pR2%2B%2F9Hp9mCHGUKWrwhgPms9V29auOuBOBgn3DwXo5fTavSczFePlBPv%2F0P2woSTIiNFfNqYeZfZqRIxUL2qmOWt8ptzziuV9VDE9N5gf5%2FRemoLTADB2DGJe8N%2FwNpGw%3D%3D&amp;pvid=10_120.239.178.0_551_1512529291425" title="树莓派天猫链接">树莓派</a>,<a href="https://s.click.taobao.com/t?e=m%3D2%26s%3DyO9jlKzcxlwcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67jeJ0D4P3i%2B8UOZr0mLjuses9V29auOuBOBgn3DwXo5fs9cRaxJdiKw3LnkA3bZiA%2F6gPESVKznnDfaTZ11r%2FGF6awgoXsc5TKo%2FiSVAKeldcSpj5qSCmbA%3D&amp;pvid=10_120.239.178.6_72377_1512026724825" title="华趸hdmi转vga线带音频带供电"><span class="caps">HDMI</span>转<span class="caps">VGA</span>带供电</a>,带开关按钮线的电源,杜邦线。</p> </li> <li> <p><strong>不用购买就用上的配件:</strong> 红外接收仪(从坏的遥控玩具拆下可用),旧电视遥控器一个,老<span class="caps">VGA</span>显示屏,旧音箱。</p> </li> <li> <p><strong>我购买而还没用的可选配件:</strong> 七彩<span class="caps">RGB</span>的<span class="caps">LED</span>元件,面包板,<a href="https://s.click.taobao.com/t?e=m%3D2%26s%3DfV%2BZ61WfxfkcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67lKP%2BmjvssA7r8hK%2FDw%2Bbm2s9V29auOuBOBgn3DwXo5fs9cRaxJdiKw3LnkA3bZiA0KFaRH2Tl0r%2BlUnyCWbT92Wt8ptzziuVx4DebA4okkCqN5EGMnCL1bGJe8N%2FwNpGw%3D%3D&amp;pvid=10_120.239.178.0_378_1512536718652" title="HDMI转DVI天猫链接"><span class="caps">HDMI</span>转<span class="caps">DVI</span></a>,光敏电阻。</p> </li> <li> <p><strong>我认为不用买的:</strong>&nbsp;小屏幕(退货了),温度感应,五向摇杆,蜂鸣器,触摸感应器等。</p> </li> </ol> <p><img alt="房间树莓派媒体中心" src="//blog.pythonwood.com/uploads/2017/房间树莓派媒体中心.jpg" title="房间树莓派媒体中心"></p> <h3 id="raspbian">二、树莓派安装官方的raspbian系统并初始化<a class="headerlink" href="#raspbian" title="Permanent link">&para;</a></h3> <h4 id="_2">基础部分教程:<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h4> <p>包括修改Pi密码,修改国内源,显示中文,设置时区为Asia/Shanghai,安装开源中文字体<strong>文泉驿</strong>,安装fcitx谷歌拼音,开启ssh服务,设置wifi连接密码</p> <ol> <li> <p><strong>树莓派上手配置</strong>&nbsp;https://yaogangli.gitbooks.io/raspberry-pi-learning/content/Raspbian.html</p> </li> <li> <p><strong>新手入门树莓派必做的四件事</strong>&nbsp;http://blog.csdn.net/A_lPha/article/details/53116767</p> </li> <li> <p><strong>How to setup multiple WiFi networks?</strong>&nbsp;https://raspberrypi.stackexchange.com/questions/11631/how-to-setup-multiple-wifi-networks/11674</p> </li> <li> <p><strong>ssh服务配置</strong>&nbsp;https://www.raspberrypi.org/documentation/remote-access/ssh/</p> </li> </ol> <h4 id="_3">优化部分教程:<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h4> <p>包括添加u盘自动挂载功能,<span class="caps">VNC</span>服务,优化无网络下树莓派开机时间等</p> <ol> <li> <p><strong>树莓派<span class="caps">USB</span>存储设备自动挂载</strong>&nbsp;http://www.jianshu.com/p/188828d9777a</p> </li> <li> <p><strong>How to change timeout in systemctl</strong>&nbsp;https://unix.stackexchange.com/questions/186162/how-to-change-timeout-in-systemctl</p> </li> <li> <p><strong>安装<span class="caps">VNC</span>服务(tightvncserver)</strong>&nbsp;https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-vnc-on-ubuntu-14-04</p> </li> <li> <p><strong>配置与学习树莓派config.txt配置文件</strong>&nbsp;https://www.zybuluo.com/SiberiaBear/note/328982</p> </li> </ol> <h3 id="_4">三、重要提醒:<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h3> <p>经验证,我安装一大堆东西后,<span class="caps">SD</span>卡使用才4G,其中几百兆是音乐,&nbsp;所以8G卡也是够用的。</p> <p>熟悉Linux环境的话,完成一台树莓派环境配置之后,可随时备份,其它树莓派克隆环境就可以了。具体方法参考另一篇随笔。 <a href="//blog.pythonwood.com/2017/11/树莓派SD卡Linux系统盘复制,10分钟克隆新机/">树莓派<span class="caps">SD</span>卡Linux系统盘复制,10分钟克隆新机</a></p> <h3 id="_5">四、参考<a class="headerlink" href="#_5" title="Permanent link">&para;</a></h3> <ol> <li> <p>开始 Raspberry Pi 项目前需要知道的 10 件事&nbsp;https://www.digikey.com.cn/zh/articles/techzone/2017/feb/10-things-to-know-before-starting-a-raspberry-pi-project</p> </li> <li> <p>树莓派启动那些事&nbsp;http://elmagnificogi.github.io/tags/#RaspberryPi</p> </li> </ol>树莓派SD卡Linux系统盘复制,10分钟克隆新机,raspbain备份恢复2017-11-30T15:02:00+08:002017-12-05T10:12:00+08:00pythonwoodtag:blog.pythonwood.com,2017-11-30:/2017/11/树莓派SD卡Linux系统盘复制,10分钟克隆新机/<p>有旧和新树莓派二个,发现<span class="caps">SD</span>卡的raspbian系统可在不同树莓派之间通用,挺好。</p> <h3 id="_1">学通树莓派系统克隆,兼备安装、备份与恢复能力。<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3> <p>在家里,用了一周时间,将新买的树莓派3,<a href="https://s.click.taobao.com/t?e=m%3D2%26s%3DyO9jlKzcxlwcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67jeJ0D4P3i%2B8UOZr0mLjuses9V29auOuBOBgn3DwXo5fs9cRaxJdiKw3LnkA3bZiA%2F6gPESVKznnDfaTZ11r%2FGF6awgoXsc5TKo%2FiSVAKeldcSpj5qSCmbA%3D&amp;pvid=10_120.239.178.6_72377_1512026724825" title="华趸hdmi转vga线带音频带供电"><span class="caps">HDMI</span>转<span class="caps">VGA</span>带供电</a>(性价比推荐),旧显示屏,旧音箱。组成家庭媒体中心。手机使用bubbleupnp投屏和推送音乐播放 …</p><p>有旧和新树莓派二个,发现<span class="caps">SD</span>卡的raspbian系统可在不同树莓派之间通用,挺好。</p> <h3 id="_1">学通树莓派系统克隆,兼备安装、备份与恢复能力。<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3> <p>在家里,用了一周时间,将新买的树莓派3,<a href="https://s.click.taobao.com/t?e=m%3D2%26s%3DyO9jlKzcxlwcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67jeJ0D4P3i%2B8UOZr0mLjuses9V29auOuBOBgn3DwXo5fs9cRaxJdiKw3LnkA3bZiA%2F6gPESVKznnDfaTZ11r%2FGF6awgoXsc5TKo%2FiSVAKeldcSpj5qSCmbA%3D&amp;pvid=10_120.239.178.6_72377_1512026724825" title="华趸hdmi转vga线带音频带供电"><span class="caps">HDMI</span>转<span class="caps">VGA</span>带供电</a>(性价比推荐),旧显示屏,旧音箱。组成家庭媒体中心。手机使用bubbleupnp投屏和推送音乐播放,听音乐的享受更舒服了,显示屏再大些就更好。</p> <p><img alt="房间树莓派媒体中心" src="//blog.pythonwood.com/uploads/2017/房间树莓派媒体中心.jpg" title="房间树莓派媒体中心"></p> <p>在工作室,还有一个树莓派和支持<span class="caps">HDMI</span>输出的投影仪。如果能复制现有树莓派的系统<span class="caps">SD</span>卡,将省事不少。(重复的事应少做)</p> <h3 id="_2">开始折腾尝试<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3> <p>借助我的个人电脑是ubutnu,可以看见树莓派分区情况,第二个分区14G采用了3G,很多空间是多余的。</p> <p><img alt="gparted看树莓派分区" src="//blog.pythonwood.com/uploads/2017/gparted看树莓派分区.jpg" title="gparted看树莓派分区"></p> <p>用了1天的不断努力尝试终于才成功,囧。</p> <h3 id="1ddddrescue">失败1:使用<span class="caps">DD</span>或ddrescue<a class="headerlink" href="#1ddddrescue" title="Permanent link">&para;</a></h3> <p>不幸的是我是从14.6G卡克隆到14.4G卡,使用dd失败是注定的。使用dd的必要条件是目标卡要更大。</p> <h3 id="2linuxssdetc">失败2:源自笔记本linux系统迁移<span class="caps">SSD</span>的经验——分区一样,复制文件,改etc配置法。<a class="headerlink" href="#2linuxssdetc" title="Permanent link">&para;</a></h3> <p>linux磁盘迁移,只需分区格式化,复制文件,修改etc下的fstab和udev目录的网卡信息,chroot+grub安装。就能完成。</p> <p>然而实践得知,不适用树莓派迁移。启动时卡在&rdquo;smsc95xx 1-1.1:1.0: eth0: register &lsquo;smsc95xx&rsquo;&nbsp;&hellip;&rdquo;之后。无法启动。</p> <p>比较发现,复制时,不会有dev/by-partuuid/目录,而本身树莓派是有这个目录的。</p> <h3 id="ddrawresize2fsqemu-imgdiskdd">成功:dd读取为raw镜像盘、resize2fs缩容分区、qemu-img缩容disk、dd写入成功启动树莓派<a class="headerlink" href="#ddrawresize2fsqemu-imgdiskdd" title="Permanent link">&para;</a></h3> <h4 id="ddrawsd">先dd读取物理盘为raw格式镜像盘。这样就可以方法搞,最坏情况也不影响已有的系统sd卡内容。<a class="headerlink" href="#ddrawsd" title="Permanent link">&para;</a></h4> <div class="highlight"><pre><span></span>$sudo dd if=/dev/sdb of=/home/16G.img bs=128K </pre></div> <h4 id="loopgparted">挂载loop设备以便于gparted能操作<a class="headerlink" href="#loopgparted" title="Permanent link">&para;</a></h4> <div class="highlight"><pre><span></span>$sudo losetup /dev/loop0 /home/16G.img $sudo partprobe /dev/loop0 # 运行后才会出现分区/dev/loop0p1,/dev/loop0p2 </pre></div> <h4 id="14g6gblock6g">第二分区从14G缩容到6G(实际是把分散的Block数据集中到前6G位置。非真正改变分区大小)<a class="headerlink" href="#14g6gblock6g" title="Permanent link">&para;</a></h4> <div class="highlight"><pre><span></span>$sudo e2fsck -f /dev/loop0p2 $sudo resize2fs /dev/loop0p2 6G </pre></div> <h4 id="gparted-fdisk-6g">GParted / Fdisk 更改第二分区为6G<a class="headerlink" href="#gparted-fdisk-6g" title="Permanent link">&para;</a></h4> <p>GParted&nbsp;/dev/loop0打开,然后选择第二分区右击,选择&rdquo;更改/移动大小&rdquo;。设置。</p> <h4 id="loop">卸载恢复loop设备:<a class="headerlink" href="#loop" title="Permanent link">&para;</a></h4> <div class="highlight"><pre><span></span>$sudo partprobe -d /dev/loop0 $sudo losetup -d /dev/loop0 </pre></div> <h4 id="raw">raw虚拟磁盘缩容<a class="headerlink" href="#raw" title="Permanent link">&para;</a></h4> <div class="highlight"><pre><span></span>$sudo qemu-img resize /home/16G.img -8G $qemu-img info /home/16G.img image: xxx.img file format: raw virtual size: 5.9G (6380060672 bytes) disk size: 5.9G </pre></div> <h4 id="ddsd">使用<span class="caps">DD</span>写入新<span class="caps">SD</span>卡,启动成功。<a class="headerlink" href="#ddsd" title="Permanent link">&para;</a></h4> <p>可选:写入sd后可以用resize2fs扩容分区完全用上<span class="caps">SD</span>卡空间,参考上面缩容。</p> <h3 id="_3">总结提升<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h3> <ol> <li>linux下安装树莓派系统到<span class="caps">SD</span>卡,使用dd刻录后还有剩余空间,用resize2fs可让最后分区来占满。</li> <li>使用dd来备份整个硬盘太慢,可以用resize2fs将数据块移动到前面,parted缩小分区,然后刻录硬盘前面有数据部分即可。这样备份文件就不会臃肿。</li> <li>备份镜像恢复时,过程类似初次安装系统。</li> </ol> <h3 id="_4">参考<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h3> <p>把一块硬盘上的 Linux 系统整个复制到新硬盘上的方法. <a href="https://www.v2ex.com/t/183310">https://www.v2ex.com/t/183310</a></p> <p>openstack文档 <a href="http://docs.ocselected.org/openstack-manuals/kilo/image-guide/content/ch_modifying_images.html#losetup-kpartx-nbd">http://docs.ocselected.org/openstack-manuals/kilo/image-guide/content/ch_modifying_images.html#losetup-kpartx-nbd</a></p> <p>使用qemu-img改变镜像文件大小 <a href="http://niusmallnan.com/_build/html/_templates/knowladge/resize_raw_file.html">http://niusmallnan.com/_build/html/_templates/knowladge/resize_raw_file.html</a></p> <p>笔记本硬盘更换记(使用<span class="caps">LVM</span>进行在线数据迁移) <a href="http://cathay4t.blogspot.hk/2009/05/lvm.html">http://cathay4t.blogspot.hk/2009/05/lvm.html</a></p> <p>新电脑+旧系统:教你在不同机器间转移Linux <a href="http://www.geekfan.net/1860/">http://www.geekfan.net/1860/</a></p> <p>usb-creator-gtk 方法和&nbsp;clonezilla方法没有深入研究。</p>